Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Mon Jun 16, 2014 6:29 am
by Myzreal
Note under option #4 says that "the floating point suffices f, F or d, D are used only when using decimal system or hexadecimal and not while using binary".
However, hexadecimal numbers make use of the F character as a digit. How does the compiler know when we meant to use F as a digit and when as a floating point suffix?
Will the number 0x1f be considered integer 31 or float 1.0f?
Re: About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Mon Jun 16, 2014 8:38 am
by admin
Integer 31. Compiler has no issues figuring that out because any number starting with 0x is in binary and since there is no 'p' or 'P' in the number, it is a decimal. To write a floating point number is binary, there must be a 'p' or 'P' in the number. Please see this for full details:
http://docs.oracle.com/javase/specs/jls ... jls-3.10.2
BTW, 'p' and 'P' are not required for the exam.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Tue Nov 04, 2014 5:38 am
by Kevin_C
Why are the suffices with binary only invalid for floating points? So, why is this invalid:
but this valid:
Also, is there a difference between lower-case and upper-case? Or is f and F the same, d and D the same, l and L the same?
Re: About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Tue Nov 04, 2014 12:54 pm
by admin
That is how the language designers designed this feature. There is no technical limitation that prevents it.
There is no difference between lower and upper case.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Sun Jan 25, 2015 12:19 pm
by gparLondon
Hi,
What else should we know about octal, hexadecimal and binary number?
1>I mean should be know the output of the program when they are written?
example :float hexa=0x01dfeacL; System.out.println(hexa);
2>Should we know about the range they take?
example:byte b = 0b1100110011; // Type mismatch: cannot convert from int to byte
Thanks,
GPAR
Re: About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Sun Jan 25, 2015 9:24 pm
by admin
No, don't worry about ranges. Just knowing how to write numbers in binary, octal, and hex is enough. No need to translate from one format to another either.
Re: About Question enthuware.ocajp.i.v7.2.1377 :
Posted: Mon May 18, 2015 8:52 am
by scranen
Maybe the explanation could be clearer on the difference between integer literals in hexadecimal format and floating point literals in hexadecimal format (recognizable by the 'p' or 'P' exponent indicator).
Code: Select all
float f1 = 0xffff_ffff; // int literal, assigned to a float
float f2 = 0xffff_ffffL; // long literal, assigned to a float
float f3 = 0xffff_ffff_ffff_ffffL; // Ditto
float f4 = 0xffff_ffff_ffff_ffff; // Not a valid int literal, because the specified number does not fit in an int!
float f5 = 0xffff_ffffp0f; // float literal, assigned to a float
float f6 = 0xffff_ffffp0; // double literal, assigned to a float: Possible loss of precision!
float f7 = 0xffff_ffff_ffff_ffffp0f; // (very large) float literal, assigned to a float
It is useful to know that hexadecimal integer literals (without the 'p') are interpreted as two's complement, whereas floating point literals are interpreted as unsigned numbers.
Code: Select all
System.out.println(f1); // prints -1.0
System.out.println(f2); // prints 4.2949673E9
System.out.println(f3); // prints -1.0
System.out.println(f5); // prints 4.2949673E9
System.out.println(f7); // prints 1.8446744E19