Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817
Moderator: admin
TwistedLizard
Posts: 57 Joined: Sat Mar 01, 2014 1:48 pm
Contact:
Post
by TwistedLizard » Sun Nov 06, 2016 10:56 am
This seems sneaky!
According to the jls:
https://docs.oracle.com/javase/specs/jl ... ml#jls-3.8
a keyword cannot be used as an identifier, yet here is a snippet that does exactly that.
Code: Select all
class Test {
public static void doIt( String String ) // 1
{
int i = 10;
i : for (int k = 0 ; k< 10; k++) // 2
{
System.out.println( String + i); //3
if( k*k > 10) continue i; //4
}
}
public static void main(String[] args){
Test.doIt("X");
}
}
I went straight for the
It will not compile because of line 1.
It will not compile because of line 3.
options.
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sun Nov 06, 2016 9:21 pm
Why do you think it does exactly that?
TwistedLizard
Posts: 57 Joined: Sat Mar 01, 2014 1:48 pm
Contact:
Post
by TwistedLizard » Mon Nov 07, 2016 2:50 am
public static void doIt( String String ) // 1
I'd read this as doIt() requiring a single parameter of type String using the variable name of String. I thought using a reserved word as an identifier was illegal.
However, I've just looked up the list of java's keywords. Although the primitive datatypes appear on that list, String or any of the wrapper types do not.
I'm still surprised it's allowed.
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Mon Nov 07, 2016 10:38 am
Very good.
String is just a name of a class. It is neither a reserved name or a keyword. It has no special meaning.
HTH,
Paul.
TwistedLizard
Posts: 57 Joined: Sat Mar 01, 2014 1:48 pm
Contact:
Post
by TwistedLizard » Mon Nov 07, 2016 4:31 pm
Thanks Paul.
I'm finding the enthuware question bank & explanations very useful.
Users browsing this forum: Google [Bot] and 8 guests