Java Basic Learning--1, basic data types

Source: Internet
Author: User
Tags wrapper

one or four big basic data types:

1. Logical Boolean:

Boolean data only allows values of true or false, and can not be substituted with 0 or not 0.

2, character Char:

A single character that is enclosed in single quotes for character constants, for example:

1 char Wechar = ' a '; 2 char Wechar = ' I ';

3, Integer type (byte, short, int, long)

decimal integers, such as: 12,-314,0;

octal integers, which require starting with 0 , such as:012;

A hexadecimal integer that requires a 0x or 0X start, such as:0x12.

The integer constants of the Java language default to int, declaring that a long constant can be followed by an ' l ' or ' l ', such as:

1 int i1 = 600;
2 long i2 = 88888888888888L;

4. Float type (float, double)

Java floating-point constants are double by default, and if you want to declare a constant of type float, you need to add F or Fafter the number, such as:

1 double d = 1234.9; 2 float F =123.3f;

5. Special String type

String in Java is a special wrapper class data, which has the following two forms of creation:

1.String s = "abc";

First: First create an object reference variable s in the stack to the string class, and then go to find if "abc" is stored in the string constant pool, and if not, create three char values ' a ', ' B ', ' C ' in the stack, and then create a string object in the heap with object, The value is the array {' A ', ' B ', ' C '} that was created in the stack of the three char values, and then the string object is stored in a constant pool of strings, and the S is then pointed to the address of the object. If ' abc ' is already stored in the string constant pool, the object with the value ' ABC ' is found in the string constant pool and the S is pointed to the address of the object.



Memory

The feature of this creation is that the JVM will automatically determine if it is necessary to create a new object based on the situation in the constant pool.

2.String s = new String ("abc");

The second method of creation can be decomposed into two steps:

①string object = "abc";

②string s = new String (object);

The first step is to refer to the first method of creation, and the second, because "ABC" has been created and saved to a string constant pool, the JVM will only create a new string object in the heap, whose value shares the three char values already in the stack.

Before you compare strings, you must understand the difference between = = and equals:

For the string class, the address of two string objects is compared, and equals is used to compare the content values of two string objects.

Example 1: Use of a string constant pool

1 String s0 = "abc"; 2 String s1= "abc"; 3 System.out.println (S0==S1); // true  to see that S0 and S1 are pointing to the same object. 

The difference between = = and equals in example 2:string

1 String s0 =newstring ("abc"); 2 String s1=newstring ("abc"); 3 System.out.println (S0==S1); // false to see that new is used to generate different objects 4 System.out.println (s0.equals (S1)); // true to see that equals compares the contents (values) of two string objects

Example 3: Compile-time determination

1 String s0= "HelloWorld"; 2 String s1= "HelloWorld"; 3 String s2= "Hello" + "world"; 4 System.out.println (S0==S1); // true to see that S0 and S1 are the same object 5 System.out.println (S0==S2); // true to see that s0 and S2 are the same object

Analysis: Because the "HelloWorld" in S0 and S1 in the example are string constants, they are determined at compile time, so s0==s1 is true, and "Hello" and "World" are also string constants, when a string is concatenated with multiple string constants. It is certainly also a string constant, so S2 is also parsed as a string constant at compile time, so S2 is also a reference to "HelloWorld" in the constant pool. So we come to s0==s1==s2;

Example 4: Compilation period cannot be determined

 1  String s0= "HelloWorld" ;  2  String s1=newstring ("HelloWorld"  3  String s2= "Hello" +newstring ("World"  4  System.out.println (S0==S1); // false  5  System.out.println (S0==S2); // false  6  System.out.println (S1==S2); // false  

Parsing: Strings created with new string () are not constants and cannot be determined at compile time, so the strings created by new string () are not placed in the constant pool, they have their own address space. S0 is also a reference to "HelloWorld" in the constant pool, s1 because it cannot be determined at compile time, so it is a reference to the new object "HelloWorld" created at run time, S2 because there is a second half part new String ("World") so it cannot be determined at compile time, So it is also a reference to the newly created object "HelloWorld";

Example 5: Compile-time optimization

 1  String s0 = "A1"  2  String s1= "a" + 1;  3  System.out.println ((s0== s1)); // result = truestring s2 = "Atrue";  4  String s3= "a" + "true"  5  System.out.println ((s2== S3)); // result = truestring S4 = "a3.4";  6  String s5= "a" + 3.4;  7  System.out.println ((a== b)); // result = True  

Analysis: During the program compile period, the JVM optimizes the "+" connection of the constant string to the concatenated value, and with "a" + 1, the compiler optimizes it to be A1 in class. The value of its string constants is determined at compile time, so the final result of the above program is true.

Example 6: Compilation period cannot be determined

1 String s0 = "AB"; 2 String s1= "B"; 3 String s2= "a" +S1; 4 System.out.println ((s0== S2)); // result = False

Analysis: JVM for string reference, because in the string "+" connection, there is a string reference exists, and the reference value in the program compilation period is not determined, that is, "a" + S1 can not be optimized by the compiler, only during the program run time to dynamically allocate and the new address after the connection to S2. So the result of the above program is also false.

Example 7: Compile-time determination

1 String s0 = "AB"; 2 Final  String S1 = "B"; 3 String s2= "a" +S1; 4 System.out.println ((s0== S2)); // result = True

Analysis: The only difference between [6] is that the S1 string has a final decoration, and for a final modified variable, it is parsed at compile time to a local copy of the constant value stored in its own constant pool or embedded in its byte stream. So the "a" + S1 and "a" + "B" effects are the same at this point. Therefore, the result of the above program is true.

Example 8: Compilation period cannot be determined

1 String s0 = "AB"; 2 Final  String S1 = getS1 (); 3 String s2= "a" +S1; 4 System.out.println ((s0== S2)); // result = False 5 Private Static String getS1 () {"B";}

Analysis: The JVM for the string reference S1, its value in the compilation period can not be determined, only after the program run time call method, the return value of the method and "a" to dynamically connect and assign the address to S2, so the result of the above program is false.

about the immutable design of string:

String is a typical representation of immutable classes (remember: basic types of wrapper classes are immutable) and is a typical application of the immutable design pattern, which cannot be changed once the string variable is initialized, prevents changing the state of the object, increases the robustness of the shared object, and reduces the error of object access. , and also avoids the need to synchronize during multi-threaded sharing.

The implementation of the immutable model has the following two main points:

1. In addition to constructors, there should be no other function (at least any public function) to modify any member variable.

2. Any function that makes a member variable get a new value should keep the new value in the new object, leaving the original object unmodified.

The non-variability of string causes the cost of using the + sign for the strings variable:

  Example 9:

1 String s = "a" + "B" + "C"; 2 String s1=  "a"; 3 String s2=  "B"; 4 String s3=  "C"; 5 String s4=  s1  +  s2  +  S3;

Analysis: The creation of the variable S is equivalent to String s = "abc"; The compiler is optimized by the example above, where only one object is created. It is also known from the above example that S4 cannot be optimized at compile time, and its object creation is equivalent to:

1 New StringBuffer (); 2 temp.append (S1). Append (S2). Append (S3); 3 String s = temp.tostring ();

From the above analysis results, it is not difficult to infer that the string using the Join operator (+) inefficiency reason analysis, such as the code:

 1  public  class   Test { 2   Public  static  void   main (String args[]) { 3  String s=null  ;  4  for  (inti = 0; i <; I++ 5  s+= "a"  ; 6  }  7 }  8 } 

Every time you do it, you produce a StringBuffer object and then throw it away after append. The next time the loop arrives, it re-generates the StringBuffer object and then append the string so that it loops until the end. If we use the StringBuffer object directly for Append, we can save N-1 time to create and destroy objects. So for applications that want to concatenate strings in a loop, the StringBuffer or Stringbulider objects are generally used for append operations.

Java Basic Learning--1, basic 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.