Some of the details of Java

Source: Internet
Author: User

The result symbol for the Java redundancy% depends onThe sign bit of the divisorDecimals can also be redundant, and the remainder is still fractional.
1  Packagecom.test;2 3  Public classTest {4 5     /**6      * @authorJongeron7      */8      Public Static voidMain (string[] args) {9System.out.println (5/3);//1TenSystem.out.println (5%3);//2 One          ASystem.out.println ( -5/3);//-1 -System.out.println ( -5%3);//-2 -          theSystem.out.println ( -5/-3);//1 -System.out.println ( -5%-3);//-2 -          -System.out.println (5/-3);//-1 +System.out.println (5%-3);//2 -          +         //floating-point types ASystem.out.println (20.5%2.5);//0.5 atSystem.out.println (20.2%10.1);//0.0 -System.out.println (10%2.);//0.0 -System.out.println ( -10.8%2);//-0.8000000000000007 -System.out.println (20.168%-10.111);//10.056999999999999 -System.out.println (20.168%10.111);//10.056999999999999 -  in     } -}
Ii. Comparison of String objects

A.string are immutable and cannot be inherited. The string class is internally saved with a char array, and this char array is declared: final. The design of the string class uses the invariant pattern.

The declaration of the B.string class is as follows:

Public final class String implements Java.io.Serializable, comparable, charsequence

C. Three parts of the data area: Java Virtual machine stack (which can be referred to as Java stack), heap, and run-time constant pool (referred to as const pool)

D. Constant pool

The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used by that type, including direct constants (String,integer and floating point constants) and symbolic references to other types, fields, and methods. String literals are stored in a constant pool.

String x = "abc"; String y = new String ("ABCD");

The first statement does not allocate memory in the heap, but instead stores "abc" in a constant pool. For the second statement, there is also a "ABCD" string in the constant pool, and when new, a copy of the string is stored in the heap, and Y points to the "ABCD" string in the heap.

1  Packagecom.test;2 3  Public classTest {4     /**5      * @authorJongeron6      */7     Private StaticString a = "AB";8     Private StaticString AB =NewString ("AB");9      Public Static voidMain (string[] args) {Ten         /** One * When two string literals are concatenated (added), A * The resulting new string is still a string literal, - * saved in a constant pool.  -          */ theString a1= "abc123"; -String a2= "AB" + "C" +123; -System.out.println (A1==A2);//true -         /** + * When the string literal is connected to a string type variable, the - * The resulting new string is no longer stored in the constant pool, + * Instead, create a new string object in the heap to hold.  A * It's obvious that constant pools require constant storage, at * There is a variable of type string that cannot exist in a constant pool.  -          */ -String b1= "abc123"; -String b2= "123"; -String b3= "ABC" +B2; -System.out.println (B1==B3);//false in         /** - * Here is a string literal connected to a string type constant. to * The resulting new string is still stored in the constant pool.  +          */ -String C1 = "AB"; the         FinalString C2 = "B"; *String C3 = "a" +C2; $SYSTEM.OUT.PRINTLN (C1 = = C3);//truePanax Notoginseng         /** - * Final String d2 = get123 (); the * Actually with final string d2 = new String ("123");  + * This means that return "123" creates a string object in the heap to save "123", A * Although D2 is defined as final. Visible is not defined as final and is saved in a constant pool. the * It is obvious that the string object referenced here D2 constant is saved in the heap. + * Because the string that get123 () has been saved in the heap, - * Final String reference does not change the fact that string has been saved in the heap.  $          */ $String D1 = "abc123"; -         FinalString D2 =get123 (); -String d3 = "ABC" +D2; theSystem.out.println (D1 = = D3);//false -         /**Wuyi * Intern () method the * Returns a normalized representation of a string object.  - * An initially empty string pool, which is privately maintained by the class string.  Wu * When calling the Intern method, if the pool already contains a string equal to this string object - * (determined with the Equals (Object) method) returns the string in the pool.  About * Otherwise, this string object is added to the pool and a reference to this string object is returned.  $ * It follows the following rules: For any two strings s and T, when and only if S.equals (t) is true, - * S.intern () = = T.intern () is true.  - * All literal string and string assignment constant expressions are manipulated using the Intern method.  -          *  A          */ +String S1 = "a"; theString s2 = "B"; -String s = S1 +S2; $System.out.println (s = = a);//false theSystem.out.println (s.intern () = = a);//true the         //////////////////////////////// theString SS1 = "a"; theString SS2 = "B"; -String SS = Ss1 +SS2; inSYSTEM.OUT.PRINTLN (ss = = AB);//false theSystem.out.println (ss.intern () = = AB);//false theSystem.out.println (ss.intern () = = Ab.intern ());//true About     } the  the     Private StaticString get123 () { the         return"ABC"; +     } -}

Some of the details of 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.