• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignement in java

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But internally both are stored as a bit pattern, so cant integer interpret the bit pattern as an integer?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There might be a way to do that, but the "=" operator isn't that way.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 95
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That link no longer works, I am afraid.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic