About Question enthuware.ocajp.i.v7.2.985 :
Moderator: admin
-
- Posts: 29
- Joined: Sat Jun 08, 2013 11:33 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.985 :
For this question, why does 'float f = 0x0123;' work but not 'float f = 43e1;' ?
why is 43e1 a double but 0x0123 is not?
Thank you for your help!
why is 43e1 a double but 0x0123 is not?
Thank you for your help!
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
That is just a rule of the language. There is no particular reason for it except that the designers designed it that way. They might have had some reason but I haven't seen it.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sat Jun 08, 2013 11:33 pm
- Contact:
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
I thought 43e1 is scientific notation meaning 43 * 10 to the power of 1... Why is it a double and not 430?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
That is just how Java language was designed. A number with e notation is considered a double. Please check Java Language Specification for more details.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 25
- Joined: Wed Dec 17, 2014 9:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
To note:
this line will compile, because 10 is of integer type, default type for integer literals.
float f1 = 10;
But this one won't compile, because 10.0 is of double type, which is default type for floating-point literals. And we try to cast double to float -> narrowing casting:
float f2 = 10.0;
this line will compile, because 10 is of integer type, default type for integer literals.
float f1 = 10;
But this one won't compile, because 10.0 is of double type, which is default type for floating-point literals. And we try to cast double to float -> narrowing casting:
float f2 = 10.0;
-
- Posts: 25
- Joined: Wed Dec 17, 2014 9:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
You should apply a general rule - real numbers are double by default, and integer numbers are int by default.nickeasyuptech wrote:For this question, why does 'float f = 0x0123;' work but not 'float f = 43e1;' ?
why is 43e1 a double but 0x0123 is not?
Here 0x0123 represents an integer number "291", that is why it compiles as well (implicit casting int to float).
How recognize real number in hexadecimal form? Look for decimal points in it:
0x123.456p0 - it is equal to "291.271", than it's double by default, and statement:
float f = 0x123.456p0;
will not compile, because of narrowing casting double to float. To say compiler that this is a float, just put f or F in the end (as with ordinary real numbers in decimal form), and than it will compile:
float f = 0x123.456p0F;
-
- Posts: 2
- Joined: Sun Jul 17, 2016 7:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
The explanation for this problem states the following: "implicit narrowing is permitted only among byte, char, short, and int." However, it seems to me that ALL narrowing must be done explicitly, as the following two lines of code throw a type mismatch error.
int i = 1;
byte b = i;
Is the explanation just wrong, or am I not understanding it?
int i = 1;
byte b = i;
Is the explanation just wrong, or am I not understanding it?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
The explanation is correct but incomplete. Implicit narrowing is permitted only only among byte, char, short, and int but the right hand side also has to be a compile time constant. This point is covered in other questions on this topic.
In your case, i is a variable and is not a compile time constant. It will work if you make it final.
HTH,
Paul.
In your case, i is a variable and is not a compile time constant. It will work if you make it final.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Jul 17, 2016 7:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
A very interesting distinction. Thank you!
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
The explanation states: "Although the values in the option 1 and 2 are compile time constants"
For reference, Option 1 and 2 are the following
1. float f1 = 1.0;
2. float f = 43e1;
How are these compile time constants? From my research, they are just variables. Compile time constants are denoted by the final keyword, which is absent from both options.
Can you confirm whether a compile time constant requires the keyword final? The JLS https://docs.oracle.com/javase/specs/jl ... jls-4.12.4 does mention any condition in which this question in particular regarding the floats applies.
For reference, Option 1 and 2 are the following
1. float f1 = 1.0;
2. float f = 43e1;
How are these compile time constants? From my research, they are just variables. Compile time constants are denoted by the final keyword, which is absent from both options.
Can you confirm whether a compile time constant requires the keyword final? The JLS https://docs.oracle.com/javase/specs/jl ... jls-4.12.4 does mention any condition in which this question in particular regarding the floats applies.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
In an assignment statement, the right hand side of = is the value that is being assigned to the "variable" on the left hand side.
f1 is the variable.
1.0 is the value that f1 is being is assigned. It is this value that the statement is talking about.
1.0 is a compile time constant.
What you are talking about is a compile time constant "variable". For example, final float f1 = 1.0;. Here, f1 is a compile time constant because its value cannot change.
f1 is the variable.
1.0 is the value that f1 is being is assigned. It is this value that the statement is talking about.
1.0 is a compile time constant.
What you are talking about is a compile time constant "variable". For example, final float f1 = 1.0;. Here, f1 is a compile time constant because its value cannot change.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Jul 25, 2017 1:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
Why do these float values not need the f at the end of the value?
For example:
float f = 4f;
float f = -1f;
etc...
Won't java try and "correct" these values to be double values without the f?
Maybe someone can point me in the direction of a better article to understand why these declarations are valid.
For example:
float f = 4f;
float f = -1f;
etc...
Won't java try and "correct" these values to be double values without the f?
Maybe someone can point me in the direction of a better article to understand why these declarations are valid.
-
- Posts: 2
- Joined: Tue Jul 25, 2017 1:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
Well, I found a really thorough article on this topic over on codingranch:
https://coderanch.com/t/238351/certific ... tion-float
https://coderanch.com/t/238351/certific ... tion-float
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
how do i know that 0x0123 can be float and 43e1 is not? Is there any table or rule to know?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.985 :
For a floating point number either a decimal point, an exponent, or a float type suffix (i.e. f or d ) are required. So, you can see that 0x123 cannot be a floating point number while 43e1 is.
You may want to check out section 3.10.2 of JLS for the details.
You may want to check out section 3.10.2 of JLS for the details.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 43 guests