Page 1 of 1

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

Posted: Sun Nov 06, 2016 10:56 am
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.

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

Posted: Sun Nov 06, 2016 9:21 pm
by admin
Why do you think it does exactly that?

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

Posted: Mon Nov 07, 2016 2:50 am
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.

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

Posted: Mon Nov 07, 2016 10:38 am
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.

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

Posted: Mon Nov 07, 2016 4:31 pm
by TwistedLizard
Thanks Paul.

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