aspose file tools*
The moose likes Java in General and the fly likes String one true one false Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String one true one false" Watch "String one true one false" New topic
Author

String one true one false

sparsh khandelwal
Ranch Hand

Joined: Dec 20, 2010
Posts: 70

please give a clear cut clarification about line 7 and line 8 : outputs
one is true its ok
but why other one is false?
please help
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sparsh khandelwal wrote:
please give a clear cut clarification about line 7 and line 8 : outputs
one is true its ok
but why other one is false?
please help


The == operator evaluates to true only if both references point to the same object (or both are null). Based on the discussion in your other thread, you should now understand why it's false.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

There is no meaning in line 5. No matter if you omit it. You will get the same answer..

Try this...


When you creating a String using "" only, JVM creates the same String only one time. That means no duplicate Strings are created.(Therefore here both s and s1 refers the same object)



But when you are using new command to create a String object, duplication is allowed. Therefore here JVM creates 2 equal objects in the memory heep. As s and s1 refers 2 objects, s==s1 returns false...


Ramesh-X
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

sparsh khandelwal wrote:


Hi Sparsh,

In Java there are two different ways to check for equality.
1. == operator
2. equals() method (a part of Object which is a parent class to every other class)

While equals() works with object types only == works with primitives as well.
equals() checks the equality using the value contained by two objects (you can also do that with the same object though)
== gives a true result only when the two primitives have the same value (downcasting and upcasting should be considered also) or two references point to the exact same object.

you can override the equals() in your class and give meaning to it as to how you want your class objects to be checked for equality.

Also, in your code s1.concat("a") won't do any change to s1 itself which is why at line 7 the result of equals() is true.
Strings are immutable objects and whenever you operate on a String object it gives you another String object.

Phew ... this much for now ...
cheers :-)


http://plainoldjavaobject.blogspot.in
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
Ramesh Pramuditha Rathnayake wrote:There is no meaning in line 5. . . .
That is not quite correct. It does have meaning, in that is creates a new String "xyza". But that String is not used and disappears into cyber‑limbo never to be seen again. It would have been helpful to explain to the OP why you still get true from the equals() method later on. Lalit Mehra does actually supply that explanation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
Lalit Mehra wrote: . . .

In Java there are two different ways to check for equality. . . .
The right way and the wrong way.

The right way is to use the equals() method, which of course does not work on primitives. The == operator can produce incorrect and misleading results on reference types, and can even give incorrect results on primitives (see no 20).
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

Campbell Ritchie wrote:That is not quite correct...


I mean that no need of that line. Returning a new String object is correct...

== checks whether the object references are equal.


Here s ans s1 both refer the same object. Therefore the references are equal. (That means s==s1 is true)

But equals() method returns true if the 2 objects are equivalent. This equality has to be defined by the creator of the class. If not, equals() method in Object class is running..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

sparsh khandelwal wrote:but why other one is false?

If this is for the SCJP exam, you might find a read of the CachedObjects page useful. If not, you should AvoidTheEqualityOperator (←click).

If you get into that habit, you shouldn't need to worry about stuff like this.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

sparsh khandelwal wrote:
but why other one is false?


The result is false because == checks for referencial equality and in your code s and s1 point to two different objects.

cheers :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String one true one false
 
Similar Threads
Regd. Strings...
mock question
String Objects Confusion
String problem
Doubt About Strings