programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Twist in the Tale 2.1(part 2)

shailu sai
Greenhorn
Posts: 5
Twist in the Tale 2.1

Here’s another quick exercise—let’s define and initialize some long primitive variables that use underscores in the literal values assigned to them. Determine which of these does this job correctly:

long var1 = 0_100_267_760;
long var2 = 0_x_4_13;
long var3 = 0b_x10_BA_75;
long var4 = 0b_10000_10_11;
long var5 = 0xa10_AG_75;
long var6 = 0x1_0000_10;
long var7 = 100__12_12;

Answer is given as Only var1,var6 and var7 correctly define a literal integer value. Could someone explain this?

Paweł Baczyński
Bartender
Posts: 2062
44
In this topic you can find an answer:
Roel De Nijs wrote:The general rule is: You can place underscores only between digits. There are of course a few exceptions, like not at the beginning of the end of a number, not adjacent to the decimal point in floating-point literals,...

You can find an explanation for var1, var2 and var3 there.

var4 is illegal because there is an underscore between b and 1.
var5 is illegal because G is illegal digit in hex literal.

Roel De Nijs
Sheriff
Posts: 11338
177
I agree with Pawel here:
• var4 is violating the general rule
• var5 is a tricky one: you'll be focusing on the underscore and its position. Nothing wrong there, all underscores between digits. But the problem is the G, for hexadecimal numbers only 0-9 and A-F are allowed as "digits"

• Hope it helps!

shailu sai
Greenhorn
Posts: 5
Thanks Pawel.!

Roel De Nijs
Sheriff
Posts: 11338
177
• 2
Nice to know: instead of making a "thank you" post, you could also +1 the post(s) which you liked. It's easier, faster and other ranchers will see immediately which are the "starred" posts.

shailu sai
Greenhorn
Posts: 5
Roel,

I am new to posting the topics and replies. Lot of my doubts were clarified from earlier posts. I will make sure to click +1 to the posts which were useful. Thank you for this awesome platform..

Thanks,
Shailu