Basic data types and constant pools in Java

8 basic data types in java

|------------ Integer

         |-----byte

         |-----short

         |-----int

         |-----long

|------------ float

         |-----float

         |-----double

|------------ character

         |-----char

|------------ boolean

         |-----boolean       

 

It is worth mentioning that the null keyword, which does not belong to Object, indicates that a reference data type is not assigned a value.

For example: String name;

           System.out.println(name);// Compile error, name is not assigned

           String name = new String();

           System.out.println(name);// name is an "" empty string!

          

           class Temp{

           String name;

         }

          System.out.println(new Temp().name);// output null

          Summary: This shows that null means that a reference data type is not assigned a value!

 

2.   In order to run faster and save memory, Java provides a constant pool for these eight data types and String. Similar to system-level caches , the difference between a string constant pool and a new object is generally asked during interviews, equal method, the usage of ==.

 

  • Objects declared directly with double quotes Stringare stored directly in the constant pool.
  • StringIf the object is not declared with double quotes , the Stringprovided internmethod can be used. The intern method will query whether the current string exists from the string constant pool. If it does not exist, it will put the current string into the constant pool.

        String s = new String("1");
        s.intern();
        String s2 = "1";
         System.out.println(s == s2);//false    String s = new String("1") + new String("1");    s = s.intern();      System.out.println(s.intern());      String s2 = "11";      System.out.println(s == s2); //true
     




 

      String s3 = new String("1") + new String("1");
      s3.intern();
      String s4 = "11";
      System.out.println(s3 == s4);   //false

 

问题:String s3 = new String("1") + new String("1");

How many objects were created in total, which ones? Welcome to criticize and correct the above mistakes!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326595899&siteId=291194637