Java Data types

Source: Internet
Author: User
Tags wrapper

Data types in Java
Primitive types: int, short, long, double, float, Boolean, char Note: There is no basic type of string, there is Yu Ching

Wrapper class data: Integer, String, double, etc. wrapper class, exist in heap

First concept:
Automatic Boxing and unpacking:
When you assign a value to a wrapper class with a value of the base type, the conversion of the base type to the wrapper type occurs:"auto-boxing"
When you assign a value to a base type with the value of the wrapper class, the system automatically converts the wrapper class to the base type: Auto-boxing

Second instance: Integer, double, float cache

Cached cache for Integer, double, float:

Integer: In Java 5, a new feature has been introduced for the operation of Integer to save memory and improve performance. An integer object implements caching and reuse in an internal implementation by using the same object reference. The above rules apply to integer intervals-128 to +127,

It is also important to note that the integer literal is cached only when it is assigned, and an integer object created with the constructor cannot be cached.

Like what:

Integer abc=New// does not have a cache for this operation

Other cached objects

This caching behavior applies not only to integer objects. We have a similar caching mechanism for classes of all integer types.

Has bytecache for caching Byte objects

Have Shortcache for caching short objects

Have Longcache for caching Long objects

Have Charactercache for caching Character objects

The Byte,short,long has a fixed range: 128 to 127. For Character, the range is 0 to 127. except the Integer can be changed by the parameter range, the others are not.

SYSTEM.OUT.PRINTLN ("Auto-boxing and auto-unpacking"); Integer a=1; Integer b=2; Integer C=3; Integer d=3; Integer e=321; Integer F=321; Long g=3l; System.out.println (c==d);//true//CacheSystem.out.println (E==F);//false//out of range of cacheSystem.out.println (C.equals (a+b));//True The first step is auto-unpacking and then compares the values of two intSystem.out.println (c== (a+b));//true//arithmetic operations, automatic unpacking, C changed to Int,a+b to int, and then two int values are comparedSystem.out.println (g== (a+b));//true//First, the a+b is automatically unboxing to int, then the implicit type conversion is involved, Int-->long,System.out.println (G.equals (a+b));//false a+b for unpacking,intA=1; Integer B=1; System.out.println (B.equals (A)); //trueSystem.out.println (B==a);//true


String: is a special wrapper class data
String str= "ASD"; it takes three steps to create a string type
1. First look in the constant pool for the existence of a string object with the content "ABC"
2. If it does not exist, create an "ABC" string object in the constant pool and have str reference the object
3. If present, let Str refer to the object directly

String string= "Asdas"; String string1= "Asdas"; System.out.println (string==string1); \true

String str = new String ("ABC") The process of creating an instance
1 First define a str string reference and store it in the stack

2 See if there is a string object with the content "ABC" in the string constant pool

3 If present, skip this step and, if it does not exist, create a string object with the content "ABC" in the string constant pool

4 performs a new operation, creates a specified object "ABC" in the heap, where the heap object is a copy object of the string constant pool "ABC" Object

5 Let str point to the "ABC" object in the heap (that is, the address in the heap where the object is stored)

Scenario One:
In a Java Virtual machine (JVM), there is a string constant pool that holds many string objects
And can be shared, so it improves efficiency, because when the string class is final, its value cannot be changed once it is created.
Strings are maintained by the string class, and we can call The Intern () method to access the string pool

String s1= "ABC"; // The string creates an object String s2= "ABC"; // "ABC" exists in the string pool and generates an object S2 point to "ABC" System.out.println (S1==S2); // true System.out.println ("+s1.equals (S2)"); // true

Scenario Two:

String s3=New string ("ABC"); // The string pool generates an "ABC" object, and the heap generates an object that points to the string "abc" . and generates a S3 reference to the object in the heap string S4=new string ("ABC"); // "ABC" already exists in the string pool, a new object is generated in the heap, and a reference to the object in the heap is generated S4 S4==S3   //false    addresses in the heap and in the string pool are not the same

Scenario three; The Intern () method can return a reference to the object in the constant pool of the string

String str1 = "abc";         = NewString ("abc"). Intern ();        System.out.println (str1==str2);   // true


Scenario four: "false" change, true generation: seemingly changed, in fact, it really generated

String str1 = "abc"= "abc"//String Reference change point pointing to "BCD"//  When the JVM finds an address in a constant pool that does not hold the value, it opens up this address and creates an object,System.out.println (str1 + "," + str2);  // BCD, ABC      System.out.println (STR1==STR2);  // false

Scenario Five: Use of macro variables, macros instead

String str1 = "abc";         Final String str2 = "AB"= str2+ "C"; System.out.println (str1==str3);  // true  = "abc";         = "AB"= str2+ "C"; System.out.println (str1==str3);  // false

When it comes to str2 is a variable, STR3 cannot generate a macro instead, first generating a StringBuilder and then append (str2). Append ("C")
Then let STR3 point to the object returned by Stringbuilder.tostring ()

String S3=new string ("ABC");
The string pool generates an "ABC" object, and the heap generates an object that points to the string "abc".
and generates a S3 reference to the object in the heap
String S4=new string ("ABC");
"ABC" already exists in the string pool, a new object is generated in the heap, and a reference to the object in the heap is generated S4
S4==S3 the address in the//false heap and the string pool are not the same

Java Data types

Related Article

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.