🌐
Coderanch
coderanch.com › t › 661872 › java › Comparing-String-Equal
Comparing a String to NOT Equal (Beginning Java forum at Coderanch)
I thought that if I had an exclamation point it would read as "else if not character.equals to vowels " I know there are other ways to achieve the goal of this lesson and I want to explore those options as well but I want to learn what I'm doing wrong here first.
🌐
Blogger
javahungry.blogspot.com › 2021 › 03 › not-equal-example-opposite-of-equals.html
not equal example : (opposite of .equals java) | Java Hungry
public class ObjectNotEqualExample { public static void main(String args[]) { Student s1 = new Student("John", 123, "Male"); Student s2 = new Student("Alexa", 234, "Female"); Student s3 = s1; System.out.println(!s1.equals(s2));// true System.out.println(!s1.equals(s3));// false } } class Student ...
🌐
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();
	
	
	
}

}

🌐
Javaprogramto
javaprogramto.com › 2020 › 11 › string-does-not-equal-java.html
Java - String not equals Examples JavaProgramTo.com
A quick guide to compare strings using != and equals() method in java. Examples on string != java and string.equals("java").
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java not equal Example - Java Code Geeks
August 10, 2021 - Here you can see an example with an override method. Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. This new method has the same name, same parameters or signature and same return type as a method in its super-class. ... public class Not_equal_override { static class Car { private static String name ; private int cc; private int speed; public Car(String name, int cc, int speed) { this.name = name; this.cc = cc; this.speed = speed; } @Override public boolean eq
🌐
Sololearn
sololearn.com › en › Discuss › 1953992 › can-someone-tell-me-why-these-two-strings-are-not-equal
Can someone tell me why these two strings are not equal? | Sololearn: Learn to code for FREE!
David Crowley Thanks buddy 😊, also good thing to know if your learning, is that objects types like String are treated differently then primitive types like int,double, char ect.. for example int x = 10; int y = 20; As these are not objects you can compare using x==y because primitive type dont have refrenece like objects do, they store the actual value so behind the scenes it looks like 10==20 👍
🌐
Coderanch
coderanch.com › t › 394611 › java › strings-equal
Why are same strings not equal (Beginning Java forum at Coderanch)
Originally posted by Chandra Bairi: public class ADirtyOne { public static void main(String[] a) { if("String".replace('g','G') == "String".replace('g','G')) System.out.println("Equal"); else System.out.println("Not Equal"); } } the o/p to the above is NotEqual why is this not equal
Find elsewhere
🌐
iO Flood
ioflood.com › blog › java-notequals-operator
Java '!=' Operator: Use Cases for 'Not Equals' Comparisons
February 29, 2024 - Here’s an example: String str = null; if (str != null) { System.out.println(str.length()); } else { System.out.println("str is null"); } # Output: # 'str is null' In this case, str is null, so str != null returns false, and the message ‘str ...
🌐
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() ...
🌐
Quora
quora.com › How-do-you-write-the-not-equals-sign-on-Java
How to write the not equals sign on Java - Quora
The method returns true if the strings compared are equal and false otherwise. To negate the result, just put an exclamationmark in front of the statement. String str1 = "hi"; String str...
Top answer
1 of 9
92

I guess it's just consistency, or "principle of least astonishment". String is an object, so it would be surprising if was treated differently than other objects.

At the time when Java came out (~1995), merely having something like String was total luxury to most programmers who were accustomed to representing strings as null-terminated arrays. String's behavior is now what it was back then, and that's good; subtly changing the behavior later on could have surprising, undesired effects in working programs.

As a side note, you could use String.intern() to get a canonical (interned) representation of the string, after which comparisons could be made with ==. Interning takes some time, but after that, comparisons will be really fast.

Addition: unlike some answers suggest, it's not about supporting operator overloading. The + operator (concatenation) works on Strings even though Java doesn't support operator overloading; it's simply handled as a special case in the compiler, resolving to StringBuilder.append(). Similarly, == could have been handled as a special case.

Then why astonish with special case + but not with ==? Because, + simply doesn't compile when applied to non-String objects so that's quickly apparent. The different behavior of == would be much less apparent and thus much more astonishing when it hits you.

2 of 9
31

James Gosling, the creator of Java, explained it this way back in July 2000:

I left out operator overloading as a fairly personal choice because I had seen too many people abuse it in C++. I've spent a lot of time in the past five to six years surveying people about operator overloading and it's really fascinating, because you get the community broken into three pieces: Probably about 20 to 30 percent of the population think of operator overloading as the spawn of the devil; somebody has done something with operator overloading that has just really ticked them off, because they've used like + for list insertion and it makes life really, really confusing. A lot of that problem stems from the fact that there are only about half a dozen operators you can sensibly overload, and yet there are thousands or millions of operators that people would like to define -- so you have to pick, and often the choices conflict with your sense of intuition.

🌐
Delft Stack
delftstack.com › home › howto › java › not equals java
Not Equals in Java | Delft Stack
March 11, 2025 - In this example, we use the .equals() method to compare the contents of str1 and str2. Since both strings contain the same text, the output indicates that they are equal in content. This showcases the critical difference between using != and .equals(), emphasizing that != checks for reference equality, while .equals() checks for value equality. Understanding the concept of not equals in Java ...
🌐
Housing Innovations
dev.housing.arizona.edu › home › finces › not equals string in java: a comprehensive guide to string comparison
Not Equals String in Java: A Comprehensive Guide to String Comparison - Housing Innovations
May 20, 2025 - You can use the "!" operator to negate the result of the ".equals()" method and check if strings are not equal. Here are some best practices to keep in mind when comparing strings in Java:
🌐
Programiz
programiz.com › java-programming › examples › compare-strings
Java Program to Compare Strings
public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style.equals(style2)) System.out.println("Equal"); else System.out.println("Not Equal"); } } ... In the above program, we have two strings named style ...
🌐
DEU University
web.deu.edu.tr › doc › oreily › java › langref › ch04_09.htm
[Chapter 4] 4.9 Equality Comparison Operators
string1.equals (string2) // Compares contents of strings string1 == string2 // Compares actual string objects · References Arithmetic Types; Boolean Type; Reference Types · The not-equal-to operator != performs a comparison between its operands and returns a boolean value.
🌐
TutorialKart
tutorialkart.com › java › java-operators › java-not-equal-operator
Java Not Equal (!=) Operator
October 14, 2021 - public class Main { public static void main(String[] args) { int x = 4; int y = 4; if (x != y) { System.out.println("x and y are not equal."); } else { System.out.println("x and y are equal."); } } } Output · x and y are equal.
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - Let’s see an example of this behavior: String string1 = "using comparison operator"; String string2 = "using comparison operator"; String string3 = new String("using comparison operator"); assertThat(string1 == string2).isTrue(); ...
🌐
Coderanch
coderanch.com › t › 603793 › java › strings-equal
Why strings are not equal? (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Please Explain why this program is giving output "Not Equal" Program-: What i know is that the "String".replace('g','G') will return this "StrinG" .
🌐
Reddit
reddit.com › r/learnjava › why to never use == with strings and instead use .equals()
r/learnjava on Reddit: Why to never use == with Strings and instead use .equals()
August 9, 2020 -

In class we were discussing this and basically I understood == tests the EXACT same thing... since Strings are each their own object, we need a special method to check for if they are actually the same words. Furthermore, == tests the address I believe?

Here's the confusing part for me... Java has optimized == so SOMETIMES "hello" == "hello" will return true even if they are two unique Strings... but "hell" += "o" == "hello" returns false. Why ?