• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

numeric literals that include comma, beware of this

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is for all who are still prepareing for SCJP :
We all know that integer literals that include comma won't compile in java, but look at the following code.

class Test {
public static void main(String[] args) {
int f = 0x20,df;
System.out.println(f);
}
}

It WILL compile.

You know why?? Because, first the literal is a hexadecimal one, which is valid. Second d and f are also valid in hexadecimal. BUT, and this is the tricky one, here df is another variable and not a constant (which can go along with 0x20 as 0x20df). Got it. I guess so.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


int f = 0x20,df;


That's kinda kewl. It took me awhile to digest it until I realized it was like:
int f = 0x20;
int df;

A very similar (but much less confusing) example is


Which is creating two variables x and y, but initializing x.

Tx for the tip
 
Gurpreet Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point here is, that integer literals generally don't include commas. But when used like given above, they surely can include comma, but that comma will not be a part of the literal. It will indicate a new variable of int.

Sounds like a Paradox.

It is
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get part way to resolving your paradox by realizing that the , is used as a seperator in Java. Coming from C++ (where the , is also an operator) I had to stumble through this.

In Java, the following won't compile:



In C++, the code prints 10 times because operator,() evaluates to its right-most expression.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gurpreet Singh:
The point here is, that integer literals generally don't include commas. But when used like given above, they surely can include comma, but that comma will not be a part of the literal. It will indicate a new variable of int.

Sounds like a Paradox.

It is



The "paradox" is only in the way you describe this.
When I read int f = 0x20,df; I saw the comma and said this is a definitioon of two variables. One is explicitly initialized, the other not. The comma is in no way a hexadecimal digit (0123456789ABCDEF) so how can it be ever thought to be part of the literal?
reply
    Bookmark Topic Watch Topic
  • New Topic