Hi guys, while practiticing for the test I came across this question (found on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 5342-5345). Enthuware. Kindle Edition. ):
Which of the following declarations are valid? Select 3 options
I answered C,E and I was torn between B and D. Eventually I went for D as D felt smaller than B. I got it right, because apparently B is a double but, I wonder, are these kind of questions likely to be appearing on the exam? I thought that conversion wasn't a topic. If it is, or if it could appear in the exam, is there a way to quickly convert numbers?
If you have a computer with a JDK installed, then you can very quickly find the answer to this question yourself. Just write a very small program and try to compile it, and see which of these compiles and which ones give you an error.
Whether an assignment to a float is valid has nothing to do with if the value of the number is big or small. It's about the types.
The Java Language Specification specifies the rules about what types of values can be assigned to a different type of variable without casting or explicit conversion.
The literal values on the right side of the = in answers A and B are of type double. Converting these values to a float means you are doing a narrowing primitive conversion, which requires a cast. The cast is required because such a conversion throws away part of the value (bits are discarded), so, with a cast, you must confirm to the compiler that that's OK.
The literal values on the right side of the = in answers C, D and E are of type int. Converting these values to a float means that you are doing a widening primitive conversion, which does not require a cast.
Whether these questions will be on the exam, is something I don't know for sure.
I wonder, are these kind of questions likely to be appearing on the exam?
No. Exam doesn't ask you to convert between one number system value to another numbering system value . Hexadecimal , Octal, binary, Decimal number system values are considered as integer values in Java. Integer values can be automatically promoted to long, float,double types. Exam doesn't tests about data types range. Even If question contains those number system values , Keep in mind they are integer values and can be automatically promoted to long, float,double.
Hope it helps !
posted 3 years ago
Thanks guys, but if all hex are ints why do they say that 43e1 is a double? If it's an int then an int can fit in a float so there is no problem
43e1 Stands for a double because "e" is a scientific notation that represent "times ten raised to the power of" (which would be written as "× 10n").
in other words, 43e1 = 43x10 and is followed by the value of the exponent 1.
This is a double because the notation is used to represent either a very big or a very small number this means in java it would be a double.
Michael Haile wrote:. . . because the notation is used to represent either a very big or a very small number . . . it would be a double.
That is logical, but not correct. It is a double because all floating‑point literals without a final f are doubles. 43e1 is 430.0, which is in the range for a float. The gory details are to be found in the Java® Language Specification (=JLS).
If I remember correctly, a “narrow” integer primitive can be assigned to from a compile‑time constant expression in the appropriate range for its target with an implicit narrowing conversion, but I don't think that applies to floating‑point numbers, nor int and long declarations.Both the right operands for the = operators are int literals in the range of their targets for assignment.
Looky! I'm being abducted by space aliens! Me and this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop