• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overloading and casting

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a modified bit of code from K&B.

I'm getting confused about casting - I thought I had this nailed but then suddenly a question comes up and the brain starts to wobble.



This prints
 
Paul Kemp
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woah, I hadn't finished posting...

This prints

class ch1.Redwood
Tree
Tree

but I was expecting that at runtime the last tree would have been redwood.

Can anyone point me to a rule I have overlooked, or am I barking up the wrong tree (sorry, I'll get my coat....)
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
go2((Tree)new Redwood(), new Redwood());

this will call

void go2(Tree t1, Redwood r1), as you are converting while calling
(Tree)new Redwood(), JVM calls using reference type not using their actual object.

Try this one:

[ January 02, 2009: Message edited by: Punit Singh ]
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this is very simple. When you call a method, the type of the reference decides which method is called. Suppose there are two method

myMethod(Number n){}//1

and

myMethod(Integer i){}//2

Now if you write this code

Integer i = 10;
Number n = i;
myMethod(i); //calls 2
myMethod(n); //calls 1

So the type of the arguments decides which method is called...
 
Paul Kemp
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, too many rules going around in my head now. Can't see the wood for the trees (ahem).

Page 115 K&B, "Reference type determines which overloaded version is selected. Happens at compile time."
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make lots of code Paul, coding is easy way to remember than just reading.
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Punit is right. During coding you not only memorize the given facts and concepts but also learn to experiment. This helps in learning new things.
 
Paul Kemp
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,

My strategy is
-->reading new K&B chapter
-->making notes
-->re-reading the notes
-->attempting questions
-->coding up the one's I get wrong
-->playing with code and attempting what-if? scenarios
-->seeking out free additional questions on the subject I have studied.
-->streamlining my notes
--> back to 1

Unfortunately, by the time I got to Chapter 10 of the book I realised I've forgotten some key points from Chapter 1. :-)

I can see that the 6 weeks I had planned to study for this were not enough for me, as I wasn't previously familiar with the nitty gritty of many of the subjects (I've been coding part-time using Java 1.4, and don't use concurrency, for example).

Time to go though everything again

 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Being a human you will forget what you have learned. We do have to revise and read them again. That is why practicing is considered a good way to remember things. 2-3 complete revisions are needed of K&B to come at comfortable level. But once you did that you can easily carry on your further learning process.
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Kemp:
Time to go though everything again



Just relax. When you'll have your SCJP certificate in your hand, you would feel that all this effort was worth it
 
Paul Kemp
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers guys.

Himanshu, I think I used to be human, but I am slowly turning into a compiler.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good for you. So do you have any version also??
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Kemp:

I think I used to be human, but I am slowly turning into a compiler.



Dont forget you should also be the JVM to pass the exam.
 
Paul Kemp
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm version 0.1 and am currently very very buggy.
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Kemp:
I'm version 0.1 and am currently very very buggy.



I think very much buggy version are Beta. So you might Paul Beta Version
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Punit Singh:
go2((Tree)new Redwood(), new Redwood());

this will call

void go2(Tree t1, Redwood r1), as you are converting while calling
(Tree)new Redwood(), JVM calls using reference type not using their actual object.

Try this one:


[ January 02, 2009: Message edited by: Punit Singh ]



I'm not getting it. In the second method call, what is the reference type of (Tree)new Redwood()?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Tree) new Redwood() is of type Tree. What might be confusing you is that you are not seeing an actual reference variable. You could think of it this way:

(Tree) new Redwood() is creating a new Redwood object, but you can also think of it as an anonymous reference of type Redwood pointing to the newly created object.
Then, (Tree) new Redwood() is casting that anonymous reference variable to type Tree (but it still points to a Redwood object.)

You can also think as casting an object to a type, but I think it is better to think of it in terms of anonymous references, since you can't actually change an object from a type to another through casting. The object remains the same, it is the reference type that changes.

Did that help, or did it make things even more confusing?
[ January 02, 2009: Message edited by: Ruben Soto ]
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Kemp:
I'm version 0.1 and am currently very very buggy.




I am also version 0.1, and my JVM is at the edge of StackOverflowError with all the information I've been trying to cram into it lately.
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ruben, according to your explanation, in Line1 if I consider there is an anonymous reference variable of type Tree pointing to a Tree object, but then it is down casted to Redwood, then in line2 what is Tree t1 pointing to and what is its reference type.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhi,

As you know, the code won't get that far (as there is an exception thrown on the first invocation of go2(). However, let's assume that the code got to line 1. Then, when you do (Redwood) new Tree() you are going to get a ClassCastException, since Tree is not subtype of Redwood. So the discussion does not apply. Does that make sense?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup.
Thanks Ruben.
 
ioanis vincent
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ruben for your help. Are these "anonymous references" explained in some sort of documentation? Where could I read more about this?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ioanis,

I haven't seen that term used in this context anywhere, although I am sure someone has used it before me. I just came to that conclusion after thinking about what casting is, and how it is that we can cast "new objects."

Actually, I searched the JLS for "anonymous reference" and I didn't come up with anything, but at the end of section 15.9.4 (JLS, 3rd Edition) it says:

The value of a class instance creation expression is a reference to the newly
created object of the specified class. Every time the expression is evaluated, a
fresh object is created.



Although it doesn't use the term "anonymous reference" it says that the result of new X() is a new reference of type X.
 
There is no "i" in denial. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic