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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

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 »

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 »

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.

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

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

Post 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
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 »

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.

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

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

Post by admin »

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 »

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 »

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

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

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

Post 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!
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 3 guests