2019.9.17java in == on a string

Direct Copy pictures well, ha ha

Inside the original picture is educoder

 

 Results

 

 Because the ==comparison of two objects of memory addresses, we know that when the variable is created the equivalent of open space in memory, while the case strand str1is occupied by two different spaces, so their memory addresses are inconsistent, in use ==, when the determination is not equal to the symbol, ie false.

So remember the address for the object reference is == is more quoted, citing both the value of

Now look at an example

 

 Results

 

 This is what causes it? Because definitions String str = "hello"and String str1 = "hello"create no object, but at the same time pointing to an object, so they are the same memory address.

 

 

The question is, since it ==can not, then what should we use it?

Java determines whether the two strings are equal, use the equals method;

Syntax: 字符串1.equals(字符串2)If 字符串1equal 字符串2returns true, otherwise return false;
for example:

Output: str与str1是否相等true

 

Guess you like

Origin www.cnblogs.com/WildSky/p/11535159.html