[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes

When storing a character string value in a Sting type variable, the judgment result differs between String.equals and==due to the difference in writing style. Note so don't forget.

--How to write using new

How to write using new

In general, the same writing method as when creating an object

    String str = new String("String");

This will compare the variables that store the values. (Is this the right way to use the word "store"?)


    //Create object
    String cat1 = new String("Marble");
    String cat2 = new String("Marble");

    //Equivalence judgment and equal value judgment
    System.out.println(cat1 == cat2);

Output result.


Easy writing

The same writing style as when storing a value in the basic data type

    String str = "String";

This will compare the variables.


    //Store value in variable
    String cat3 = "Mike";
    String cat4 = "Mike";

    //Equivalence judgment and equal value judgment
    System.out.println(cat3 == cat4);

Output result.


After all, if you use the == operator to compare strings, the judgment result will be different, so be careful.

Recommended Posts

[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
About the relationship between the Java String equality operator (==) and initialization. Beginners
About the difference between classes and instances in Ruby
About the difference between irb and pry
A note on the differences between interfaces and abstract classes in Java
[Java] Understand the difference between List and Set
[Rails / ActiveRecord] About the difference between create and create!
Understand the difference between abstract classes and interfaces!
[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] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
[Java] Difference between == and equals
[Java] About String and StringBuilder
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] Check the difference between orElse and orElseGet with IntStream
Think about the differences between functions and methods (in Java)
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
[Java] Difference between Hashmap and HashTable
About the difference between gets and gets.chomp (other than line breaks)
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
[Java] Judgment of identity and equivalence
[Java] Difference between array and ArrayList
[Java] What is the difference between form, entity and dto? [Bean]
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Understand the difference between int and Integer and BigInteger in java and float and double
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[For beginners] Difference between Java and Kotlin
[Note] Cooperation between Java and DB (basic)
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Java] Difference between equals and == in a character string that is a reference type
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[iOS] Understand the difference between frame and bounds
About the idea of anonymous classes in Java
Difference between next () and nextLine () in Java Scanner
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Summarize the differences between C # and Java writing
A note about the Rails and Vue process
[Java] Difference between "final variable" and "immutable object"
[Java] The difference between the Stream.of () and Arrays.stream () methods that you do not know unexpectedly
What is the difference between a class and a struct? ?? ??
Please note the division (division) of java kotlin Int and Int
What is the difference between System Spec and Feature Spec?
About the relationship between HTTP methods, actions and CRUD
[Java] Difference between static final and final in member variables
Compare the difference between dockerfile before and after docker-slim
[Java] About Objects.equals () and Review of String comparisons (== and equals)
About Class loading and initialization when the JVM starts
Java classes and instances to understand in the figure
What is the difference between skip and pending? [RSpec]
[Rails] I investigated the difference between redirect_to and render.
About Java setters and getters. <Difference from object orientation>