Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference variable casting

 
Greenhorn
Posts: 21
  • 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 p:112 there is an example program which explains reference variable casting. can anybody explain me line: 3 and 4.

1. class CastTest2{
2. public static void main(String [] ar){
3. Animal[] a={new Animal(), new Dog(), new Animal()};
4. for(Animal animal : a){
5. animal.makeNoise();
6. if(animal instanceof Dog){
7. animal.playDead();
8. }}}}
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vejaykrishna:
HI,

In K&B book p:112 there is an example program which explains reference variable casting. can anybody explain me line: 3 and 4.

1. class CastTest2{
2. public static void main(String [] ar){
3. Animal[] a={new Animal(), new Dog(), new Animal()};
4. for(Animal animal : a){
5. animal.makeNoise();
6. if(animal instanceof Dog){
7. animal.playDead();
8. }}}}



Line 3: just creates an array of objects, all of which can be casted to an Animal object (since the array is of type Animal). The new Dog() object is a subclass of Animal and hence, can be upcasted to an Animal object and the other two elements are Animal objects.

Line 4: an example of the new enhanced for-loop in 1.5. It is initializing an object named animal which is of type Animal which will be assigned to each of the elements in the array created in line 3.

In the first iteration it animal will point to the first element in the a array (the new Animal object); in the second iteration, a will point to the second element in the array (the new Dog object). In the final iteration, it will be assigned to the second new Animal object created on line 3.

Does this answer your question?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding your display name and the other thread. I will just copy my last post.


At the top of this forum under the Post a Reply or Post new message buttons there is a My Profile link, click that and under the field where it says display name, just add a space to the end of what you currently have and add your last name.

Thanks

Mark
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr. Chris Allen.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought you said your last name is venkatesan. Is that what the V K is supposed to mean?

Thanks

Mark
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Mark,

My name is R. V. Vejaykrishna, where V-stands for Venkatesan(last name) and R- stands for My Grand Father and Vejaykrishna is my name.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vejaykrishna R V:
Hi Mr. Mark,



Mark is my first name, so Mr. wouldn't be with Mark. It would be with my last name Spritzler, as in Mr.. Spritzler.

This is what I'd expect to see your display name be "Vejaykrishna Venkatesan"

Mark
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vejaykrishna R V:

My name is R. V. Vejaykrishna



R. V., then that should be what your display name is set to. The initials at the end of the name are not acceptable. Please adjust it at your earliest opportunity.

thanks,
bear
JavaRanch Sheriff
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vejaykrishna,

Thank you so much for changing your display name. I think it looks beautiful now. I really do appreciate it.

Mark
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr. Bibeault & Mr. Spritzler.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3. Animal[] a={new Animal(), new Dog(), new Animal()};

Dog and Animal implicitly cast to Animal.

4. is kind of a for-each loop (JDK 1.5 feature). You should read it as

for-each "animal" in array of|collection of animals "a"
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic