• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

K&B doubt

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

in k&B book its written in chapter...6 summury..

that ...

[B}
The StringBuffer class equals() is not overridden; it uses == under
the covers.
[/B]
what does == means here ...


and

Wrappers won�t pass equals() if they are in different classes.

but in one example ...
it written

Double d1 = new Double("3.0");
Integer i1 = new Integer(3); // create a couple of wrappers
if ( d1.equals(i1) ) { // are the values equal ?
System.out.println("wraps are equal"); // no output, different classes
}

what does the above sentence means with respect to eg. ?

pls clear both the above doubt
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

DOUBT #2
It is fairly simple:

3 == 3.0 BUT Double != Integer ---> Double(3.0) != Integer(3)

If you had:

3 == 3.0, Double == Double ---> Double(3.0) == Double (3)

The point is that with wrapper objects, the equal test will be true ONLY if BOTH the numbers AND the wrapper class are equal.

Giovanni

BUT you are using those "equal" numbers in two "different" wrapper objects (Double and Integer), for this reason the equal test returns false.
[ May 09, 2005: Message edited by: Giovanni De Stefano ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
didn't clear....pls
read the question again....though i m clear what is written in k&B and u just repeat that...
but what i m asking that K&B has conused with the eg. and the text written..
see question again....with contradictory eg.

thanx
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test1 {
public static void main(String arg []){
StringBuffer sb1 = new StringBuffer("xyz");
StringBuffer sb2 = new StringBuffer("xyz");
// later add here
if (sb1==sb2)
System.out.println("object is only one and both sb1 n sb2 are refering to that");
else
System.out.println("objects are different with same value");
if (sb1.equals(sb2))
System.out.println("equal");
else
System.out.println("not equal");

}
}

Hi Amit,
the above code is self explanatory.....
after running that code please add this line at //later add here
sb2=sb1;
again the answer is self explanatory.......
if any query further please let me know.....
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx deepak

ok but tell me one thing that will equal() will not produce error when we use to compare different types of object i.e not in the same class hirarchy ...

see in above my post ...there is Double is comparing with Integer ? will it not produce error..as it written in some eg. of K&B
but K&B also wrote that it will produce compiler error when we compare differnt objects ?

/???
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Wrappers won�t pass equals() if they are in different classes."
this means, that wrappers equals method will return false, if we'll try to compare wrappers of different types, whether they have equal values or not

Double d1 = new Double("3.0");
Integer i1 = new Integer(3); // create a couple of wrappers
if ( d1.equals(i1) ) { // are the values equal ?
System.out.println("wraps are equal"); // no output, different classes
}

The comment says, that there will be no output, although one would think, that there will be "wraps are equal" output. The comment says true - there will be no output, as the wrappers are of different type
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit,

can you please tell me WHERE you found this:

...there is Double is comparing with Integer ? will it not produce error..as it written in some eg. of K&B
but K&B also wrote that it will produce compiler error when we compare differnt objects ?



We are talking about comparing DIFFERENT WRAPPERS...why should you get an error??? From the way you speak you are expecting something like a compiler error or a runtime error...but it will simply return FALSE!!!

Just try to read more carefully whatever you are studying, and use simple example to try what the book says...

If the following code does not help you...well...I don't know what to say!


Be sure you understand when the book talks about == or equals(), they do not act always the same way...guess when they do?

THAT'S IT!

Giovanni
[ May 10, 2005: Message edited by: Giovanni De Stefano ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx goivanni..

can u pls tell me why t1==t2 is giving compiler error...
atleast it must return false if they don't reffer to same object..

i m saying this because... every reference variable is holding bits patterns which internally gives the address of object on heap...
so when we compare reference variable we campare bit patterns
and on the basis of that it returns true/false..

pls correct me...and my confussion..
thanx
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit,

t1 == t2 gives you a compiler error because they are different objects, it's like comparing apples with meat...they have nothing in common and you cannot check if they point to the same thing (the same object) because it wouldn't make any sense to compare them... == checks if 2 things point to the same object. For the bit pattern...well, you only have to know that there is such a thing as a bit pattern only to understand how objects are stored on the heap, don;t use this knowledge to understand how == and equals work!

equals checks if 2 things are meaningful the same...

Let me know if this is clear, or if you still have doubts, there are a lot of other things to say about equals and ==, that part might be confusing on K&B at the beginning, but if you try a lot of examples, you will get it!

This explanation is just the basic you MUST understand in order to grasp the other concepts.

Giovanni
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, Amit is right about this one.

When you compare two different objects, Java will implicitely cast the subclass to the base class... But in this case, neither object is a subclass of the other. They have to be explicitely casted to a common base class. (Or you can cast one, and let Java implicitely cast the other)

Henry
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


They have to be explicitely casted to a common base class. (Or you can cast one, and let Java implicitely cast the other)


Henry,
Can you just show this with some code from the above example,so that we should not get any compiler error.This == and equals thing really gets confusing at times.Thanks
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya... pls explain in details regarding explict/implicing casting while comparing ==

it would be glad or ...just copy past some eg.


another thing i would like to advice to bert bates and kethy sierra
to remove the line they have written in earlier chapter ...that
== compares bit pattern of object while comparing...
so ppl don't get confused...

Thanx v.much..
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jas oberai:

Henry,
Can you just show this with some code from the above example,so that we should not get any compiler error.This == and equals thing really gets confusing at times.Thanks



You can explicitly cast both objects...



Or you can cast only one, either one, and let Java implicitely cast the other.



And BTW, this exercise is kinda pointless. If you need a cast to a common base class, you kinda know the result. The condition will report false -- as there is no way they can be the same reference.

Henry
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

And BTW, this exercise is kinda pointless. If you need a cast to a common base class, you kinda know the result. The condition will report false -- as there is no way they can be the same reference.



I totally agree with you...that's why in my previous post I wrote:

Let me know if this is clear, or if you still have doubts, there are a lot of other things to say about equals and ==, that part might be confusing on K&B at the beginning, but if you try a lot of examples, you will get it!

This explanation is just the basic you MUST understand in order to grasp the other concepts.



The secret is to try EVERY combination suggested in the book and that comes in your mind!

For example, run this example and be sure you understand the output:


...and this:
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Giovanni!

nice posts, but lets not talk Strings yet...thats a totally different ball game. that may confuse a lot more.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think in giovanni last post the

answer of first eg will be
s1 == s2
s1 equals s2


and answer of 2nd eg. will be

s1 != s2
s1 equals s2

is i am rt ??


but in any case why goivanni give this eg.. i m not able to correlate eg. with the post ..pls tell me

thanx
 
reply
    Bookmark Topic Watch Topic
  • New Topic