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 8090725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:49:09+00:00 2026-06-05T19:49:09+00:00

Possible Duplicate: Java String.equals versus == I know it’ a dumb question but why

  • 0

Possible Duplicate:
Java String.equals versus ==

I know it’ a dumb question but why this code doesn’t work.

boolean correct = "SampleText"  == ((EditText)findViewById(R.id.editText1)).getText().toString();
    if(correct) ((TextView)findViewById(R.id.textView1)).setText("correct!");
    else ((TextView)findViewById(R.id.textView1)).setText("uncorrect!");  

The point is to check if content of “editText1” is equal to “Sample Text”

  • 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-05T19:49:10+00:00Added an answer on June 5, 2026 at 7:49 pm

    In Java, two strings (and in general, two objects) must be compared using equals(), not ==. The == operator tests for identity (meaning: testing if two objects are exactly the same in memory), whereas the method equals() tests two objects for equality (meaning: testing if two objects have the same value), no matter if they’re two different objects. Almost always you’re interested in equality, not in identity.

    To fix your code, do this:

    String str = ((EditText)findViewById(R.id.editText1)).getText().toString();
    boolean correct = "SampleText".equals(str);
    

    Also notice that it’s a good practice to put the string literal first in the call to equals(), in this way you’re safe in case the second string is null, avoiding a possible NullPointerException.

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

Sidebar

Related Questions

Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: Java Calendar Setting Incorrectly i have this code: GregorianCalendar cal = (GregorianCalendar)
Possible Duplicate: Java GUI repaint() problem? I write a Java code, but I have
Possible Duplicate: Java - escape string to prevent SQL injection I know php has
Possible Duplicate: Java: convert List<String> to a join()d string Having this: List<String> elementNames =
Possible Duplicate: Java isEmpty() undefined for String? I copied my code from one java
Possible Duplicate: how to convert java string to Date object In my Java code
Possible Duplicates: Java String.equals versus == whats the difference between ".equals and ==" public
Possible Duplicate: Java string comparison? I was trying to do this: boolean exit =
Possible Duplicate: Convert String to code in Java Dynamic code execution on Java I

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.