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

Moderator: admin

Post Reply
troydh53
Posts: 5
Joined: Fri Sep 11, 2015 8:06 am
Contact:

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

Post by troydh53 » Wed Sep 30, 2015 1:11 pm

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?

troydh53
Posts: 5
Joined: Fri Sep 11, 2015 8:06 am
Contact:

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

Post by troydh53 » Wed Sep 30, 2015 3:35 pm

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.

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

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

Post by admin » Thu Oct 01, 2015 12:15 am

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.

Nethaji.M
Posts: 2
Joined: Sun Mar 13, 2016 8:25 pm
Contact:

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

Post by Nethaji.M » Sun Mar 13, 2016 8:38 pm

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.

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

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

Post by admin » Sun Mar 13, 2016 10:11 pm

The answer and explanation are correct. There is no compilation error.
-Paul.
If you like our products and services, please help us by posting your review here.

Nethaji.M
Posts: 2
Joined: Sun Mar 13, 2016 8:25 pm
Contact:

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

Post by Nethaji.M » Mon Mar 14, 2016 6:24 pm

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.

zoharch
Posts: 4
Joined: Fri Sep 29, 2017 3:34 pm
Contact:

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

Post by zoharch » Sun Oct 15, 2017 8:01 am

Dear
Why int x9 = 0_52; is Ok (octal)
and int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix

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

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

Post by admin » Sun Oct 15, 2017 10:13 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests