File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doesn't print the statement in System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doesn Watch "doesn New topic

doesn't print the statement in System.out.println

Raju Sri
Ranch Hand

Joined: Mar 10, 2004
Posts: 108
Hi all,
Can any body tell me why the following code doesn't print "TO JAVA: false".
String h1="hello";
String h2=new String("hello");
System.out.println("TO JAVA:"+h1 == h2);
I thought the program should print like below
TO JAVA:false
But when I run the code it is not printing "TO JAVA" statment .
Thanks in advance

SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS 1.4
Vijay Pawar

Joined: Sep 04, 2003
Posts: 26
Dear shrini,
The rule is :
Operand 1 + Operand 2
If Operand 1 is a String then + will behave as concatination operator
and result will be a String
"To JAVA:" + h1 == h2 will be executed as
"To Java: hello" == h2
and the result u know is false
I agree. Here's the link:
subject: doesn't print the statement in System.out.println
It's not a secret anymore!