• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its because your concatenation operator (+) has higher precedence than == operator
 
Sujatha Rangarajan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but how that stop hello world getting printed..i didnt get the point..please can you explain it..
 
Gaurav Singh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well what you are doing is that first you concatenate the two strings "hello world" and "abc" and then compare it with another string, and finally print out the boolean result.
Hope thats clear.

I guess this question should have been posted in beginners forum.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String equality is tested by the "equals" method. It's rarely correct to compare strings using "==". More information can be found here.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here a is created in the String Pool while b is an object created in heap.
== operator compares the object reference so obviously it wud result into false
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic