String
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
for following code:
class sample
{
public static void main(String[] args)
{
String a="abc";
String b=new String("abc");
System.out.println("Hello World!"+a==b);
}
}
output: false
can anyone explain the reason why "Hello World!" is not printed?
Thanks
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hope thats clear.
I guess this question should have been posted in beginners forum.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
== operator compares the object reference so obviously it wud result into false
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The + operator has higher precedence than the == operator. Because of this, the line:
System.out.println("Hello World!"+a==b);
is treated identically to
System.out.println(("Hello World!"+a)==b);
So, what gets printed is a comparison of two references. One reference is to the string "Hello World!abc" and the other reference is to the string "abc". The two references are unequal, so false gets printed. The contents of the strings never get compared or even considered in this example.
[ August 23, 2006: Message edited by: Matt Harrah ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Matt Harrah:
The problem -- "why wasn't Hello World printed" -- is exactly what Guarav Singh pointed out - operator prcedence.
The + operator has higher precedence than the == operator. Because of this, the line:
System.out.println("Hello World!"+a==b);
is treated identically to
System.out.println(("Hello World!"+a)==b);
So, what gets printed is a comparison of two references. One reference is to the string "Hello World!abc" and the other reference is to the string "abc". The two references are unequal, so false gets printed. The contents of the strings never get compared or even considered in this example.
[ August 23, 2006: Message edited by: Matt Harrah ]
OOps I also missed that..???
BTW UD you took at wrong direction

The Best way to predict your future is to create it - Every great individual common man
| Police line, do not cross. Well, this tiny ad can go through: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










