Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keywords confusion

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 49 reserved keywords and 3 reserved literals ( null,true and false ) in 1.4 , that we can't use in our code as an identifier for variable , method or class etc .

My question is , 1.5 also has the same 52 reserved words ? ( although in my opinion , sun can't reserve any new word now for keyword because it might break code of lots of people ( if they have used that name in their project ) , but just wanted to confirm )

I read some online resources mentioned in this site only , they are mentioning more than 52 reserved words , what is truth ?

Thanks a lot .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 5 has added enum.

I think that enum is only reserved if you have "-source 1.5" in your javac command.
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:

My question is , 1.5 also has the same 52 reserved words ? ( although in my opinion , sun can't reserve any new word now for keyword because it might break code of lots of people ( if they have used that name in their project ) , but just wanted to confirm )


Why can't sun add new keyword, they added assert keyword when 1.4 was released.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that assert was an optional reserved word in Java 1.4 and is a default reserved word in Java 1.5. I expect that enum will be a default reserved word in Java 1.6.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Mike , If suppose any company moves their project to 1.5 ( they have made in 1.3 ) and they have used assert as their costom identifier , then does they have to change their code ?? ( although possibilities are very low )

enum is the only keyword added by 1.5 ?

please comment anybody ..

thanks a lot .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac 1.5 supports "-source 1.3" and "-source 1.4" as well as "-source 1.5". That protects legacy code.

BTW, the default is "-source 1.5", so beware if you may have used enum as an identifier in your code.

enum is the only new reserved word in Java 1.5.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Mike .
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't sun add new keyword, they added assert keyword when 1.4 was released.

They can add keywords, but they try very hard not to, because it can break code for people who used the new keyword as an identifier. Indeed, I remember seeing code like this:

Such code now breaks. Of course, most people who used Enumerations have moved on to iterators by now, but still...

I imagine Sun wanted to avoid this situation, but they couldn't find a better solution to support enumerated types without creating a new keyword. So they bit the bullet, and created the keyword.

In contrast, the enhanced for loop might have looked nicer if they'd chosen a syntax like this:

or

However these would have required new keywords to be created. (And I personally use "in" as an identifier for an InputStream quite frequently.) Instead, Sun was able to find an alternative syntax:

This isn't quite as easy to understand the first time you see it - but it doesn't require any new keywords. So we see that Sun is willing to create new keywords on rare occasions, but they usually choose not to, if they can avoid it.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic