About Question enthuware.ocajp.i.v7.2.1377 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Myzreal
Posts: 1
Joined: Mon Jun 16, 2014 6:25 am
Contact:

About Question enthuware.ocajp.i.v7.2.1377 :

Post 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?

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1377 :

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

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1377 :

Post by Kevin_C »

Why are the suffices with binary only invalid for floating points? So, why is this invalid:

Code: Select all

float x = 0b100f;
but this valid:

Code: Select all

long x = 0b100L;
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?

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1377 :

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

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1377 :

Post 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

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1377 :

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

scranen
Posts: 12
Joined: Thu May 07, 2015 8:55 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1377 :

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests