This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignement in java

 
goel Ashish
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
goel Ashish
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But internally both are stored as a bit pattern, so cant integer interpret the bit pattern as an integer?
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a way to do that, but the "=" operator isn't that way.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
C++ Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That link no longer works, I am afraid.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic