Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Name of variables  RSS feed

 
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I met an example where a name of variable is named String.

I ever read  that you can't use keywords from Java language as name of variables.

I am very confused because the compiler don't say anything.

String String = "anda"; is legal.

 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'String' is not a reserved word, so it can be used as a variable name (not recommended, of course)
 
Marshal
Posts: 67163
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because it's legal doesn't mean it's a good idea
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Types of data are not reserved.

But on OCA exam I met some examples, and my response was wrong because I said: don't compile.

I will suggest to Java community to make reserved all the date types.

:-)


 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:Types of data are not reserved.

But on OCA exam I met some examples, and my response was wrong because I said: don't compile.

I will suggest to Java community to make reserved all the date types.

:-)



data type or date type? what are you referring to in this?
If you are considering "String" as a data type, any class can become data type. You can not make every class reserved word.
I guess that is why primitive data types are considered as reserved word.
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, you don't need to guess at what is a reserved keyword...

https://en.wikipedia.org/wiki/List_of_Java_keywords

Henry
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Henry.

I don't know what are 50 reserved words in Java.

I knew that keyword statements are reserved word.

So you can define a variable named Boolean, Byte, Integer, Long and the compiler don't say anything.

Interesting....
 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:I don't know what are 50 reserved words in Java.


You should know them! Because invalid usage of a Java keyword will result in a compiler error and thus you have to select the "Compilation fails" answer option

And int or Integer as variable name definitely makes a difference And don't forget that Java is case-sensitive so case definitely matters!
 
Sheriff
Posts: 23867
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:So you can define a variable named Boolean, Byte, Integer, Long and the compiler don't say anything.



Yes. And you can create your own class named Cheese and then define a variable named Cheese. Should that be illegal too? Or should it only be illegal to use names of the (several thousand) classes in the standard API?

Here's the problem with that idea. With every new version of Java come a lot of new classes. So that could suddenly create a lot of new names which now are illegal to use as names of variables. This could make programs which used to be correct now not correct. And that would be a very bad thing. Oracle takes backwards compatibility (which means that releasing a new version of Java should not make existing code work differently) very seriously.
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel for your good example.

 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I write the line Integer int = 5; in my NetBeans editor I met the error compiler:

''Not a statement. Expected ;."

The error compiler is not helpful, I would like to said: You can't define name of variables as keyword Java.


Again the error compiler is nou useful, is confusing because ; is not missing.

Probably over years the compilers will be evaluated.

 
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reserved key words in Java usually start with  small case. If you are using an IDE for practice you can easily observe the color change when your write normal class (User defined or Platform provided) and key words. Anything you write after writing a class will be considered as a variable of that type(the class which you write). The same holds good for primitive data types also(int, long, char, byte, etc.,.). Hence you can differentiate through smaller case and upper case      
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:The error compiler is not helpful, I would like to said: You can't define name of variables as keyword Java.


That's related with how compilers are implemented. A lexical analysis is performed to verify if all your code is valid.

Dana Ucaed wrote:Again the error compiler is nou useful, is confusing because ; is not missing.


If you would use javac (instead of an IDE like NetBeans), you will notice that the compiler error shows where a semicolon was expected (which makes the error itself less confusing)
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But everybody use IDE.

Do all IDE generate error compiler confusing?

There is a IDE that show us the same error compiler as javac?

 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:But everybody use IDE.


That's not true! During the Java course at school I used Notepad, javac and java to compile and run applications.

Dana Ucaed wrote:Do all IDE generate error compiler confusing?


Different IDEs might generate different compiler error messages.

Dana Ucaed wrote:There is a IDE that show us the same error compiler as javac?


Why not simply use javac & java and get rid of the IDE while preparing for the OCAJP exam? For me using an IDE during the preparation for the certification exam is definitely a don't. Why? Very easy: on the actual exam you also don't have an IDE (and compiler) to help you verify and run code. You are all on your own. So my first advice would definitely be: get rid of your IDE, only use your favourite text editor, javac and java. Other opinions about using an IDE while preparing for the OCA exam can be found here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!