🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-and-equals-method-in-java
Difference Between == Operator and equals() Method in Java - GeeksforGeeks
January 4, 2025 - The == operator checks reference equality for objects and value equality for primitives. ... // == operator for compatible data types class Geeks { public static void main(String[] args) { // integer-type System.out.println(10 == 20); // char-type ...
Discussions

Making Strings not Equal a user input.
You seem to try to compare String values with == or !=. This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase(). See Help on how to compare String values in the r/javahelp wiki. Your post is still visible. There is no action you need to take. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
8
4
February 16, 2023
Java string equals string in if statement
Nor can you extend String, because ... you're not into inheritance that's ok for now). ... Then s and t actually refer to the same String object, and ‘s == t’ will return true, because for objects the double equal sign ("==") reads as "is the same object". ... Then s!=t (because you've explicitly created a new string) although s.equals(t) will return true (because the equals method checks for value equality instead of reference equality). There is a "String constant pool" in the Java ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
April 2, 2019
Why to never use == with Strings and instead use .equals()

Holy cow, this is so awesome. Thank you for this reply, that makes so much sense and is so cool!

More on reddit.com
🌐 r/learnjava
38
75
August 8, 2020
Java. Is there a String that is not equal to itself?

This may crash or may not, depending on how the constructor of B is defined. Maybe the constructor of B sets name to a random word every time the constructor is called or maybe it's always the same.

More on reddit.com
🌐 r/learnprogramming
4
0
September 16, 2012
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Expressions_and_operators
Expressions and operators - JavaScript | MDN
Strings are compared based on standard lexicographical ordering, using Unicode values. In most cases, if the two operands are not of the same type, JavaScript attempts to convert them to an appropriate type for the comparison. This behavior generally results in comparing the operands numerically. The sole exceptions to type conversion within comparisons involve the === and !== operators, which perform strict equality and inequality comparisons.
🌐
Quora
quora.com › How-do-you-check-if-a-string-is-not-equal-to-another-string-in-Java
How to check if a string is not equal to another string in Java - Quora
Answer (1 of 4): Below is a sample code to check for not equal to: String s1 = "hello"; String s2 = "Hello"; if (s1!= null && s2!=null) { if (!s1.equals(s2)) { System.out.println("S1 And S2 Are Not Equal"); } } Please note that the equals() method is case sensitive i.e. here it will consi...
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
public class Main { public static void main(String[] arg) { String str1 = new String("java"); String str2 = new String("java"); System.out.println(str1 == str2); } } ... Here, the two strings have equal values but these values are not stored ...
🌐
Reddit
reddit.com › r/learnjava › making strings not equal a user input.
r/learnjava on Reddit: Making Strings not Equal a user input.
February 16, 2023 -

I’m writing code for a rock, paper, scissors game and I need to check the users input to make sure they input Rock, Paper, or Scissors. I know that you can use “!=“ to compare two strings and “.equals” in an if-else statement. For the most part I have the entries, as well as the comparing the two entries to see who wins, however, I’m having trouble checking to make sure their input is correct to proceed on, I’ve tried “!=“ but it automatically will default to “Wrong Choice!” (as I have that as what will output if their entry isn’t either of the options) and I can’t seem to figure it out. Any ideas on how I would do that?

Here is the code I have for it so far,

import java.util.Scanner;

public class Assignment2 { public static void main (String[]args) { String player1; String player2;

	Scanner keyboard = new Scanner(System.in);
	
	System.out.println("*****Hello! Welcome to the Rock, Paper, Scissors Program*****");
	System.out.println("Rules: This is a two player game, both players must take turns entering their choice");
	System.out.println("\tRemember, rock beats scissors, scissors beats paper, and paper beats rock!");
	
	System.out.println("Player 1: Choose rock, paper, or scissors: ");
	player1= keyboard.nextLine();
	
	if ((player1 != "Rock") || (player1 != "Paper") || (player1 != "Scissors" )) 
	{
		System.out.println("Wrong Choice!");
		player1 = keyboard.nextLine();
	}
	else 
	{
		player2 = keyboard.nextLine();
		
	}
	
	System.out.println("Player 2: Choose rock, paper, or scissors: ");
	player2=keyboard.nextLine();
	
	
	
	if (player1.equalsIgnoreCase("Rock") && player2.equalsIgnoreCase("Scissors"))
	{
		
		System.out.println("Player 1 wins!");
		
	} 
		else if (player1.equalsIgnoreCase("Scissors")&&player2.equalsIgnoreCase("Paper"))
		{
		
			System.out.println("Player 1 wins!");
		
		} 
		else if (player1.equalsIgnoreCase("Paper")&&player2.equalsIgnoreCase("Rock")) 
		{
		
			System.out.println("Player 1 wins!");
		
		} 
		else if (player1.equalsIgnoreCase(player2)) 
		{
		
			System.out.println("It's a tie!");
		
		}
	else 
	{
			System.out.println("Player 2 wins!");
		
	}
	
	
	
	keyboard.close();
	
	
	
}

}

Find elsewhere
🌐
CodeChef
codechef.com › learn › course › java › JAVANEW16 › problems › WNDHVZ16
Comparison with Not Equal (!=) in Java Conditionals in Java
Test your Learn Java knowledge with our Comparison with Not Equal (!=) in Java Conditionals practice problem. Dive into the world of java challenges at CodeChef.
🌐
Blogger
javahungry.blogspot.com › 2021 › 03 › not-equal-example-opposite-of-equals.html
not equal example : (opposite of .equals java) | Java Hungry
To do the opposite just put an exclamation mark at the start of the statement, for example !str1.equals(str2) String str1 = "Java"; String str2 = "Hungry"; boolean notEqual = !str1.equals(str2); System.out.println( notEqual ); //true Given below is the example to compare objects ·
🌐
Coderanch
coderanch.com › t › 394611 › java › strings-equal
Why are same strings not equal (Beginning Java forum at Coderanch)
To understand this, you need to know two things: First, String literals are shard in the JVM, Every instance of "String" represents the same physical object; so "String" == "String" is true, while "String" == new ("String") is false. Second, the guys who wrote the Java APIs are smart enough ...
🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
Java Examples Java Videos Java ... equals() method compares two strings, and returns true if the strings are equal, and false if not....
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - Constructs a new String by decoding the specified array of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
🌐
Medium
medium.com › @thilinajayawardana_85346 › java-string-nullpointerexception-safe-equals-check-404481934e9b
Java String NullPointerException safe equals check | by Thilina Jayawardana | Medium
June 30, 2020 - public class DemoApplication { final static String theStringIknow = "Hello"; public static void myTestMethod(String someString) { //do not do this if (someString.equals(theStringIknow)) { System.out.println("Same same"); } } public static void main(String[] args) { String testString = null; myTestMethod(testString); } }Exception in thread "main" java.lang.NullPointerException at com.example.demo.DemoApplication.myTestMethod(DemoApplication.java:7) at...
🌐
Team Treehouse
teamtreehouse.com › community › java-string-equals-string-in-if-statement
Java string equals string in if statement (Example) | Treehouse Community
April 2, 2019 - Nor can you extend String, because it's declared as final, meaning no sub-classing is allowed (if you're not into inheritance that's ok for now). ... Then s and t actually refer to the same String object, and ‘s == t’ will return true, because for objects the double equal sign ("==") reads as "is the same object". ... Then s!=t (because you've explicitly created a new string) although s.equals(t) will return true (because the equals method checks for value equality instead of reference equality). There is a "String constant pool" in the Java language.
🌐
Coderanch
coderanch.com › t › 661872 › java › Comparing-String-Equal
Comparing a String to NOT Equal (Beginning Java forum at Coderanch)
February 9, 2016 - All things are lawful, but not all things are profitable. ... Here's how I would use charAt and indexO, using String instead of array of String for the vowels variable:
🌐
W3Schools
w3schools.com › java › java_conditions.asp
Java If ... Else
Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings
🌐
LeetCode
leetcode.com › problems › backspace-string-compare
Backspace String Compare - LeetCode
Can you solve this real interview question? Backspace String Compare - Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. Note that after backspacing an empty text, the text will continue empty.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java not equal Example - Java Code Geeks
August 10, 2021 - The type of each operand that we use can be anything, for example, String, integer, double, short etc but when we compare them the type must be the same. The example code in this article was built and run using: ... The main difference between == and equals is that “==” is used to compare primitives while equals() method is recommended to check equality of objects. The goes for not equal. Here we show you some examples about != Java to understand better the use of this operator.
🌐
Medium
medium.com › javarevisited › understanding-javas-operator-equals-method-and-compareto-method-through-real-life-9a636e925e4f
Understanding Java's == Operator, equals() Method, and ...
May 13, 2025 - After some deep investigation, I realized the culprit was a simple mix-up between Java’s == operator and the equals() method.
🌐
Opensource.com
opensource.com › article › 19 › 9 › compare-strings-java
How to compare strings in Java | Opensource.com
September 20, 2019 - As a common practice, use String equals for case-sensitive strings and String equalsIgnoreCase for case-insensitive comparisons. However, one caveat: take care of NPE (NullPointerException) if one or both strings are null. The source code is available on GitLab and GitHub. ... Constructors are powerful components of programming. Use them to unlock the full potential of Java.