[Java] Difference between equals and == in a character string that is a reference type

Reference location comparison and value comparison

Comparison method Contents
equals Memory area comparison
== Value comparison

Program example

public class MyApp {
		
	public static void main(String[] args) {

		String s1 = "ab"; //Store memory area
		String s2 = "ab"; //Store memory area (same area as above)

		if(s1.equals(s2)) { //Compare if the contents are the same
			System.out.println("same1 equals");
		}

		if(s1 == s2) { //Comparison of basic data types
			System.out.println("same2 ==");
		}
		//Generate string object (with equals method)
		String ss1 = new String("ab"); //Storage of memory area
		String ss2 = new String("ab"); //Storage of memory area (create another area from the above)

		//Comparison of basic data types
		if(ss1 == ss2) { //Since the memory area is being compared, it will be false and will not be executed.
			System.out.println("same3 ==object");
		}
		//Comparison of data contents
		if(ss1.equals(ss2)) {
			System.out.println("same4 equals object"); //Executed because the memory area is different but the value is the same
		}
	}
}

Execution example

same1 equals
same2 ==
same4 equals object

The character string has a habit

In the character string that is originally a reference type, the location of the memory area of the reference destination is stored in the variable.

And, in the example, the same character string  Ľab` is assigned to s1 and s2, but the location of the memory area is entered in s1 and the same character string is assigned to s2, so the same memory It seems that the location of the area will be included. In other words

String s1 = "ab";
String s2 = s1;

Same meaning as? become.

When I try it

same1 equals
same2 ==
same4 equals object

What happens if the reference location is the same and the referenced value is returned in either case? Does changing only the value of s1 change both?

String s1 = "ab";
String s2 = s1;
s2 = "cd";

What happens if you do ... Easy to understand

		System.out.println("s1: " + s1);
		System.out.println("s2: " + s2);

And run.

s1: ab
s2: cd
same4 equals object

Only s2 has been changed and is not reflected in s1.

Speaking of which, character strings behave in the same way as basic data types, and they behave in the same way as int types. .. ..

Does it mean that a new memory area is created when the value is updated with the same reference destination while the value is the same?

It's complicated, but I tried it and understood it again.

If it is an array, both values will be updated. Review Review!

Recommended Posts

[Java] Difference between equals and == in a character string that is a reference type
[Java] Difference between == and equals
Difference between primitive type and reference type
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Is short-circuit evaluation really fast? Difference between && and & in Java
Difference between final and Immutable in Java
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==
Difference between int and Integer in Java
Difference between next () and nextLine () in Java Scanner
What is the difference between a class and a struct? ?? ??
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] How to convert a character string from String type to byte type
Jersey --What is Difference Between bind and bindAsContract in HK2?
Difference between element 0, null and empty string (check in list)
I tried to convert a string to a LocalDate type in Java
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[JAVA] Difference between abstract and interface
What is a reference type variable?
[Java] Difference between array and ArrayList
Understanding equals and hashCode in Java
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
What is the difference between a web server and an application server?
[Java] What is the difference between form, entity and dto? [Bean]
[Java] Divide a character string by a specified character
How to test a private method in Java and partially mock that method
Regarding String type equivalence comparison in Java
[Easy-to-understand explanation! ] Reference type type conversion in Java
A note on the differences between interfaces and abstract classes in Java
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Difference between getText () and getAttribute () in Selenium
Split a string with ". (Dot)" in Java
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Java] Comparison of String type character strings
Arrylist and linked list difference in java
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Review of "strange Java" and Java knowledge that is often forgotten in Java Bronze
[Java] Difference between Intstream range and rangeClosed
Understand the difference between int and Integer and BigInteger in java and float and double
[Java] Eclipse shortcuts that make sense when you go back and forth between source code in a project
[Java] Understand the difference between List and Set
Read a string in a PDF file with Java
The intersection type introduced in Java 10 is amazing (?)
Code to escape a JSON string in Java
A bat file that uses Java in windows
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Distinguish between positive and negative numbers in Java
[Java] Difference between "final variable" and "immutable object"
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Java] Incompatible types error occurred when assigning a character string to a char type variable
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?