Javase (vi) The difference between a wrapper class, a conversion between a base type and a string, = = and equals

Source: Internet
Author: User
Tags character classes

first, Packaging class

The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, which in practical use there are a lot of inconvenience, in order to solve this problem,

when designing a class, a corresponding class is represented for each base data type, so that the classes corresponding to the eight and the basic data types are collectively referred to as wrapper classes (Wrapper Class), and some are translated into the outer or data type classes.

1.1, packaging classes are located in the Java.lang package, the corresponding relationship between the packaging class and the basic data types are shown in the following table:

Primitive-type Wrapper-class
BYTE byte
Short Short
int Integer
Long Long
float float
Double Double
Boolean Boolean
Char Character

Note: In these eight class names, in addition to the integer and character classes, the class names of the other six classes are identical to the basic data types, except that the first letter of the class name is capitalized

1.2, the location of the packaging class

  wrapper types are classes under Java.lang, so you do not need to import packages when you use them .

1.3. Each wrapper class defines properties and methods for use by its objects

This is the most obvious difference from a basic type to a wrapper type.now point to the object, you can access the properties in the object and call the methods in the object ., previously just a simple numeric value, without any properties and methods.
For example:
Using variable I cannot access any properties and access
Because the variable i does not point to the object and cannot point to the object
int i = 1;

The view API shows that constructors in the integer class use the
Use the variable i to access the properties and methods defined in the integer class
Because the variable i is pointing to an object, this is a variable of reference type
Integer i = new integer (1);
Or
Integer i = new Integer ("1");

Note:the properties and methods in the wrapper class are mostly static and can be accessed directly using the class name. (There are also non-static methods that need to be called with the object)

For example:
Main
System.out.println (Integer.max_value);
System.out.println (Integer.min_value);
System.out.println (integer.tobinarystring (100));

1.4, the common method

    

Simple to use:

        

1.5, the role of packaging class

1) as the class type corresponding to the basic data type exists, it is convenient to involve the operation of the object.

2) contains relevant properties for each basic data type, such as maximum value, minimum value, and related operation methods.

1.6, JDK1.5 added automatic unboxing box function (low version of the JDK compilation error)

Note: For eight basic types and their packaging types, here is an example of int and integer

Auto-boxing, wrapping the literal value 1 as an integer type Object
Integer i1 = 1;

Automatic unpacking, converting an integer object to a simple int type value
int i2 = new Integer (1);

Note:
Compiled by
Integer a = 1;

//Compile Error
//1 can be automatically boxed as an integer type Object
//But integer and long do not have any relationship
Long b = 1;

Because int is a 32-bit long is a 64-bit
INT--can be automatically converted to-long

Because integer and long do not have a child parent class relationship
Integer--cannot be converted to-Long

ii. conversions between basic types and strings  

In program development, we often need to convert between the basic data type and the string.

2.1. There are three ways to convert a basic type to a string:

1) Use the ToString () method of the wrapper class

2) using the ValueOf () method of the String class

3.) using an empty string with the base type, you get the string that corresponds to the base type data

  

  

2.2. There are two ways to convert a string to a basic type:

1) Call the Parsexxx static method of the wrapper class

2.) Call wrapper class's ValueOf () method to convert to a basic type of wrapper class, which will be automatically unboxing

    

three, = = and the Equals method difference

Both of these two variables are compared to each other.
1) Comparison of basic type variables
The base type variable points to not an object, cannot call a method, so it can only be compared by using = =, and compares simple values of basic type variables for equality.

2) Comparison of reference type variables
A reference type variable points to an object, so you can use = = to compare, or you can use equals to compare

The difference is:
Equals is a method in object, and each object can be called to compare equality with other objects, by default comparing the object memory address values pointed to by the two references (as with the = = number), but you can also override the Equals method in your class to compare two objects according to your own logic.

= = is a basic operator in Java, it cannot be overridden, and two references are compared using = =, so the value of the memory address that the reference points to is the same.

Hashcode method of ToString Method

Both ToString and Hashcode are methods in the object class, so each object can be called directly.

The Hashcode method returns the hash code value of the object, which is typically achieved by converting the memory address of the object to an integer.

The tostring method that returns the string representation of the object.
the form is:
the fully qualified name of the class @hashcode method returns the 16 binary form of the value
That
O.getclass (). GetName () + "@" + integer.tohexstring (O.hashcode ())


For example:
public class student{

}
Main
Student s = new Student ();
String str1 = s.tostring ();
String str2 = S.getclass (). GetName () + "@" +integer.tohexstring (S.hashcode ());
System.out.println (STR1);
System.out.println (STR2);

Output Result:
[Email protected]
[Email protected]


Note: We can think of the last six decimal digits as the memory address of this object, but it is not really the address value, but the object's hash code value, this hash code value by default is a number converted from the object address value. (If we rewrite the Hashcode method, the returned hash code value really doesn't have anything to do with the object memory address.)


For example:
Student s = new Student ();
When you print a reference, the ToString method of the object that it points to is called by default
System.out.println (s);
System.out.println (S.tostring ());

Note: Sometimes it's a little different.
Student s = null;
Print null
System.out.println (s);
Run error, NULL pointer exception
System.out.println (S.tostring ());

 


Javase (vi) The difference between a wrapper class, a conversion between a base type and a string, = = and equals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.