Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

implecit casting

Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is implecit casting is done for all datatype,which comes narrower than int?
look here
byte b=100//means byte b=(byte)100 OK
char c=100//means char c=(char)100 or
char c=(byte)100 or
cannot be implecitely casted?
long l=10.2f//why explecit cast is required??
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implicit casting happens all the time.

int i = 5;
float f = i; //this assignment is correct, the int is implicitly cast to a float.
Any time you assign a value to a wider variable, the compiler implicitly casts that value to the wider variable. Since there's no potential loss of information, the assignement can be automatic.
What you are talking about in your examples are *narrowing conversions*. They are ONLY done implicitly in the following situations:
the variable being assigned to is byte, char, or short, AND the value your are assigning can "fit" into that variable, AND that can be determined at compile time.
That's why this always works:
byte b = 100; //100 is an int literal.
and this works too
final int value = 100;
byte b = i ; //implicitly narrowed, because at compile time we can tell that 100 will fit into a byte.
but this doesn't work because the compiler can't guarantee the variable on the right will have a value that will fit at runtime:
int i = 20;
byte b = i; //won't work, compiler error.
the following example is misleading:
char c=(byte)100 ;
you're not assigning a byte to a char; first the int literal 100 gets narrowed via the byte cast; since 100 will fit into a byte, there is no loss of precision. Then, this byte is assigned to a char. Again, because this is a compile time constant, the compiler can verify that 100 will fit into a char variable. But try this:
char c = (byte)-100; //compile error
In this fails because char variables can only hold unsigned values, ie, from 0 to 65,535.
Your final example:
long l=10.2f; //why explecit cast is required??

You can't assign a floating-point value like 10.2 to an integral type like long. They're incompatible. You have to cast the float value to an integral type.
long l = (long) 10.2f;
    Bookmark Topic Watch Topic
  • New Topic