A Test Question

S

Stephan Wehner

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
 
C

Christophe Vanfleteren

Stephan said:
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?
 
T

Tony Morris

Stephan Wehner said:
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
 
R

Ryan Stewart

Tony Morris said:
Stephan Wehner said:
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?
 
M

marcus

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

Ryan said:
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?
 
W

Woebegone

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

Ryan said:
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"));
}
 
W

Woebegone

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

Ryan said:
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
 
T

Timo Kinnunen

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?
 
T

Tim Slattery

Timo Kinnunen said:
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.
 
C

Chris Uppal

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
 
T

Timo Kinnunen

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!
 
S

Stephan Wehner

Chris Uppal said:
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
 
M

marcus

Timo said:
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.
 
T

Tony Morris

Stephan Wehner said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top