About Question enthuware.ocajp.i.v7.2.1378 :
Moderator: admin
-
- Posts: 5
- Joined: Fri Sep 11, 2015 8:06 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1378 :
I thought that when you initialize a long or a float, you have to put an L, l or f after the number. How are we supposed to know when those letters are required?
-
- Posts: 5
- Joined: Fri Sep 11, 2015 8:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
Using IDE, I think I figured it out.
5.0 is a double and can't go into a float, so float f = 5.0 won't work. Must be 5.0f.
You can put an int into a long, so long L = 123 is just putting an int into a long, no problem. But can't put long into int so int i = 123L won't work.
5.0 is a double and can't go into a float, so float f = 5.0 won't work. Must be 5.0f.
You can put an int into a long, so long L = 123 is just putting an int into a long, no problem. But can't put long into int so int i = 123L won't work.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
You got it. But to fill any holes in your understanding, you should go through at least the "implicit narrowing" part described here: https://docs.oracle.com/javase/specs/jl ... jls-5.html or http://www.coderanch.com/t/380722/java/ ... -Narrowing
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Mar 13, 2016 8:25 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
Dear Admin,
int i = 1___3;
long l = 1_3;
float f = 3.234_567f;;
System.out.println(i+" "+l+" "+f);
while compile the code, it will throw compilation error
but your answer is : 13 13 3.234_567f;
I can't understand how you are define these, can you explain me?
Thanks & Regards,
Nethaji M.
int i = 1___3;
long l = 1_3;
float f = 3.234_567f;;
System.out.println(i+" "+l+" "+f);
while compile the code, it will throw compilation error
but your answer is : 13 13 3.234_567f;
I can't understand how you are define these, can you explain me?
Thanks & Regards,
Nethaji M.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
The answer and explanation are correct. There is no compilation error.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Mar 13, 2016 8:25 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
Nethaji.M wrote:Dear Admin,
int i = 1___3;
long l = 1_3;
float f = 3.234_567f;;
System.out.println(i+" "+l+" "+f);
while compile the code, it will throw compilation error
but your answer is : 13 13 3.234_567f;
I can't understand how you are define these, can you explain me?
Thanks & Regards,
Nethaji M.
Dear Admin,
I found the issue, i am compiling the code in java 6, but's working in java 7.
Regards,
Nethaji M.
-
- Posts: 4
- Joined: Fri Sep 29, 2017 3:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
Dear
Why int x9 = 0_52; is Ok (octal)
and int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
Why int x9 = 0_52; is Ok (octal)
and int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1378 :
It is just because those are the rules of the language. A number starting with 0 is considered to be an octal number and a number starting with 0x is considered a hexadecimal number. May be they disallowed 0_x because it is too confusing!
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 41 guests