I. Basic CONCEPTS
Basic data types in ①java (simple type, built-in type):
byte type (byte), short Integer, Integer (int), long, character (char), floating point (float), double type (double), Boolean (Boolean), note all lowercase, no string type, The string type is not a basic data type in Java.
② variable types (differentiated by variable scope):
Global variables, member variables, local variables.
③ Heap:
Holds all new objects that are referenced in the stack, when the reference in the stack disappears after the object in the heap is reclaimed by the garbage collector.
④ Stack:
A reference to a variable of the base type and a reference to another object, but the object itself is not stored in the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool. )
The data size and life cycle in the stack can be determined, and the data disappears when no references point to the data.
⑤ constant Pool:
Holds string constants and basic type constants (final decorated data that can be determined during compilation and saved in a compiled. class file).
1 PackageCn.edu.whu.compare;2 3 Public classComparetest {4 Public Static voidMain (string[] args) {5 //"AB" exists in a constant pool, a points to the address of "AB" in the constant pool6String a = "AB";7 //"B" exists in a constant pool8 FinalString BB = "B";9String cc = "B";TenString B = "a" +BB; OneString C = "a" +cc; A /** - * The value of variable B is compiled to determine the address of "AB" in the constant pool, because BB uses the final modifier and BB points to the determined string - * The value of variable C is determined only at run time, and variable C is the equivalent of a string object that is new at runtime, stored in the heap the */ -System.out.println (BB = = cc);//true -System.out.println (A = = B);//true - /** + * A points to "AB" in the constant pool "ab" Address C points to the address of the object "AB" in the heap created at run time - * C.intern () returns a String object that points to the address of the object "AB" in the constant pool pointed to by the--->c + */ ASystem.out.println (A = = c);//false atSystem.out.println (A = = C.intern ());//true - } -}
Two. The difference between equals () and = =
① about = = operator
This operator compares two objects (various variable names in the previous instance, constant names) in the stack (is a memory address that has the contents of the actual object) and returns True if the value is equal, otherwise false
The Equals method defined by the ②object class:
1 Public Boolean equals (Object obj) {2 return ( this = = obj); 3 }
That is, the equals of the object class directly compares the memory address of two objects by = =, so that any subclass inheriting from the object class does not override the Equals method in fact, the equals and = = Two comparison method results are the same.
The Equals method defined by the ③string class
1 Public Booleanequals (Object anobject) {
When the memory address of the two comparison objects is the same, it is not necessary to directly return true in comparison, because the memory address is equal, the contents must be the same AH!! 2 if( This==anobject) {3 return true;4 }
Compares each character in a string with only two objects equal to each character value equals5 if(AnObjectinstanceofString) {6String anotherstring =(String) anobject;7 intn =value.length;8 if(n = =anotherString.value.length) {9 CharV1[] =value;Ten CharV2[] =Anotherstring.value; One inti = 0; A while(n--! = 0) { - if(V1[i]! =V2[i]) - return false; thei++; - } - return true; - } + } - return false; +}
The excess is not explained by reference to the above comments.
Copyright, reproduced Please specify the source!
java-resolving equals () and = = from a stack constant pool