Page 1 of 1

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

Posted: Wed Sep 30, 2015 1:11 pm
by troydh53
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?

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

Posted: Wed Sep 30, 2015 3:35 pm
by troydh53
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.

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

Posted: Thu Oct 01, 2015 12:15 am
by admin
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

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

Posted: Sun Mar 13, 2016 8:38 pm
by Nethaji.M
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.

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

Posted: Sun Mar 13, 2016 10:11 pm
by admin
The answer and explanation are correct. There is no compilation error.
-Paul.

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

Posted: Mon Mar 14, 2016 6:24 pm
by Nethaji.M
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.

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

Posted: Sun Oct 15, 2017 8:01 am
by zoharch
Dear
Why int x9 = 0_52; is Ok (octal)
and int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix

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

Posted: Sun Oct 15, 2017 10:13 pm
by admin
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!