Java 'if' Statements: Solved & Explained

  • Java
  • Thread starter Apple&Orange
  • Start date
  • Tags
    Java
In summary, The conversation discusses the technical details of using == versus String.equals in Java and the benefits of using interned strings. It is recommended to always use String.equals for comparing strings, even if they are interned, for efficient and accurate results.
  • #1
Apple&Orange
30
2
TL;DR Summary
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
  • #2
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
 
  • #3
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).
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
2K
Replies
1
Views
6K
Replies
2
Views
1K
Replies
2
Views
2K
Replies
2
Views
12K
Replies
1
Views
6K
Back
Top