• 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

Twist in the Tale 2.1(part 2)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Pawel.!
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    reply
      Bookmark Topic Watch Topic
    • New Topic