Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: When would this code print out....

  1. #1
    icykum is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Question When would this code print out....

    class jaha{
    public static void main (String [] args){
    String x="hello";
    String y="hello";
    if (x.equals(y)){
    System.out.println("true");
    if (x==y){
    System.out.println("true");
    }
    if (x!=y){
    System.out.println("false");
    }
    }
    }
    }


    Is there any values of x and y to make it print out
    true
    false
    ??

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    rougeking is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: When would this code print out....

    output of the code would be:
    true
    false

    x.equals(y) gives us true
    but as x and y are strings x==y doesnt work so it prints false

    hope it helps :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: When would this code print out....

    Quote Originally Posted by rougeking View Post
    output of the code would be:
    true
    false

    x.equals(y) gives us true
    but as x and y are strings x==y doesnt work so it prints false

    hope it helps :)
    Not quite. x == y will be true *sometimes*. Since most Strings are interred in the String pool in order to minimize the unnecessary needless creation of objects, many times stringA will == stringB even when you don't expect it to. But if you explicitly tell the JVM to create a new String object via the new String(...) constructor, then you know that == will return false even if the Strings contain the same chars in the same order. The reason being that == checks for object equality -- that two references are actually one and the same, while the equals(...) method tests for functional equality -- that two objects are functionally equal as defined by the equals method of their class.

Similar Threads

  1. Why is the spacing so odd when I print this code?
    By HalfAZN in forum New To Java
    Replies: 5
    Last Post: 10-10-2012, 12:02 AM
  2. Code Print Error! Help needed
    By socboy6579 in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 03:35 AM
  3. How to use and print the JAVA code from a JSP
    By Ginkan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-23-2010, 02:05 PM
  4. Java code to connect printer to print content
    By wendyz in forum Advanced Java
    Replies: 3
    Last Post: 04-30-2009, 06:51 PM
  5. Replies: 11
    Last Post: 04-16-2008, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •