Results 1 to 5 of 5

Thread: Differance between "==" & string.equals()

  1. #1
    Geek_Guest
    Guest

    Differance between "==" & string.equals()

    What is the differance between "==" & string.equals() explain with example

    Question asked by visitor hanumanth


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    Re: Differance between "==" & string.equals()

    Hi,
    Strings in Java are considered objects and you cant test equality with objects using "==" cause in Java this means that you are testing that both object references are pointing to the same object..


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    12

    Re: Differance between "==" & string.equals()

    In Java, "==" is used for comparing the premetive data types, and for objects "equals()" must be used. if u use "==" for objects comparison, it wiil just compare the reference of the objects not the content.

    if u declare a string like

    String a = "Same"; //1

    Actually JVM doesnot consider it as a String object, rather than it will be consider as String literal. Only when u declare like

    String a = new String("Same"); //2

    will leads to create a object of type String.

    When u use type 1 of declaration u can make use of "==". But for the second type u must use "equals()" method.


  4. #4
    Junior Member
    Join Date
    Jul 2007
    Answers
    2

    Re: Differance between "==" & string.equals()

    It is important to understand that the equals() method and the == operator perform two different operator.

    simply i will tell


    the equals() method compares the characters inside a string object.
    the == operator compares two object references
    .

    example


    class compare {
    public static void main(String args[]) {
    String s1 = "abc";
    String s2 = "abc";
    System.out.println(s1==s2);
    System.out.println(s1.equals(s2));
    Stirng s3 = new String("def");
    String s4 = new String("def");
    System.out.println(s3==s4);
    System.out.println(s3.equals(s4));
    }
    }

    just this program compile and run it then understand these compare of the == and equals().


  5. #5
    Junior Member
    Join Date
    Jul 2007
    Answers
    2

    Re: Differance between "==" & string.equals()

    It is important to understand that the equals() method and the == operator perform two different operator.

    simply i will tell


    the equals() method compares the characters inside a string object.
    the == operator compares two object references.

    example


    class compare {
    public static void main(String args[]) {
    String s1 = "abc";
    String s2 = "abc";
    System.out.println(s1==s2);
    System.out.println(s1.equals(s2));
    Stirng s3 = new String("def");
    String s4 = new String("def");
    System.out.println(s3==s4);
    System.out.println(s3.equals(s4));
    }
    }

    just this program compile and run it then understand these compare of the == and equals().


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact