Results 1 to 6 of 6

Thread: Explain the difference between == & .equals methods in Java?

  1. #1
    Join Date
    Dec 2009
    Posts
    12

    Explain the difference between == & .equals methods in Java?

    I am new learner(beginner) in the Java programming world. I am also learning other programming language like C sharp, C++, .NET and DBMS. Actually now a days, i am studying/working the different methods of strings in Java. I came across a query i.e what is the difference between "= =" and ".equal" method in Java? Please help me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Explain the difference between == & .equals methods in Java?

    I know this method(i.e "= =" and ".equal") strings are very confusing one. I would say that "==" method is swallow comparison. In simple words., it means that they will use the memory location for comparison/comparing instead of using that data. And ".equals()" method is in-depth comparison. In simple words, this method will check the content between two object instead of that the memory location. Hope your confusion will be gone.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Explain the difference between == & .equals methods in Java?

    Both are the method of the string comparison, but the behavior of both of them are completely different from each other. The method "==" will do checking that whether two strings are pointing to same location(memory location) or not. And the method ".equals" will do checking whether that two strings are same(value) or not. Both of them gives output value as true(1) or false(0). Hope i have clear your doubt. All the best.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Explain the difference between == & .equals methods in Java?

    Good question ask by you. I think both the methods are different from eac other in their significance. The ".equals()" method is inside the java.lang.Object class and it check for the equivalence of the state of objects(i.e check value of object).And on the other hand, the '==' operator is expected to check the actual object instances are same or not(i.e check the memory location of objects). Hope your doubt would be clear.

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: Explain the difference between == & .equals methods in Java?

    According to me, the ""==" is used to check the memory location of the obecjts are same or not and compare both are of same type or not. And whereas ".equals" is used to compare both are of same contents
    or not.
    Example:
    String str1="demo";
    String str2=new String("demo");
    if(str1.equals(str2))
    System.out.println(" if(str1.equals(str2)"); //true
    if(str1==str2)
    System.out.println(" s1==s2"); //false
    Hope you will understand the difference between both the methods.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Explain the difference between == & .equals methods in Java?

    This both methods are very confusing for any programmer at the start. People think they both are similar only, but they are not similar. The "equals()" method is from Object Class.And so this method will check the equality between two objects.
    example:
    String s1 = "JAVA";
    String s2 = "JAVA";
    s1.equals(s2) ;// this will returns true as answer.
    And "==" is a operator type, and it can will be use in two ways: to compare the values of primitive variables and to compare the object reference variables.
    Example:
    String s1
    char s2
    String s3
    s1==s3 //it will returns true as answer.
    s1==s2 //it will returns false as answer.

Similar Threads

  1. Explain java xor (^) operator
    By sligh in forum Software Development
    Replies: 5
    Last Post: 09-05-2012, 06:44 PM
  2. Explain IP Address and Real IP as well as their difference
    By Abélard in forum Networking & Security
    Replies: 6
    Last Post: 02-06-2011, 11:22 PM
  3. Explain difference between primary and secondary computer memory
    By fARUQ aHMED in forum Motherboard Processor & RAM
    Replies: 3
    Last Post: 29-12-2010, 06:09 PM
  4. Replies: 5
    Last Post: 05-01-2010, 05:42 PM
  5. explain class in java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 09:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,485,999.04765 seconds with 17 queries