aspose file tools*
The moose likes Beginning Java and the fly likes Assignement in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assignement in java" Watch "Assignement in java" New topic
Author

Assignement in java

goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
I have a little confusion in assignment concept in java.
What I have understood is compiler only gives error when we try to assign a variable of larger bits to small bit size variable(narrowing) like long to int. And doesn't gives error if we do widening.
But in the example below :-

Compiles fine while being a narrowing.

but this one below :-


gives compiler error while both float and int are 32 bits.
Why is that??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

If you read the error the compiler gives you, it tells you exactly why it is giving an error:


How is the compiler supposed to store that fractional part in an integer-type?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
But internally both are stored as a bit pattern, so cant integer interpret the bit pattern as an integer?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

There might be a way to do that, but the "=" operator isn't that way.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

goel Ashish wrote:But internally both are stored as a bit pattern, so cant integer interpret the bit pattern as an integer?

Sure, it COULD be done...but the result would be totally meaningless. What would be the point?
goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
Paul Clapham wrote:There might be a way to do that, but the "=" operator isn't that way.

so does that mean that floating point no.s cant be assigned to any other type like char, int, byte etc. without any explicit cast but the other types can be assigned to one another if we are widening.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

goel Ashish wrote:
Paul Clapham wrote:There might be a way to do that, but the "=" operator isn't that way.

so does that mean that floating point no.s cant be assigned to any other type like char, int, byte etc. without any explicit cast but the other types can be assigned to one another if we are widening.


No. It means that there might be a way to assign the 32 bits which represent a float value to an int, but that the "=" operator isn't that way. It doesn't say anything at all about assigning things to char, it doesn't say anything about assigning values of other types to one another, and it doesn't say anything about widening.
goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
Paul Clapham wrote:
goel Ashish wrote:
Paul Clapham wrote:There might be a way to do that, but the "=" operator isn't that way.

so does that mean that floating point no.s cant be assigned to any other type like char, int, byte etc. without any explicit cast but the other types can be assigned to one another if we are widening.


No. It means that there might be a way to assign the 32 bits which represent a float value to an int, but that the "=" operator isn't that way. It doesn't say anything at all about assigning things to char, it doesn't say anything about assigning values of other types to one another, and it doesn't say anything about widening.


So what all are legal assignments in java using = operator? I am not clear about the concept
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
goel Ashish wrote: . . . So what all are legal assignments in java using = operator? I am not clear about the concept
Look in the Java™ Language Specification.
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Refer to this link http://en.wikipedia.org/wiki/Primitive_wrapper_class
It says that an int takes only an Integer as an argument and float takes a float or double as an argument
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
That link no longer works, I am afraid.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assignement in java