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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:42:33+00:00 2026-06-12T09:42:33+00:00

Possible Duplicate: How do I compare strings in Java? i have written some code

  • 0

Possible Duplicate:
How do I compare strings in Java?

i have written some code which compares two strings “abc” and “de”. The string abc is parsed and returned to “doc” to ext and then it is compared. Although it seems that if condition is true but still the else part is executing. where i am not getting plz help me ….thanks a lot.

public class xyz{

    String abc="doc2.doc";
    String de="doc";

    public static void main(String arg[]){

    xyz c=new xyz();

    String ext = null;
    String s =c.abc;
        String d =c.de;
    int i = s.lastIndexOf('.');

    if (i > 0 && i < s.length() - 1){
    ext = s.substring(i+1).toLowerCase();
    }
    System.out.println(ext);
    if(ext==d){

    System.out.println("true");
    }
    else{
    System.out.println("false");
    }


    }


    }
  • 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-12T09:42:34+00:00Added an answer on June 12, 2026 at 9:42 am

    You cannot compare strings with == as they are different objects.
    The contents may be the same, but that is not what == looks at.
    Use the equals method on one of the strings to compare it to the other string.

    In your code, use:

    if(d.equals(ext)){
    

    If you ever need to compare two strings, and only one of them is a variable, I suggest the following approach:

    "foo".equals(variableString)
    

    This way you can be sure that you will not be calling equals on a Null object.

    Another possibility is to use the compareTo method instead of the equals method which is also found in some other Java classes. The compareTo method returns 0 when the strings match.

    You can find more information about strings in Java here.

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

Sidebar

Related Questions

Possible Duplicate: How do I compare strings in Java? I have written this code:
Possible Duplicate: A better way to compare Strings which could be null I have
Possible Duplicate: How do I compare strings in Java? String s1 = andrei; String
Possible Duplicate: How do you compare two version Strings in Java? I've 2 strings
Possible Duplicate: How to compare two arraylist? I have two String ArrayLists of different
Possible Duplicate: How do I compare strings in Java? I read some records from
Possible Duplicate: Java Strings and StringPool I created two Strings String s1=MyString; String s2=new
Possible Duplicate: How do I compare strings in Java? Java String.equals versus == I
Possible Duplicate: How do I compare strings in Java? i have 2 class, let
Possible Duplicate: How to compare two hashmaps in java? Hi i have two different

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.