A Test Question

Discussion in 'Java' started by Stephan Wehner, Apr 8, 2004.

  1. What is the output when executed?



    public static void main(String[] args)
    {
    String a = "A";
    String b = "A";
    System.out.println("a==b:" + a==b);
    }




    Stephan

    ______________________
    Stephan Wehner
    Editor, Traffic Life: Passionate Tales and Exit Strategies
    www.trafficlife.com
     
    Stephan Wehner, Apr 8, 2004
    #1
    1. Advertising

  2. Stephan Wehner wrote:

    > What is the output when executed?
    >
    >
    >
    > public static void main(String[] args)
    > {
    > String a = "A";
    > String b = "A";
    > System.out.println("a==b:" + a==b);
    > }


    Ooh, I know, I know ...

    How about running it and seeing what result it gives?

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Apr 9, 2004
    #2
    1. Advertising

  3. A Test Answer. Try it.
     
    Andrew Thompson, Apr 9, 2004
    #3
  4. Stephan Wehner

    Tony Morris Guest

    "Stephan Wehner" <> wrote in message
    news:...
    > What is the output when executed?
    >
    >
    >
    > public static void main(String[] args)
    > {
    > String a = "A";
    > String b = "A";
    > System.out.println("a==b:" + a==b);
    > }
    >
    >
    >
    >
    > Stephan
    >
    > ______________________
    > Stephan Wehner
    > Editor, Traffic Life: Passionate Tales and Exit Strategies
    > www.trafficlife.com


    The output is true, in accordance with Java Language Specification §3.10.5
    What kind of test is this ?
    The SCJP exam demands knowledge of String literals and how they are handled.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Apr 9, 2004
    #4
  5. Stephan Wehner

    Ryan Stewart Guest

    "Tony Morris" <> wrote in message
    news:c54ncf$kps$...
    > "Stephan Wehner" <> wrote in message
    > news:...
    > > What is the output when executed?
    > >
    > >
    > >
    > > public static void main(String[] args)
    > > {
    > > String a = "A";
    > > String b = "A";
    > > System.out.println("a==b:" + a==b);
    > > }
    > >
    > >
    > > Stephan
    > > ______________________
    > > Stephan Wehner
    > > Editor, Traffic Life: Passionate Tales and Exit Strategies
    > > www.trafficlife.com

    >
    > The output is true, in accordance with Java Language Specification §3.10.5
    > What kind of test is this ?
    > The SCJP exam demands knowledge of String literals and how they are

    handled.
    >

    Technically, it's "a==b:true". But yes, is this supposed to be news?
     
    Ryan Stewart, Apr 9, 2004
    #5
  6. Stephan Wehner

    marcus Guest

    Wow -- I get false -- i do this wrong all tht time
    try a.equals(b)

    Ryan Stewart wrote:
    > "Tony Morris" <> wrote in message
    > news:c54ncf$kps$...
    >
    >>"Stephan Wehner" <> wrote in message
    >>news:...
    >>
    >>>What is the output when executed?
    >>>
    >>>
    >>>
    >>>public static void main(String[] args)
    >>>{
    >>> String a = "A";
    >>> String b = "A";
    >>> System.out.println("a==b:" + a==b);
    >>>}
    >>>
    >>>
    >>>Stephan
    >>>______________________
    >>>Stephan Wehner
    >>>Editor, Traffic Life: Passionate Tales and Exit Strategies
    >>>www.trafficlife.com

    >>
    >>The output is true, in accordance with Java Language Specification §3.10.5
    >>What kind of test is this ?
    >>The SCJP exam demands knowledge of String literals and how they are

    >
    > handled.
    >
    > Technically, it's "a==b:true". But yes, is this supposed to be news?
    >
    >
     
    marcus, Apr 9, 2004
    #6
  7. Stephan Wehner

    Woebegone Guest

    "marcus" <> wrote in message
    news:...
    > Wow -- I get false -- i do this wrong all tht time
    > try a.equals(b)
    >
    > Ryan Stewart wrote:
    > > "Tony Morris" <> wrote in message
    > > news:c54ncf$kps$...
    > >
    > >>"Stephan Wehner" <> wrote in message
    > >>news:...
    > >>
    > >>>What is the output when executed?
    > >>>
    > >>>
    > >>>
    > >>>public static void main(String[] args)
    > >>>{
    > >>> String a = "A";
    > >>> String b = "A";
    > >>> System.out.println("a==b:" + a==b);
    > >>>}
    > >>>
    > >>>
    > >>>Stephan
    > >>>______________________
    > >>>Stephan Wehner
    > >>>Editor, Traffic Life: Passionate Tales and Exit Strategies
    > >>>www.trafficlife.com
    > >>
    > >>The output is true, in accordance with Java Language Specification

    §3.10.5
    > >>What kind of test is this ?
    > >>The SCJP exam demands knowledge of String literals and how they are

    > >
    > > handled.
    > >
    > > Technically, it's "a==b:true". But yes, is this supposed to be news?
    > >
    > >

    >

    I get false too; I think §3.10.5 applies when at least one of lhs or rhs is
    a literal --

    public static void main(String[] args) {
    String a = "A";
    String b = "A";
    System.out.println("a == b: " + a == b);
    System.out.println("\"A\" == b: " + "A".equals(b));
    System.out.println("a == \"B\": " + b.equals("A"));
    }
     
    Woebegone, Apr 9, 2004
    #7
  8. Stephan Wehner

    Woebegone Guest

    "Woebegone" <> wrote in message
    news:0gqdc.11949$...
    >
    > "marcus" <> wrote in message
    > news:...
    > > Wow -- I get false -- i do this wrong all tht time
    > > try a.equals(b)
    > >
    > > Ryan Stewart wrote:
    > > > "Tony Morris" <> wrote in message
    > > > news:c54ncf$kps$...
    > > >
    > > >>"Stephan Wehner" <> wrote in message
    > > >>news:...
    > > >>
    > > >>>What is the output when executed?
    > > >>>
    > > >>>
    > > >>>
    > > >>>public static void main(String[] args)
    > > >>>{
    > > >>> String a = "A";
    > > >>> String b = "A";
    > > >>> System.out.println("a==b:" + a==b);
    > > >>>}
    > > >>>
    > > >>>
    > > >>>Stephan
    > > >>>______________________
    > > >>>Stephan Wehner
    > > >>>Editor, Traffic Life: Passionate Tales and Exit Strategies
    > > >>>www.trafficlife.com
    > > >>
    > > >>The output is true, in accordance with Java Language Specification

    > §3.10.5
    > > >>What kind of test is this ?
    > > >>The SCJP exam demands knowledge of String literals and how they are
    > > >
    > > > handled.
    > > >
    > > > Technically, it's "a==b:true". But yes, is this supposed to be news?
    > > >
    > > >

    > >

    > I get false too; I think §3.10.5 applies when at least one of lhs or rhs

    is
    > a literal --
    >
    > public static void main(String[] args) {
    > String a = "A";
    > String b = "A";
    > System.out.println("a == b: " + a == b);
    > System.out.println("\"A\" == b: " + "A".equals(b));
    > System.out.println("a == \"B\": " + b.equals("A"));
    > }
    >
    >

    Sorry -- badly done by me. Meant to say:
    public static void main(String[] args) {
    String a = "A";
    String b = "A";
    System.out.println("a == b: " + a == b);
    System.out.println("\"A\" == b: " + "A".equals(b));
    System.out.println("b == \"A\": " + b.equals("A"));
    }
    with output

    false
    "A" == b: true
    b == "A": true
     
    Woebegone, Apr 9, 2004
    #8
  9. On 8 Apr 2004 15:47:43 -0700, Stephan Wehner wrote:

    > What is the output when executed?
    >
    > public static void main(String[] args)
    > {
    > String a = "A";
    > String b = "A";
    > System.out.println("a==b:" + a==b);
    > }


    The output is false, because
    System.out.println("a==b:" + a==b);

    is the same thing as
    System.out.println(("a==b:" + a)==b);

    The clue was: what happened to the "a==b:" -part?
     
    Timo Kinnunen, Apr 9, 2004
    #9
  10. Stephan Wehner

    Tim Slattery Guest

    Timo Kinnunen <> wrote:

    >On 8 Apr 2004 15:47:43 -0700, Stephan Wehner wrote:
    >
    >> What is the output when executed?
    >>
    >> public static void main(String[] args)
    >> {
    >> String a = "A";
    >> String b = "A";
    >> System.out.println("a==b:" + a==b);
    >> }

    >
    >The output is false, because
    >System.out.println("a==b:" + a==b);
    >
    >is the same thing as
    >System.out.println(("a==b:" + a)==b);


    It's false, but not because of that.

    a==b gives false here because the == operator sees that a and b are
    two different String objects, it would return true only if a and b
    both referred to the same object. That's why there's an "equals"
    function in the String object, that compares the strings contained in
    the objects. The "==" operator is useless with Strings.

    --
    Tim Slattery
     
    Tim Slattery, Apr 9, 2004
    #10
  11. Stephan Wehner

    Chris Uppal Guest

    Tim Slattery wrote:

    > > > String a = "A";
    > > > String b = "A";


    > a==b gives false here because the == operator sees that a and b are
    > two different String objects, it would return true only if a and b
    > both referred to the same object.


    They *do* refer to the same object.

    For reasons already stated in this thread.

    (BTW, I consider this to be an unfair question for use in any test. I agree
    that both issues can, and do, come up in real life, but *I* would likely have
    missed the precedence issue, so the question is *obviously* unfair....)

    -- chris
     
    Chris Uppal, Apr 9, 2004
    #11
  12. On Fri, 09 Apr 2004 09:08:30 -0400, Tim Slattery wrote:

    > Timo Kinnunen <> wrote:
    >
    >>The output is false, because
    >>System.out.println("a==b:" + a==b);
    >>
    >>is the same thing as
    >>System.out.println(("a==b:" + a)==b);

    >
    > It's false, but not because of that.


    Yes it is. System.out.println(("a==b:" + a)==b); outputs "false" while
    System.out.println("a==b:" + (a==b)); outputs "a==b:true". The first output
    is missing the "a==b:" part!
     
    Timo Kinnunen, Apr 9, 2004
    #12
  13. "Chris Uppal" <-THIS.org> wrote in message news:<>...
    > Tim Slattery wrote:
    >
    > > > > String a = "A";
    > > > > String b = "A";

    >
    > > a==b gives false here because the == operator sees that a and b are
    > > two different String objects, it would return true only if a and b
    > > both referred to the same object.

    >
    > They *do* refer to the same object.
    >
    > For reasons already stated in this thread.
    >
    > (BTW, I consider this to be an unfair question for use in any test. I agree
    > that both issues can, and do, come up in real life, but *I* would likely have
    > missed the precedence issue, so the question is *obviously* unfair....)
    >
    > -- chris


    Ok, maybe it is unfair for an interview or so.
    But it was fun to run into this, and so I passed it on.

    Stephan

    ______________________
    Stephan Wehner
    Editor, Traffic Life: Passionate Tales and Exit Strategies
    www.trafficlife.com
     
    Stephan Wehner, Apr 9, 2004
    #13
  14. Stephan Wehner

    marcus Guest

    Timo Kinnunen wrote:
    > On Fri, 09 Apr 2004 09:08:30 -0400, Tim Slattery wrote:
    >
    >
    >>Timo Kinnunen <> wrote:
    >>
    >>
    >>>The output is false, because
    >>>System.out.println("a==b:" + a==b);
    >>>
    >>>is the same thing as
    >>>System.out.println(("a==b:" + a)==b);

    >>
    >>It's false, but not because of that.

    >
    >
    > Yes it is. System.out.println(("a==b:" + a)==b); outputs "false" while
    > System.out.println("a==b:" + (a==b)); outputs "a==b:true". The first output
    > is missing the "a==b:" part!



    I'll be g-d'ed. I stand corrected, feel humbled and all that.

    I did not know identical string literals were the same object.
     
    marcus, Apr 10, 2004
    #14
  15. Stephan Wehner

    Tony Morris Guest

    "Stephan Wehner" <> wrote in message
    news:...
    > What is the output when executed?
    >
    >
    >
    > public static void main(String[] args)
    > {
    > String a = "A";
    > String b = "A";
    > System.out.println("a==b:" + a==b);
    > }
    >
    >
    >
    >
    > Stephan
    >


    Good one.
    You certainly sucked me in.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Apr 12, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting
    Replies:
    5
    Views:
    1,661
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    397
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    685
    Chris
    May 11, 2006
  4. Edvard Majakari
    Replies:
    4
    Views:
    706
    Edvard Majakari
    Feb 25, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    765
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page