This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment question?? doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment question?? doubt?" Watch "assignment question?? doubt?" New topic

assignment question?? doubt?

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
float a = 1; // 1
float b = 1L; // 2
float c = 1F; //3
float d = 1.0; //4

4 wouldnt compile as ur trying to assign an double value to a float...

byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8

what i feel after seeing these expressions is ..
uptil int if its a constant it can assign to any far as it is in the range of the left hand operand...
but after that for long ,float,double it sees whether they can assignable to appropriate primitive...

i got this doubt coz line 6 7 8 generate compile error,i thought they are compile time constants so they cub be easily fit into it...

please clarify

Thanks & Regards<br /> <br />-Srikanth
Roel De Nijs

Joined: Jul 19, 2004
Posts: 7059

about this line float d = 1.0; //4
you said yourself: "4 wouldnt compile as ur trying to assign an double value to a float..."

what's the difference between that line and following 2 lines (except for the left thing being a byte instead of a float)
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8

indeed, there is no difference.

for line 6 you're telling the compiler you are using a long and so you try assigning a long to a byte, which also don't work without using explicit cast

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
It is sorta covered in the JavaRanch Style Guide.
subject: assignment question?? doubt?
It's not a secret anymore!