Object comparison

xiaoxiao2021-03-06  67

Class a {Int a = 1;}

Public class equaltest {public static void main (string [] args) {a obj1 = new a (); a obj2 = new a (); String S1, S2, S3 = "abc", S4 = "abc"; S1 = New string ("abc"); s2 = new string ("abc"); system.out.println ("S1 = S2?" (S1.Equals (S2))); system.out.println ("S1 = S3? " (S1 == S3)); system.out.println (" S1 = S3? " (S1.Equals (S3))); System.out.Println (" S3 = S4? " (S3 == S4)); System.out.Println ("S2 = S3?" (S2.Equals (S3))); System.out.Println ("S2 = S4?" (S2.Equals (S4)) ); System.out.println ("obj1 = obj2?" )); System.out.println ("S1 = S2?" (S1 == S2)); OBJ1 = Obj2; System.out.println ("obj1 = Obj2 after obj1 = obj2?" (Obj1.equals (obj2))); system.out.println ("obj1 = Obj2 after obj1 = Obj2?" (Obj1 == Obj2) }}

result:

S1 = S2? Trues1 = S3? falses1 = S3? Trues3 = S4? Trues2 = S3? Trues2 = S4? trueobj1 = OBJ2? falses1 = s2? falseobj1 = obj2 after obj1 = Obj2? TrueObj1 = Obj2After Obj1 = Obj2? True

problem:

The book is written to the object of the same class, == and Equals method return value is false. So is S3.S4 of the same type of object? If so, then the gray identity is not right. If not that is it? Answer: Remember a sentence: The reference is always placed on the stack, and the object is always allocated on the heap. The difference between == and Equals is actually in-depth to technical details is the judgment of stack addresses and stack addresses. Learn Java, you have to figure out what is a simple data type and what is an object. Remember, String in Java is a class. == and Equals Difference: == Used to determine whether the reference address is the same, that is, if the reference (pointer) is to point to the object in the same heap, equals is the same as the data within the object, not the address (not the pointer pointing " the address of). System.out.println ("S1 = S2?" (S1.Equals (S2))); this is clear, the content is true system.out.println ("S1 = S3?" (S1 == S3 )); The address is different, for false system.out.println ("S1 = S3?" (S1.Equals (S3))); content is true system.out.println ("S3 = S4?" S3 == S4))); there is still some problems here, the big brother tells me. System.out.Println ("S2 = S3?")); The content is the same as true system.out.println ("S2 = S4?" (S2.Equals (S4))); The same content is true system.out.println ("obj1 = Obj2?")); Obj1 reference points indicated by NEW and does not assign a value after New, so it is false, ie the content in the heap is unknown. For False System.Println ("S1 = S2?" (S1 == S2)); point to the stack address, for false obj1 = obj2; is reference assignment, that is, the pointer is assigns system.out.Println (" Obj1 = obj2 after obj1 = obj2? " (obj1.equals (obj2))); system.out.println (" obj1 = Obj2 after obj1 = Obj2? " (Obj1 == Obj2)); the above is all all True, since the reference assignment, the reference pointer content is the same. So True. Java is an object-oriented thing, and the reason why the stack is in order to consider speed, although flexible in the heap, but the efficiency is indeed a big problem, the compiler has to dynamically perform the memory size configuration, and though can't be in the stack Dynamically configured, but it is very fast. Each has gone. Finally, Thank you ghyghost such a detailed answer green marked, why is the last for false, and the following S3 = S4, is it true? Solution: The green labeled S1 and S3 are different. S1 is a string after the instance is the relationship of NEW. Opened new memory space. And S3 is not. So not directly ==.

转载请注明原文地址:https://www.9cbs.com/read-120389.html

New Post(0)