This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof" Watch "instanceof" New topic
Author

instanceof

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, Why I am getting error at Line 7 but I don't see any issue with Line 5
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

I think the error at Line 7 may be because- Its trying to do a cast?


Mohamed Sanaulla | My Blog
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

This is a syntax errror
just remove those brackets of Short
it has nothing to do with instanceof test

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Yes Prasad. I do not have brackets at Line 5. That's why Line 5 is fine. But Line 7 has brackets. Please advice why I am getting error if I keep the brackets at Line 7. What is wrong with that ?
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

mainly we use () brackets for casting the variable and value...here compiler gives error "Illegal start type" because we can't use instanceof operator when we doing casting...see more code like-



here we can not use instanceof operator because this gives us syntax error.


SCJP 6- 91%, IBM DB2, IBM RAD Certified
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

we write types in brackets for typecasting
here we are not casting anything and I don't think that it is valid also to use casting with instanceof operator
as instanceof will check for whole hierarchy
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
I am not sure how typecasting affects here..I used brackets in this code but do not see any issue.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

My take on this:
The use of (<Type>) is usually regarded as a typecast- So once the compiler encounters this- it expects a value to be followed.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

Saibabaa Pragada wrote:I am not sure how typecasting affects here..I used brackets in this code but do not see any issue.

of course there should not be any issue with brackets here
we are not typecasting here at all, this is just simple mathematics with brackets
 
Consider Paul's rocket mass heater.
 
subject: instanceof
 
Similar Threads
KB self test
once again instanceof
Getting the type of variable instance
Collections. Understanding why this compiles and runs ok.
short assignment.