Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6348483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:25:42+00:00 2026-05-24T21:25:42+00:00

Possible Duplicates: Java String.equals versus == whats the difference between ".equals and ==" public

  • 0

Possible Duplicates:
Java String.equals versus ==
whats the difference between ".equals and =="

public String getName() {
    return new String("foobar");
}

if(getName() != "foobar2") {
    //Never gets executed, it should, wtf!.
}

if(!getName().equals("foobar2")) {
   //This works how it should.
}

So yeah my question is simple.. why doesn’t != behave the same as !equals() aka (not Equals).

I don’t see any logicial reason why one should fail, both are the same exact code in my mind, WTH.

Looking at java operators
http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

You can clearly see
equality == !=

are the equality operators, sure I usually use != only on numbers.. but my mind started wandering and why doesn’t it work for String?

EDIT:
Here’s something that looks more like the actual issue..

    for (ClassGen cg : client.getClasses().values()) {
        final ConstantPoolGen cp = cg.getConstantPool();
        if(cp.lookupInteger(0x11223344) != -1) {
            for (Method m : cg.getMethods()) {  
                System.out.println("lots of class spam");
                if(m.getName() != "<init>") continue;
                System.out.println("NEVER GETS HERE, 100% SURE IT HAS CONSTRUCTOR LOL");
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:25:42+00:00Added an answer on May 24, 2026 at 9:25 pm

    A string is an Object, not a primitive.
    == and != compare two primitives to each other.
    To compare strings you need to loop trough each character and compare them in order which is what .equals() does.

    If you do any OOP in Java you need to override equals when you want to do equality checks on the Objects, and implement Comparable and .compare() if you want to be able to do things like sort them.

    Here is a quick example of equals:

    public class Person {
        public name;
    
        public Person(String name) {
            this.name = name;
        }
    
        public boolean equals(Object o){
            if(o instanceof Person)
                if(this.name.equals(o.name))
                    return true;
            return false;
        }
    }
    

    Now a Person can be compared to another Person like:

    person1.equals(person2)
    

    Which will only return true if both people have the same name. You can define what makes two objects equal however you want, but objects are only == if they are really just two pointers to the same object in memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: String comparison and String interning in Java What is the difference between
Possible Duplicates: What's the difference between | and || in Java? Difference in &
Possible Duplicate: Java Generics To be more specific, whats the role of the <String>
Possible Duplicates: How do I convert an InputStream to a String in Java? In
Possible Duplicate: What is the purpose of the expression “new String(…)” in Java? It's
Possible Duplicates: How can I Decode string? Java: How to decode HTML character entities
Possible Duplicates: Difference between the use of double quote and quotes in python Single
Possible Duplicates: Using == or Equals for string comparison Are string.Equals() and == operator
Possible Duplicate: Java: Parse a mathematical expression given as a string and return a
Possible Duplicate: What is the purpose of the expression “new String(…)” in Java? What's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.