About the relationship between the Java String equality operator (==) and initialization. Beginners

For Java beginners, instead of looking at the meaning of the equality operator (==) for String, "whether they are the same value" I think one of the points of learning is to see "whether the references are the same".


public static void main(String[] args) {
	String str1 = "100";// ⓵
	String str2 = new String("100"); // ⓶
		 System.out.println(str1 == str2); //Become False.

I understand that it will be false at this time. But String can be initialized with either ⓵ or ⓶. When will the references be the same? For example:


public static void main(String[] args) {
	String str1 = "100";
	String str2 = "100";
		 System.out.println(str1 == str2); //Become True.

In the above example, it will be True. Well, I thought that it would be false because I made two different reference type variables. It seems that str1 and str2 are looking at the same reference at this time because the equality operator is looking at "whether the reference is the same".

I was wondering what this meant. After going around, the following came out. Reference: Learn the impact on performance when using String # intern ()

The point is, in String, if there is an Intern () method, and if you use that, there is a String object with the same string, It seems that all the references are available. This will reduce wasted memory. It is inefficient to create objects one by one even though they have the same value.

Isn't this happening in Test2.java? That is, the Intern () method is automatically applied when initializing. That way, you won't waste memory. What happens in the following cases?


 public static void main(String[] args) {
	String str1 = new String("100");
	String str2 = new String("100");
		 System.out.println(str1 == str2);

In this case it seems to be false. The thing is that when initializing with new ~~, the Intern () method is not applied and it is just called new, so it seems that you are creating a new object.

Summary, -If str1 = "100" and str2 = "100", the reference destination will be the same. -When initializing with new String ("100"), a new object is created, so the reference destination is different.

By the way, I also executed the following to find out whether this event occurs in the reference type or whether the String is special.


 public static void main(String[] args) {
	Integer int1 = 100;
	Integer int2 = 100;
		 System.out.println(str1 == str2);

It was True. In the case of reference type, unless you write new, you can say that you are basically looking at the same reference point.

Recommended Posts

About the relationship between the Java String equality operator (==) and initialization. Beginners
About the relationship between HTTP methods, actions and CRUD
[Java] About String and StringBuilder
Think about the differences between functions and methods (in Java)
The relationship between strict Java date checking and daylight savings time
Relationship between kotlin and java access modifiers
[For beginners] Difference between Java and Kotlin
About the difference between irb and pry
Interpret the relationship between Java methods and arguments into a biochemical formula
[Java] Understand the difference between List and Set
[Rails / ActiveRecord] About the difference between create and create!
[Java] The confusing part of String and StringBuilder
Summarize the differences between C # and Java writing
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
[Java] String comparison and && and ||
About the difference between classes and instances in Ruby
About the ternary operator
[Java Silver] About initialization
[JAVA] What is the difference between interface and abstract? ?? ??
[Java] About Objects.equals () and Review of String comparisons (== and equals)
About Java String class
About Class loading and initialization when the JVM starts
Verification of the relationship between Docker images and containers
About the relationship between Rails credentials.yml.enc and master.key (undefined method `[]'for nil: NilClass (NoMethodError))
About the [ruby] operator
What is the difference between Java EE and Jakarta EE?
About the difference between "(double quotation)" and "single quotation" in Ruby
Java beginners briefly summarized the behavior of Array and ArrayList
[For beginners] About the JavaScript syntax explained by Java Gold
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] Check the difference between orElse and orElseGet with IntStream
[Java] I thought about the merits and uses of "interface"
Study Java # 2 (\ mark and operator)
[Java] Difference between == and equals
Relationship between Controller and View
About the same and equivalent
Relationship between package and class
About Java Packages and imports
About the difference between gets and gets.chomp (other than line breaks)
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
I tried to summarize the methods of Java String and StringBuilder
[Java] What is the difference between form, entity and dto? [Bean]
[Java] How to convert from String to Path type and get the path
About Java static and non-static methods
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
Relationship between ActiveRecord with_lock and cache
Java variable declaration, initialization, and types
About fastqc of Biocontainers and Java
Java for beginners, expressions and operators 1
About the equals () and hashcode () methods
Difference between == operator and eqals method
[Java beginner] About abstraction and interface
Understand the difference between each_with_index and each.with_index
Java for beginners, expressions and operators 2
[Java beginner] == operator and equals method
About the current development environment (Java 8)
Relationship between database and model (basic)
[JAVA] Difference between abstract and interface