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

  • SEARCH
  • Home
  • 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 9004305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:52:06+00:00 2026-06-16T00:52:06+00:00

Possible Duplicate: Java String.equals versus == I’m trying to write a method in az

  • 0

Possible Duplicate:
Java String.equals versus ==

I’m trying to write a method in az DBOpenHelper extends SQLOpenHelper class.
It supposed to evaluate if there’s an entry in the DB with the same name.

public boolean existsContact(Contact contact) {

    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {

            String name = cursor.getString(1);
            String cname = contact.getName();
            if (name == cname) {

                cursor.close();
                db.close();
                return true;
            }

        } while (cursor.moveToNext());
    }

    db.close();

    return false;
}

Here’s the relevant part of Contact class:

 public class Contact {

    String _name;
    public String getName(){
        return this._name;
    }
    }

Now here’s the strange thing:

Scenario A : if (name == cname) where name = "foo" and cname = "foo" equals false.
Eclipse debugger show name’s foo and cname’s foo have different id’s.
both variables filled as seen before in code.

Scenario B: if(name == cname) where variabales are loaded like this:

String name = "foo";
String cname = "foo";
         statement equals true as it's supposed to.

Scenario C: if("foo" == "foo") equals true…BUT…debugger goes out the window. LogCat show debugger connected, but there’s no activity in eclipse’s Debug perspective. Breakpoints have no effect. No Threads shown.

  • 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-06-16T00:52:07+00:00Added an answer on June 16, 2026 at 12:52 am

    In java, when using == on two objects, you’re not actually comparing the strings themselves. You’ll need to use .equals(String).

    == actually compares the two object’s references, not their values.

    string1.equals(String target) compares the two strings based off of the actual characters in the strings.

    See: http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html

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

Sidebar

Related Questions

Possible Duplicate: Java String.equals versus == I am writing a program to simulate the
Possible Duplicate: Java String.equals versus == I am using jcreator to practice java language.
Possible Duplicate: How do I compare strings in Java? Java String.equals versus == I
Possible Duplicate: Java String.equals versus == I thought this would be a neat way
Possible Duplicate: Java String.equals versus == I am using an if statement in java
Possible Duplicate: Calling a method named “string” at runtime in Java and C I
Possible Duplicate: Difference Between Equals and == String comparison and String interning in Java
Possible Duplicates: Java String.equals versus == whats the difference between ".equals and ==" public
Possible Duplicate: String comparison in Java heres my script: public class euler4 { /**
Possible Duplicate: Java string comparison? I was trying to do this: boolean exit =

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.