• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Exception Handling

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

Please explain me the line Confusion1 and Confusion 2.

public class Test {
2. public static void main(String args[]) {
3. class Foo {
4. public int i = 3;
5. }
6. Object o = (Object)new Foo(); \\ confusion 1
7. Foo foo = (Foo)o; \\ confusion 2
8. System.out.println(�i = � + foo.i);
9. }
10. }

Output is i=3;

Thank you in advance
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I can say waht I know.

Object o = (Object)new Foo(); \\ confusion 1

o can refer to an Object, Foo is a subclass of Object so that is ok. The (Object) is a cast, but I do not think it's necesarry.

Foo foo = (Foo)o; \\ confusion 2
foo can refer to a Foo but o is not a reference to a Foo. For the compiler o just refers to an Object, even if we know it actually is a Foo. Hence here you need the (Foo) cast to make clear to the compiler that o actually refers to a Foo.

But I do not understand the title of your question, is my answer helping you with what you wanted to ask??
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yOU ARE RIGHT MARC
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


6. Object o = (Object)new Foo(); \\ confusion 1
7. Foo foo = (Foo)o; \\ confusion 2
8. System.out.println(�i = � + foo.i);



Line 6 : Cast is not necessary as Marc has pointed out.
Line 7 : Cast is must, to compile the code, there wont be any problem at
run time because the run time type of the o will be Foo object so
assigning Foo object to Foo ref variable is OK.

Line 8: foo.i is OK.

o.i //compiler error
((Foo)o) .i; //OK





Thanks,
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with marc in this
[ May 22, 2007: Message edited by: Prasad Tamirisa ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,

I think its better you give an appropriate "Title" to the thread which is meaningful.

Marc has given a suitable reply to your query i think.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,

About confusion1:
Object o = (Object)new Foo(); \\ confusion 1

it is like upcasting, though not needed.

About confusion2:
It is a must and here you are downcasting the object reference o to class Foo's reference.
7. Foo foo = (Foo)o; \\ confusion 2

Hope the confusion is out.

Regards,
Gitesh
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic