• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Explanation of the program

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends ,
can anyone explain the meaning of lines 9 and 10.
Thanks in Advance

karthikeyan Ravindran





1. public class test
2. {
3. public static void main(String args[])
4. {
5. class Foo
6. {
7. public int i = 3;
8. }
9. Object o = (Object)new Foo();
10. Foo foo = (Foo)o;
11. System.out.println("i =" + foo.i);
12. }
13. }
[ November 28, 2007: Message edited by: Karthikeyan Ravindran ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Karthikeyan Ravindran:

9. Object o = (Object)new Foo();
10. Foo foo = (Foo)o;



These two lines are equivalent to this single line:


In line 9, you are getting a new object of class Foo and casting it to Object.
In line 10, you again cast it to Foo.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
9. Object o = (Object)new Foo();

Note that the cast in this line is unnecessary. All classes extend from object, so you can assign anything (except primitive values) to a variable of type Object without needing to cast it.
 
Karthikeyan Ravindran
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper and pranav..
[ November 28, 2007: Message edited by: Karthikeyan Ravindran ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!