Java 'if' statements

  • Java
  • Thread starter Apple&Orange
  • Start date
  • #1

Summary:

Strings used in if statements

Main Question or Discussion Point

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:

Answers and Replies

  • #2
rbelli1
Gold Member
908
328
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
Filip Larsen
Gold Member
1,235
168
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).
 

Related Threads for: Java 'if' statements

Replies
8
Views
1K
  • Last Post
Replies
15
Views
4K
Replies
11
Views
3K
Replies
4
Views
12K
Replies
11
Views
2K
Replies
5
Views
1K
Replies
9
Views
935
Replies
19
Views
1K
Top