Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Mon Jun 08, 2015 11:23 am
by suresh
Not able to understand,1)String String,2)Integer Integer are valid declarations whereas int int is not.
Could you pl. explain?.

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Mon Jun 08, 2015 9:04 pm
by admin
int is a keyword, therefore it cannot be a name of a variable. You should try putting it in code and observe the error message.

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Wed Feb 17, 2016 10:23 am
by realzakaria
String and Integer are keywords too????? (confused!)

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Wed Feb 17, 2016 10:37 am
by admin
realzakaria wrote:String and Integer are keywords too????? (confused!)
No, they are not. Here is a list of keywords:
https://docs.oracle.com/javase/tutorial ... words.html

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Tue Nov 08, 2016 6:21 am
by nikitos
It's like member(local variable) shadowing?)

I mean that it's possible to shadow field of the class:

Code: Select all

class A{
int i=1;
void method(){
//shadowing here:
int i=2;
}
}

So I think it does not correlate with my meaning.
And user can have every name that is not from the "Java Building Block" (abstract, assert, boolean, etc)/

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Tue Nov 08, 2016 8:27 am
by admin
Sorry, I am not sure I understand your question. What is like member(local variable) shadowing?
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Mon Nov 14, 2016 4:21 pm
by jax502
Great question! This one got me. Yes String String is indeed a valid syntax. Probably not good coding practice but definitely valid. I need to watch out for these types of questions in the exam.

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Fri Nov 18, 2016 1:25 am
by mj.anjuthan
Is there any possibility for an ambiguity (for compiler) while using a variable name same as a class name (say using "String")? (I'm not able to think of any.)

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Fri Nov 18, 2016 2:12 am
by admin
No, there is no ambiguity.

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Wed Jan 25, 2017 4:26 pm
by Jose Manuel
I've been playing with the idea of using the same name for a class and for an instance of the class as follows:

class Person{

static void staticMethod(){}
void instanceMethod(){}

public static main void(String[] args){

Person Person = new Person(); //this compiles OK

//The problem comes here

Person.staticMethod(); // Ok, no problem at all
Person.instanceMethod(); // doesn't compile

}

I think Eclipse offers preference to the class before the instance.

¿Could anyone clarify it?

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Wed Jan 25, 2017 9:59 pm
by admin
Could be a problem with Eclipse. Always use the command line and Oracle's Javac while practicing for the exam.

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Sat Dec 05, 2020 8:26 am
by Deleted User 6318
JoseManuel, it compiles for me in IntelliJ (besides the fact that I needed to swap main and void - did it really compile for you like that)?

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Sat Dec 05, 2020 8:27 am
by Deleted User 6318
I see beginIndex can be the same as endIndex - then the result is just empty. This is quite a paradox for me, because we include and exclude the same index at the same time.
I get how it work, but I think maybe it would be good to add this in the explanation, or make another question with such a case (if there isn't already one).

Re: About Question enthuware.ocajp.i.v7.2.1022 :

Posted: Sat Dec 05, 2020 8:50 am
by admin
Good point.
thanks for the feedback!