• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof ?? surprising

 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# What is the output of the following code ?

Integer ii = new Integer(10);
System.out.println(ii instanceof (Object));

******

The code does not compile.

BUT
System.out.println((ii) instanceof Object); -- This compiles and returns TRUE.

What is the Difference ???
[ February 25, 2005: Message edited by: Soumy Sam ]
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what is the compiler error you get?
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using (Object), try removing () and use Object.
 
Evgeni Shuster
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(SomeType)Something means: cast Something to type SomeType,
for example:

here (Object)MyString is of type Object
In your example compiler needs some variable or expression after (Object)
 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The errors are
1) Illegal start of type

2) ) expected.
 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using (Object), try removing () and use Object.

My doubt is why (Object) gives error..
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, because that's not the syntax that the creators of the Java language chose to use. The instanceof keyword must be followed by the name of a reference type. That's it. No parentheses are required or allowed.
 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic