This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof ?? surprising Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof ?? surprising" Watch "instanceof ?? surprising" New topic
Author

instanceof ?? surprising

Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
# 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 ]

SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Well, what is the compiler error you get?
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
Why are you using (Object), try removing () and use Object.


SCJP 1.4<br />SCWCD 1.3
Evgeni Shuster
Greenhorn

Joined: Feb 13, 2005
Posts: 15
(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

Joined: Nov 02, 2004
Posts: 78
The errors are
1) Illegal start of type

2) ) expected.
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
Why are you using (Object), try removing () and use Object.

My doubt is why (Object) gives error..
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
thanks for the replies..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof ?? surprising
 
Similar Threads
Question about instanceof
Getting the type of variable instance
instanceof
2D Arrays
instanceof operator.