Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Explanation of the program  RSS feed

 
Karthikeyan Ravindran
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 ]
 
Pranav Pal
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!