Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit Widening aka Conversions ???

 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ImplicitWidening
{
public static void main(String[] args)
{
int i = 25;
short s2 = i;
byte b = i;
char c = i;

/*

---------- Javac ----------
ImplicitWidening.java:9: possible loss of precision
found : int
required: short
short s2 = i;
^
ImplicitWidening.java:10: possible loss of precision
found : int
required: byte
byte b = i;
^
ImplicitWidening.java:11: possible loss of precision
found : int
required: char
char c = i;
^
3 errors
Normal Termination
Output completed (0 sec consumed).

*/
}
}// Done

why do i get the errors in comment???
============================================================================
==========================================================================

why do i not get an error for the following??? What role does final play here???

class ImplicitWidening
{
public static void main(String[] args)
{
final int i = 25;
short s2 = i;
byte b = i;
char c = i;

}
}// done

============================================================================
Have A Nice Day !!!
 
Karthik Vaidiswaran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java performs an implicit narrowing conversion if certain conditions are satisfied.
Please go through this page
http://www.absolutejava.com/main-articles/data-type-conversions/


Thanks
Karthik
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic