• 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

Primitive variable assignments

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the compliler inserts an implicit cast with literal variable assignments smaller than an int:

byte = 10----->puts int the (byte) cast 4 you

Does anyone know why an implicit cast for variable assigntment with expressions is not done.
e.g
byte b = 10; implcit cast from int to byte
byte c = 11; implcit cast from int to byte
byte d = b + c;// complier error

You have to put in you own cast.
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you add any two integral numbers together, you will at least get an int.

i.e

byte a = 1, b = 2;
byte a = a + b; //will evaluate to int (compile time error)

byte a = (byte)(a + b); //ok, casting result back to byte value
 
Philani Dlamini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx but what about when you Divide, Substract, or Multiply integral numbers, you only mentioned adding. I thought this promotion happens with any type of expression that is less than or equal to an int.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ByteTest
{
public static void main ( String rags[] )
{
byte b1 , b2 , b3 , b4 , b5 ;

b1 = 2 ;
b2 = 4 ;

b3 = ( byte ) ( b2 / b1 ) ; // ( 1 )
System.out.println ( " The value of b3 is " +b3 ) ;

b4 = ( byte ) ( b2 * b1 ) ; // ( 2 )

System.out.println ( " The value of b4 is " +b4 ) ;

b5 = ( byte ) ( b2 - b1 ) ; // ( 3 )

System.out.println ( " The value of b5 is " +b5 ) ;

}
}

if the l.h.s is byte , short or char datatype variable and the r.h.s is a int value , then implicit typecasting occurs and the int data is typecasted to appropriate data-type

the compiler when compiling the code doesn't evaluate the expressions at ( 1) , ( 2 ) , ( 3 ) , this evaluation is only done during run time , so during compilation it becomes necessary to make the compiler aware that the resultant data that is produced by the expression will go into a type-compatible variable
 
Philani Dlamini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx for the response, now i understand
 
reply
    Bookmark Topic Watch Topic
  • New Topic