Java Java 'if' Statements: Solved & Explained

  • Thread starter Thread starter Apple&Orange
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
Using the `==` operator for string comparison in Java leads to unexpected results because it checks for reference equality, meaning it verifies if both references point to the same memory location. In contrast, the `String.equals()` method checks for value equality, comparing the actual content of the strings. This distinction is crucial, as a new string object will not reference the same memory as a string literal like "exit," resulting in a false comparison when using `==`. Additionally, Java's string interning can optimize memory and comparison efficiency by ensuring that identical string literals share the same instance. While using interned strings can improve performance, it is considered best practice to always use `String.equals()` for string comparisons. This method is effective for both interned and non-interned strings, ensuring accurate results regardless of how the strings are created.
Apple&Orange
Messages
28
Reaction score
2
TL;DR
Strings used in if statements
Edit: Never mind guys, figured it out. I needed to use s.equals :)
However, could someone explain to me why we can't use == on a technical level?
I'm curious to know on a technical level.

Hi guys

Doing a bit of programming, and I'm not too sure why I'm not getting the expected results.
Below is the code, and whenever I type in exit, it always asks me to "Enter the number of points" instead of printing "Terminate".

I guess I am missing something but not too sure what.

import java.util.Scanner;
public class Uppgift_6_my {

public static void main(String[] args) {

System.out.println("Enter the number of points: ");
Scanner testresultinput = new Scanner (System.in);
String testresult = testresultinput.next();

if (testresult == "exit") {
System.out.println("Terminate");
}

else {
System.out.println("Enter the number of points: ");
}
}
}
 
Last edited:
Technology news on Phys.org
Java has no operator overloading so the String class can't implement ==. == used on objects in Java returns true if the two objects refer to the same memory. Your new object, no matter what the value is, will never have the same location as a constant. Therefore your comparison to "exit" will always be false.

BoB
 
A small comment that relates to String.equals.

While it is always semantically correct to use String.equals it is also beneficial to know about how Java interns string literals into canonical String instances and how you as a programmer can tap into this string interning by using the String.intern method.

Using interned strings can (in some situations) increase memory efficiency (as identical strings share a single instance) and comparison efficiency (as equals takes a quick short cut if the two instances are the same). One such case could be in a syntax tree where the node ID node for a variable is the interned variable name. Even if this variable name is repeated thousands of time in the tree the string instance will only be there once.

Note, that even if you know that all strings you want to compare are interned either implicitly by being a string literal or explicitly by the program, thus making comparison by the == operator semantically equal to comparing content, it is always best practice to still use String.equals to compare strings as this also works for non-interned strings and is still very efficient for interned string since equal strings share same instance and non-equal string (most likely) have different hash codes which is both utilized in the String.equals implemenation (a pattern followed by most equals methods implementations).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 1 ·
Replies
1
Views
6K