Basic data types in Java

Source: Internet
Author: User

There are eight big data types in Java Boolean (undecided) char (2 bytes) byte (1 bytes) short (2 bytes) int (4 bytes) long (8 bytes) float (4 bytes) double (8 bytes), and the corresponding wrapper class Boolean Character Byte Short Integer Long Float Double, the conversion between the basic data and the wrapper class is called boxing and unboxing

        New Integer (10); // Boxing This can only be done before the java1.5.        Integer i1 = 10; // after java1.5, you can initialize an integer object directly . New Integer (ten);                           int // It's called unboxing .    

What's the difference between int i and integer i, see the program:

        Integer n1 = 127;         = 127;         New Integer (ten);         = ten;        System.out.println (N1.equals (N2));        SYSTEM.OUT.PRINTLN (N1= =n2);                System.out.println (n3= =N4);        System.out.println (N3.equals (N4));    

The result is true
True
False
True
Java "= =" Comparison is the address of the object, and equal compared to the content, N3 and N4 compared to the results, then why N1 and N2 "= =" Comparison is true, see the value of 127 is the maximum value of Byte, Java in the byte range of data because of the use of more, So this object will be stored in memory, N1 and N2 point to the same memory address, but more than 127 or less than 128 result is

        Integer n1 = -129;         = -129;         New Integer (ten);         = ten;        System.out.println (N1.equals (N2));        SYSTEM.OUT.PRINTLN (N1= =n2);                System.out.println (n3= =N4);        System.out.println (N3.equals (N4));    

True
False
False
True

The wrapper class has some common methods, mainly the data type conversion, except the Boolean char constructs the method to have two kinds, but the float has three kinds.

The conversion method of the numeral, use integer to do example. Bytevalue ();d oublevalue (); floatvalue;intvalue;shorvalue; All of these methods are unboxing and return basic data types.

In turn valueOf (int i) is put back into the wrapper class.

PS: Basic data type conversion follows the principle:

1 small size types are automatically converted into large data types, data types are sorted by capacity size

byte,short,char<int<long<float<double; Note that this is the capacity size is not the number of bytes

2 When a large data type is converted to a small-capacity data type, a cast character is added, but the precision can be reduced or overflowed.

3 There are a number of data type blending operations, the system will automatically convert all the data into the largest type, and then perform the operation.

4 Real constants, the system defaults to double;

5 integer constants, the system defaults to int;

So long lg = 10000000000l; Add an L later, otherwise the system will be saved because the default is int out of range

Basic data types in Java

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.