Wednesday, 25 January 2012

Java Primitives and Uncommon Behaviour

You are writing Java program and using primitives in it and suddenly got error like can not covert,type mismatch,out of range etc.This happen because sometimes primitives doesn't work in the way we assume and have their own set of rules as well as predefined limitations.Lets look into the details of each primitive types and understand the limitation  :-

1.byte : 1 byte, range -128 to 127(inclusive) , default value is 0.


2.short : 2 byte, range -32,768 to 32,767(inclusive) , default value is 0.

3. int : 4 byte , range -2,147,483,648 to 2,147,483,647 (inclusive) , default value is 0. int is a default data type for all operations like(+,-,/,*) , passing arguments to methods,Printing in system out.

4.long: 8 byte, range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive) , default value is 0.


5. float: 4 byte , range 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative), default 0.0f.

6. double : 8 byte , range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative) , default  0.0d
7.char : 2 byte, 0 to 65,535 , default is '\u0000'
 
8. boolean : true or false , default is false.

Primitives behaviour :-


1. Unless specified any non decimal value in case of statements like assignment,method parameters and switch is considered as int datatype.
e.g.  byte b1=-128;short s=32767;long l=1234567890;

byte b3=10;
byteTest(b3+1); //Error , The method byteTest(byte) is not applicable for the argument int.
public void byteTest(byte x)
{

}
public void byteTest(int x)
{

}
//No Error.


2. Implicit type casting is performed only when there is assignment operation is involved.
    
    byte b1=-129; //Error , Type mismatch : cannot convert from int to byte
    Reason : byte can hold values in the range -128 to 127



   short s2=32768; //Error , Type mismatch : cannot convert from int to short
   Reason : short can hold values in the range -32,768 to 32,767

   short s3=-32768;  //No Error

   long l= 12345678901;//Error , though long can accomodate this value.
   Reason : int is out of range.



3. Arithmetic operators always returns an int, unless one of the operands is a long, float, double.   
     
    byte b9=10; //No Error , Implicitly int is typecasted to byte and 10 is in byte limit(-128 to 127)
    
    byte b10=20;
    byte b11=10+20; //No Error , Results an int , but implicit typecasting is done
    
    byte b12=(byte)(b9+b10);//Error , if not typecaseted explicitly.


    So what happened above , its fine when you say 10+20 and error when you say b10+b11 ?  
    Actually, when there are variables involved no implicit typecasting is performed. 
      
     long b9=10L;
     byte b10=20;

     byte b11=2L/2; //Error, Results a long , no implicit typecasting
     If there are two different data types in arithmetic operation , result will be the type of highest datatype among the operands.    


     b11=b9/b10;    //Error ,Results a long , explicit typecasting is required


4.Explicit type casting is required when passing int , float , long , double values to their lowest data types respectively.


values can be implicitly assigned in this fashion.
byte ->short -> int -> long -> float ->double





No comments:

Post a Comment