Binary Literal in JDK 7 [OCAJP]
posted 2 years ago
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
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
posted 2 years ago
Hi,
A warm welcome to the CodeRanch!
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
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
posted 2 years ago
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,
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,
posted 2 years ago
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 floatingpoint 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!
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 floatingpoint literals,...
Now let's apply the general rule to all 3 variables:
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
shailu sai
Greenhorn
Posts: 5
Happiness is not a goal ... it's a byproduct of a life well lived  Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/ThreadBoostfeature
