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

# Assignment of hex and binary literals

Ranch Hand
Posts: 386
12
• Number of slices to send:
Optional 'thank-you' note:
I'm a bit confused as to why some of these assignments are allowed, if anyone minds straightening me out please!

line 1: allowed as it's a binary representation of an integer literal widened to float
line 2: binary can't represent float literals? (enthuware)
line 3: hexadecimal can - so the f is a suffix, not a hexadecimal value?
line 4: but this line makes me think it's a hexadecimal value - how can a float literal be assigned to an int implicitly?

hopefullly this is outside the scope of the exam and i can go back to my revision

Nick

Ranch Hand
Posts: 182
18
• 1
• Number of slices to send:
Optional 'thank-you' note:
I havent seen the enthuware question or answer.
I think line 2 doesnt work because, you cannot have values like' f' in a binary representation. you can have only 0 or 1.If you remove 'f' it should work.

and I think in line 3 and line 4,'f' must be hexadecimal value only. If we calculate the hexadecimal value for 0x1000f

hexadecimal 'f' represents 15. then ( here ^ represents power and not bitwise exclusive OR)

1*(16^4)+0*(16^3)+0*(16^2)+0*(16^1)+15*(16^0) = 65536+15=65551

If you print these values in line 3 and line 4. it gives

Sheriff
Posts: 11604
178
• 1
• Number of slices to send:
Optional 'thank-you' note:

nick woodward wrote:I'm a bit confused as to why some of these assignments are allowed, if anyone minds straightening me out please!

I think you get a bit carried away with the additional complexity of the hex and binary literals and you are simply forgetting the basics

nick woodward wrote:line 1: allowed as it's a binary representation of an integer literal widened to float

Correct!

nick woodward wrote:line 2: binary can't represent float literals? (enthuware)

The suffix 'f' (or 'F') is not an allowed character in the binary representation. That's why you'll get a compiler error.

nick woodward wrote:line 3: hexadecimal can - so the f is a suffix, not a hexadecimal value?

It's similar with line1: an int value which is widened to a float. If you print the values, you'll notice that f is part of the value and it's not the suffix to indicate a float

nick woodward wrote:line 4: but this line makes me think it's a hexadecimal value - how can a float literal be assigned to an int implicitly?

For exactly the same reason as line3: f is part of the value and it's not the suffix to indicate a float

Hope it helps!
Kind regards,
Roel

Roel De Nijs
Sheriff
Posts: 11604
178
• 1
• Number of slices to send:
Optional 'thank-you' note:

Ramya Subraamanian wrote:I think line 2 doesnt work because, you cannot have values like' f' in a binary representation. you can have only 0 or 1.If you remove 'f' it should work.
...
and I think in line 3 and line 4,'f' must be hexadecimal value only.

On both cases you are spot-on! Have a cow for not only providing an excellent answer but also faster than me

Ranch Hand
Posts: 175
17
• 1
• Number of slices to send:
Optional 'thank-you' note:

Ramya Subraamanian wrote:...you can have only 0 or 1

Not quite right. An l or L suffix is allowed.

In Java SE 7, the integer types (byte, short, int, and long) can also be expressed using the binary number system.

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:
And I like to mention one very important tip: you can only express integer literals with the number systems binary, octal and hexadecimal (so no floating-point literals).

It seems the suffix 'l' (or 'L') is allowed in any of the number systemsWithout the suffix 'L' all three lines would fail to compile because the value is outside the range of int (but not outside the range of long).

But for the suffix 'f' (or 'F') it's a different storyOnly flt2 compiles successfully, both flt1 and flt3 give at least one compiler error. The value of flt1 is outside the range of int (so the suffix 'F' is part of the value). For flt3 you'll get two (related) compiler errors: value outside the range of int and syntax error on token 'F'.

And since we are having so much fun, let's try the same for the suffix 'd' (or 'D')The results are exactly the same as with the suffix 'f' (or 'F'). Yay, consistency!

Hope it helps!
Kind regards,
Roel

PS. I think this will definitely be outside the scope of the OCAJP exam. I even can't remember if I got a question on the actual exam using different number systems.

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

Joe Bishara wrote:

Ramya Subraamanian wrote:...you can have only 0 or 1

Not quite right. An l or L suffix is allowed.

That's indeed spot-on! And you truely deserves a cow for posting that at exactly the same time as me. I think you have better odds for winning the lottery

Ramya R Subramanian
Ranch Hand
Posts: 182
18
• Number of slices to send:
Optional 'thank-you' note:

Not quite right. An l or L suffix is allowed.

oh yes, it does work with suffix L or l.

it is like widening primitive conversion. from long to float and long to double.

and Narrowing primitive conversion

and long with L suffix

nick woodward
Ranch Hand
Posts: 386
12
• Number of slices to send:
Optional 'thank-you' note:
yeah, it seems that i somehow got confused by the answer:

Test 5, question 30

float x = 0b10_000f; This is invalid because the floating point suffices f, F or d, D are used only when using decimal system or hexadecimal and not while using binary.

to me this suggests that the f as a suffix can be used with hexadecimals. but the answer is not distinguishing between being a suffix, and just being allowed at the end of the hex number as a normal value.

however, from what you're saying Roel, it seems the distinction is when the literal being assigned is greater than the max range of an integer, and that this isn't possible with binary numbers, but is with others.

think i'm going to have to search for a thread on the basics again, just to make sure i've got them sorted.

thanks for your help, both of you!

Nick

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

nick woodward wrote:to me this suggests that the f as a suffix can be used with hexadecimals. but the answer is not distinguishing between being a suffix, and just being allowed at the end of the hex number as a normal value.

You definitely can use 'f' (or 'F') and 'd' (or 'D') in a hexadecimal number, because those are valid characters in the hexadecimal number system. But you can not use them as the suffix to convert the default type of an integer literal (which is - as you definitely know - int) to float or double. So if your hexadecimal number is outside the range of an int, you can only use the suffix 'l' (or 'L') to convert the int literal to a long literal. Illustrated in the following code snippet

nick woodward wrote:however, from what you're saying Roel, it seems the distinction is when the literal being assigned is greater than the max range of an integer, and that this isn't possible with binary numbers, but is with others.

For binary numbers it's different: 'f' (or 'F') and 'd' (or 'D') or not valid characters in the binary number system. So strictly speaking you could use them as the suffix to convert an int literal to a float or double. But it's not allowed for binary numbers! When you use one of these characters in a binary literal, you'll get a compiler error. So if your binary number is outside the range of an int (which is possible), you can only use the suffix 'l' (or 'L') to convert the int literal to a long literal. illustrated in the following code snippet

Hope it helps!
Kind regards,
Roel

nick woodward
Ranch Hand
Posts: 386
12
• Number of slices to send:
Optional 'thank-you' note:
oh ok, yes it does, thanks!

do you see where my confusion came from in that enthuware answer though? it does seem to suggest that 'f' and 'd' can be used as a suffix in hex.

anyway, good to know about converting an int literal to a long literal. apreciated!

Nick

Roel De Nijs
Sheriff
Posts: 11604
178
• 1
• Number of slices to send:
Optional 'thank-you' note:

nick woodward wrote:do you see where my confusion came from in that enthuware answer though? it does seem to suggest that 'f' and 'd' can be used as a suffix in hex.

Yeah! I can definitely see your confusing. And for some reason the octal system is missing too in that statement (which allows using 'f'/'F' and 'd'/'D' as a suffix. Illustrative code snippetSo they probably should rephrase that statement like "This is invalid because the floating point suffices f, F or d, D are used only when using decimal or octal system, not while using binary. And f, F or d, D can (of course) be used in hexadecimal system too, but then they are interpreted as a valid character (value 15 and 13 respectively) not as a suffix." (or something similar of course).

Hope it helps and that statement is less confusing!
Kind regards,
Roel

Enthuware Software Support
Posts: 4829
52
• 1
• Number of slices to send:
Optional 'thank-you' note:

nick woodward wrote:yeah, it seems that i somehow got confused by the answer:

Test 5, question 30

float x = 0b10_000f; This is invalid because the floating point suffices f, F or d, D are used only when using decimal system or hexadecimal and not while using binary.

to me this suggests that the f as a suffix can be used with hexadecimals. but the answer is not distinguishing between being a suffix, and just being allowed at the end of the hex number as a normal value.
Nick

Nick,
I just checked this question (qid enthuware.ocajp.i.v7.2.1377 ) and the complete explanation that is provided with this option is as follows:

This is invalid because the floating point suffices ///f, F, d, and D/// are used only when using decimal system and not while using binary. However, since f is a valid digit in hexadecimal system, a hex number may end with an f although it will not be interpreted as float but as the digit f. Thus, float x = 0x10_000f; and float x = 10_000f; are valid because they are written in hex and decimal respectively but float x = 0b10_000f; is invalid because is written in binary.

Note that a floating point number cannot be written in Octal. Therefore, float x = 010_000f; is valid but it is not octal even though it starts with a 0. It is interpreted in decimal.

So it does make it quite clear that an f at the end of a hex number is interpreted as a digit and not as a floating point suffix.

HTH,
Paul.

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

Paul Anilprem wrote:

Note that a floating point number cannot be written in Octal. Therefore, float x = 010_000f; is valid but it is not octal even though it starts with a 0. It is interpreted in decimal.

That's a really nice note Have a cow! I didn't know that (since I didn't print these values). But now I have and that's indeed true! Not that I doubted about correctness of Enthuware's explanation, but it's always a (very) good idea to try it out yourselfOutput: 4096 10000.00 10000.00

Kind regards,
Roel

nick woodward
Ranch Hand
Posts: 386
12
• Number of slices to send:
Optional 'thank-you' note:
Thanks Paul, I think this is a case of me needing to update the software, because the line

However, since f is a valid digit in hexadecimal system, a hex number may end with an f although it will not be interpreted as float but as the digit f

does not appear in my answer (it is otherwise exactly the same).