File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes I think there is a bug in the java soft Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I think there is a bug in the java soft" Watch "I think there is a bug in the java soft" New topic
Author

I think there is a bug in the java soft

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689



hello, i am trying to print this lineX statement to the console
but it just displays flase
it is not displaying s3==s4 false
so, this is the bug in java :roll:

atleast it must to give compile time error


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
hi krish.....
there is no bug in your code
System.out.println("s3==s4"+s3==s4);//lineX
in this case
first s3 gets concanated to your "s3==s4"
and then there is comparision betn
"s3==s4apple" == s4
first second
so
output
false
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
There is no bug in either the JVM or the compiler. You're assuming that you can test string equality by using "==". In general, that is not so - use the String.equals method.


Ping & DNS - my free Android networking tools app
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

Cool.

First read this,

Println() always calls the toString() method on Object and the returned toString() String argument will not be placed String pool but will be stored as a normal attribute in local method stack, or treated as argument to method println().

Here the toString() method on both s3 & s4 will be called and 2 String objects in local stack will be created, so the == will give false, because, they are pointing to different memory locations.

One can rectify this using the equals method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This is a wonderfully subtle issue, and only Amod Gole picked up on it; neither Ulf nor Srinivas spoke to it.

The reason that only 'false' is printed, and not 's3 == s4 false' is that the expression

"s3 == s4" + s3 == s4

is not interpreted as

"s3 == s4" + (s3 == s4)

but rather as

("s3 == s4" + s3) == s4

and, given that, it should be obvious why just 'false' is printed.

Note that much of what Srinivas says about Strings and pools and stacks and locals and so on and so on wouldn't be relevant (or quite correct) even if this was a question about equals() vs. == -- but it's not.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I think there is a bug in the java soft
 
Similar Threads
General problems..
Strings
Doubt on Strings
is getting me confused
String and String Object Problem