Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

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: 17346
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: 17346
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: 17346
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: 67637
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: 17346
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"
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic