Java basic type Summary

Source: Internet
Author: User
Tags comparable

I have been summing up and reflecting on myself recently. Although I have practice at the company, I have not felt that I have made great progress, I didn't feel that I had made great achievements when I was developing something in school. So I started to learn Java from the basics while I was on leave for free at school, enrich yourself.

I. Data Types

From this, we can clearly see the types in Java, where the red box contains four basic data types in Java:

The following describes the basic types in detail:

1. Integer type

1) in Java, there are four types of integer types, which have a fixed expression range and field length, and are not affected by the specific operating system, ensuring the cross-platform Java

2) in Java, integers are represented in three forms:

A. Decimal integer, Such as 120,-31, 0

B. octal integers, Must start with 0, such as: 012,

C. hexadecimal integer, Must start with 0x, such as: 0x12

3) in Java, the default Integer type is int. When the long type is declared, an l or L should be followed. We recommend that you use uppercase L to prevent and number 1, uppercase letters, such as 30L and 30l

4) Four integer types in Java:

  

2. Floating Point Type

1) Like the integer type, it also has a fixed expression range and field length, and is not affected by the specific operating system, ensuring the cross-platform Java.

2) in Java, the floating point type has two forms:

A. decimal number format, Such as 3.14

B. Scientific notation, For example, 3.14e2, 3.14e-2,-3.14E2

3) in Java, the default floating point type is double. When the float type is declared, the following letter f or F is added, such as 3.14F and 3.14f.

4) two floating point types in Java:

  

3. Character Type

1) The character type is a single character enclosed by single quotation marks (''), for example, char c = 'W'; char a = '中 ';

2) Java characters are encoded in Unicode (globally unified encoding). Each character occupies two bytes and can be expressed in hexadecimal encoding format, for example, char c = '\ u0061'

3) in Java, escape character '\' can be used to convert the characters following it into other meanings, for example, char c = '\ n'; // indicates line feed.

4. boolean type

1) there are only two values of the boolean Type: true and false.

2) The boolean type cannot be converted to another data type.

5. Conversion of Basic Data Types

1) boolean cannot be converted to another data type.

2) integer, floating point, and period types can be converted to each other. The following principles are observed during conversion:

A. Small data types are automatically converted to large data types, and the data types are sorted by capacity:

Byte, short, char <int <long <float <double

B. byte, short, and char types are not converted to each other. During calculation, the three types are first converted to the int type.

C. When converting a type with a large capacity to a small type, a forced conversion character must be added, which may cause reduced precision or overflow problems.

D. When there are multiple data types in hybrid computing, the system first automatically converts the data type to the one with the largest capacity before continuing the calculation.

E. The default value of the real number type is double, for example, 1.2. The default value of the integer type is int, for example, 1.

Ii. JVM processing of Basic Types 1. Constant pool technology

1) in java, most of the basic packaging classes implement the constant pool technology. These classes are Byte, Short, Integer, Long, Character, Boolean, the other two floating-point type packaging classes are not implemented.

2) The object pool can be used only when the corresponding values of the five Integer packaging classes Byte, Short, Integer, Long, and Character.

The following describes how to use the Long type.

First, write a test class:

LongTypeTest. java

LongParam = 30L Long longParam2 = 30L}View Code

After compiling through the javac command, we can use the jad command to generate a compilation file to view the compilation information, as shown below:

LongParam = 30L Long longParam2 = Long. valueOf (30L}View Code

From row 26th, we can see that the valueOf method in the Long class is called when the packaging class is initialized. Let's take a look at the method in the Long class.

       Long valueOf(           offset = 128                   (l >= -128 && l <= 127) {               LongCache.cache[()l +                         }

The Code shows that when the value of l is less than 127, it will call LongCache. cache () to obtain the value in the constant pool. LongCache is an internal class.

                                          Long cache[] =  Long[-(-128) + 127 + 1                           ( i = 0; i < cache.length; i++                 cache[i] =  Long(i - 128      }

Other bytes, Short, Integer, Long, Character, and Boolean are similar.

In the valueOf of Double, we can see that the source code is like this:

   Double valueOf(            }

The same is true for Float.

2. short, int, char-> byte (char)

It is estimated that everyone is very surprised to see this. What exactly does this title mean? Next we will explain:

1) when the short, int, and char values are less than 127 and greater than-128, the compiler uses the byte type for recording in memory.

2) When the short, int, and char values are greater than 127 but less than 215-1, the compiler will use the char type for recording in memory.

In other words, byte, short, int, and char are four basic types. The compiler uses the smallest unit in memory to store the content of their values.

Next, we will write a test class to verify it:

Bytes = 10 Byte bytes = 10 bytes Param = 40 Short shrotParam2 = 40 intParam = 20 intParam2 = 20 Integer integerParam = 20 longParam = 30 Long longParam2 = 30L doubleParam = 20.0 doubleParam4 = 20.0 Double doubleParam2 = 20.0 Double doubleParam3 = 20.0 floatParam = 20366f Float floatParam2 = 20366f booleanParam = Boolean booleanParam2 = charParam = 'A' charParam2 = 'A' Character characterParam = 'A '}View Code

2) continue to view the compilation information:

Byte0 = 10 Byte byte1 = Byte. valueOf () 10 byte2 = 40 Short short1 = Short. valueOf () 40 byte3 = 20 byte4 = 20 Integer integer = Integer. valueOf (20 l = 30L Long long1 = Long. valueOf (30L d = d1 = Double double1 = Double double2 = f = Float float1 = flag = Boolean boolean1 = Boolean. valueOf (byte5 = 97 byte6 = 97 Character character = Character. valueOf ('A '}View Code

After reading the above, you may have such a question: short and char are both two bytes. Why should we choose to use the char type for storage during storage?

Answer: Because short is signed, and char is non-typed, the value range can be larger than short, although it is also two bytes, however, the char type is selected instead of the short Type for value storage.

3. Relationship between final, Number, and Comparable and basic type packages

1)All the basic types of packaging classes are modified using final to ensure the security of the system.Suppose someone has customized such a java. lang. integer inherits the java. lang. integer, and some Destructive code is implemented in it, the consequences will be unimaginable, right.

2)Except Character and Boolean, all other basic types of packaging classes inheritAbstract classNumber.This abstract class requires that the inherited class must convert the value of the current type to another type of value method.

3)All basic type packaging classes implement the interface Comparable.The function of this interface is:

A. When objects in List/Array implement this interface, you can use the Collections. sort () method for automatic sorting.

B. when a Common Object implements this interface, it can be used as an automatic sort (for example, TreeMap) the sorting in the Map Set is based on the key or the Set with the automatic sort function.

 

  

 

 

 

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.