• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary Literal in JDK 7 [OCAJP]

 
Ashish Krishnan
Greenhorn
Posts: 2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was referring to the Oracle Certified Associate Java Programmer I by Mala Gupta and was solving the few questions.
I had a small doubt on a topic of Liternal for Numerical Datatypes.

Here is the Question.


Determine which of these does initialization job correctly:
long var1 = 0_100_267_760;
long var2 = 0_x_4_13;
long var3 = 0b_x10_BA_75;

The answer marked by the book are : var1 and var3



Edit:
But according to me, the answer should be only var3 as the underscore cannot follow directly after a representation like 0b, 0x, 0B,0X or 0 (used for octal indication).

**
But according to me, the answer should be only var3 as the underscore can follow a representation like 0b, 0x, 0B,0X or 0 (used for octal indication).
and var 1 initialization has 0_100_267_760 which can be a Octal Number?



Am I wrong here?
The answer can also include var1 if and only if liternal is not octal. But how can we come to know its not octal or is octal? ...
Rather confused in all this,

Regards,


Ashish

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A warm welcome to the CodeRanch!

Ashish Krishnan wrote:But according to me, the answer should be only var3 as the underscore can follow a representation like 0b, 0x, 0B,0X or 0 (used for octal indication).
and var 1 initialization has 0_100_267_760 which can be a Octal Number?

What's the reason according to you why var1 is wrong? Because according to your own explanation (the underscore can follow a representation like 0b, 0x, 0B,0X or 0) the underscores are used correctly.

Kind regards,
Roel
 
Ashish Krishnan
Greenhorn
Posts: 2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sir for a warm welcome.

Sorry I did a typo over there.
I should have written ...the underscore should not directly follow after a 0X, 0x, 0b, 0B or 0 .

For example: var1 = 0x_1223_22 (this is wrong to me)
it could be var 1 = 0x122_3_22

Hence, 0_2344 shouldnt be correct instead for a octal indication using literals : 02_4

So accoding to the question the answer can be var1 if and only if liternal is not octal. But how can we come to know its not octal or is octal? ...
Rather confused in all this,
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take the 3 variables again to start from:
long var1 = 0_100_267_760;
long var2 = 0_x_4_13;
long var3 = 0b_x10_BA_75;


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,...

Now let's apply the general rule to all 3 variables:
  • var1: all underscores between digits, so that's ok.
  • var2: underscore between 0 and x, so that's not between digits and thus illegal.
  • var3: this is a very poor example, because the starting 0b indicates a boolean literal but only 0 and 1 are allowed, so this one is already invalid because it uses illegal digits/letters. But the underscore is also behind 0b and thus violates the general rule.


  • So the correct answer should only be var1, var2 and var3 are invalid! Can you share which question from the book it was?

    Hope it helps!
     
    shailu sai
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,

    This question is from Chapter 2 Working with Java data types.(Mala Gupta)

    I am also bit confused with Twist in the tale 2.1 part 2. Could you please clarify us about all the primitive values given in that question?

    Thanks,
    shailu
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    swathi pathi wrote:I am also bit confused with Twist in the tale 2.1 part 2. Could you please clarify us about all the primitive values given in that question?

    Just start a new topic with the code you have doubt/question about.
     
    shailu sai
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Roel.!

    I started a new thread.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic