About Question com.enthuware.ets.scjp.v6.2.242 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.242 :

Post by TwistedLizard »

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:

Re: About Question com.enthuware.ets.scjp.v6.2.242 :

Post by admin »

Why do you think it does exactly that?

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.242 :

Post by TwistedLizard »

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:

Re: About Question com.enthuware.ets.scjp.v6.2.242 :

Post by admin »

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:

Re: About Question com.enthuware.ets.scjp.v6.2.242 :

Post by TwistedLizard »

Thanks Paul.

I'm finding the enthuware question bank & explanations very useful.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests