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).
 

What is a "Java 'if' statement?"

A Java 'if' statement is a conditional statement that allows a program to make decisions based on certain conditions. It checks if a given condition is true, and if it is, the code within the 'if' statement is executed. If the condition is false, the code within the 'if' statement is skipped and the program moves on to the next section of code.

How do you write a "Java 'if' statement?"

A Java 'if' statement consists of the keyword 'if' followed by parentheses containing the condition to be checked, and curly braces containing the code to be executed if the condition is true. For example:

if (condition) {
    // code to be executed
}

What is the purpose of the "else" statement in a "Java 'if' statement?"

The "else" statement is used to provide an alternate code path to be executed if the condition in the 'if' statement is false. This allows the program to handle both true and false cases of the condition being checked.

Can you have multiple conditions in a "Java 'if' statement?"

Yes, you can have multiple conditions in a Java 'if' statement by using logical operators such as "&&" (AND) and "||" (OR). This allows the code to check for multiple conditions before deciding whether to execute the code within the 'if' statement.

What is a nested "if" statement in Java?

A nested "if" statement in Java is an 'if' statement that is placed inside another 'if' statement. This allows for more complex decision-making and allows the program to check for multiple conditions in a specific order. However, nesting too many 'if' statements can make the code difficult to read and maintain, so it is recommended to use logical operators when possible.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
777
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
Back
Top