File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Literals doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Literals doubt" Watch "Literals doubt" New topic
Author

Literals doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
class MCZ16 {
public static void main (String[] args) {
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above


Hi All,
Answer for this question is "a". Why is that line 2 and line 4 compile successfully?. Since long is 64 bits how can it fit into float variable and how line 4 compiles by default it will be double. Please explain me this



Thanks All
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Sorry the answer is "d". But i have doubt in line 2. Explain me please.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

arora

Check below

byte--->short---->int--->long--->float--->double

char ------->int
you can do this
with out cast you can assign byte value to short
and short to int

in your case

float b = 1L; // 2
you are assigning the value of long to float
[ April 30, 2007: Message edited by: anil kumar ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks anil
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Nikhil,

Are you sure you know what you are asking for?

Anyway, compiler error will be generated at Line #4.
All other are fine.

Points:

1- By default the integral literal is int so it can be assigned to float
It means widening is done there. Option (a)

2- By default the floating point literal is double so if you want to assign it to the float data type; you must do either of the following two

a) float f1 =1.0f; //F or f will do
b) float f1 = (float)1.0;

3- While you talk about long and float, think about range of values each can hold instead of how many bits each hold.

a) Although long is 64 bit long, its range of values (the values it can hold) is quite less than the float that is only 32 bit.
This is because of scientific representation of floating point numbers
by the float.

Here is how 10000 can be represented:
10000 = 10 * 10^3 or 1*10^4
10000 = 10 * 10^4 or 10^5
and so on!

Here 4 and 5 are exponents.

finally a number that a long can maximum hold, float can hold but vice versa is not true.


cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi chandra,
I didnt get the below lines. Can you explain me once again please


3- While you talk about long and float, think about range of values each can hold instead of how many bits each hold.

a) Although long is 64 bit long, its range of values (the values it can hold) is quite less than the float that is only 32 bit.


This is because of scientific representation of floating point numbers
by the float.

Here is how 10000 can be represented:
10000 = 10 * 10^3 or 1*10^4
10000 = 10 * 10^4 or 10^5
and so on!

Here 4 and 5 are exponents.

finally a number that a long can maximum hold, float can hold but vice versa is not true
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Using scientific notation, we can represent a far wider range of values using less number of bits than. The largest number a long can hold is 2^63 - 1.


long
8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
float

4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).


You see which data type can hold huge range of values.
Definitely float in comparison to long.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks chandra got it
vish vodeyar
Greenhorn

Joined: Apr 26, 2007
Posts: 4
Hi,
The Answer is option D.This is because the compiler is not smart enough to convert a double to float.This gives type miss match error.This cannot be done implicitly by the compiler and you need a cast. The other options are ok as the sizes are lesser than or equal to float.
I hope I have Explained this to the best of my knowledge.
-Thanks and regards,
Vodeyar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Literals doubt
 
Similar Threads
Data Types
wrappers
Primitive Casting Exercise
can i assign long var to float?
dan exam dought 3