Implicit Typecasting (Widening)
JAVA compiler will automatically typecast a smaller data type to a larger one. This is called widening or Implicit Typecasting as JAVA complier does it for you. For instance:
int a = 12;
long l = a; //here a is automatically typecasted to long
Explicit Typecasting (Narrowing)
When you assign a larger data type to a smaller one, you will loose data. Such conversions need to be type casted as JAVA needs confirmation that you are OK with losing data!!
Consider below examples:
long a = 123456;
int b = (int)a;
double d = 4.2;
int x = (int)d; //x = 4
Converting Numeric data type to String
int a = 2;
String intString = String.valueOf(a); //"2"
long l = 24567;
String longString = String.valueOf(l);//"24567"
float f = 3.6f;
String floatString = String.valueOf(f);//"3.6"
double d = 4.5;
String doubleString = String.valueOf(d);//"4.5"
Converting String to Numeric data type
String intString = "2";
int a = Integer.valueOf(intString);//2
String longString = "24567";
long l = Long.valueOf(longString);//24567
String floatString ="3.5";
float f = Float.valueOf(floatString);//3.5f
String doubleString="4.5";
double d = Double.valueOf(doubleString);//4.5
String someString ="demo123";
int intValue = Integer.valueOf(someString);//This will give ERROR! as demo123 //cannot be converted to a integer number!!
Common mistakes to avoid:
Division :
Consider below scenario:
int a = 9 , b = 2;
double div = a/b;
System.out.println(div);
The output for above snippet is :
4.0
Puzzled? Shouldn’t it be 4.5 ?
Well this is the magic of JAVA 😉
So the rule is : when you divide two data types, the answer is stored in the bigger type. Once the answer is evaluated then it is assigned or typecasted to the data type on the left.
Here, as a and b both are integers, the division is done in integer data type and value is 4. As 4 is now assigned to double ,so it gets typecasted to 4.0
So, if you want the correct output as 4.5 for the above division. Try typecasting numerator or denominator as double. This will make JAVA perform division in the higher data type which is double and you will not loose data.
int a = 9 , b = 2;
double div = (double)a/b; //you can also do : a/(double)b or (double)a/(double)/b
System.out.println(div);
Output: 4.5
Multiplication:
Similar to division, in multiplication JAVA will perform multiplication in the highest data type on the right hand side and then typecast the result to the type mentioned on left hand side of the equation.
Consider below scenario:
int a = 9 , b = 2;
double mul = a * b;
System.out.println(mul);
Output: 18.0
Here, it doesn’t seem to have issues. But imagine the values of a and b are large enough , such that multiplication result of a and b is outside the range of integer. Then the result would overflow and you will get wrong result! So , if you have possibility where the product of two numbers can overflow their own data type, then we need to smartly typecast one of the number to avoid overflow !
int a = 2147483646 , b = 2; long mul = a * b; System.out.println(mul); Output:-4
As there was overflow when a is multiplied to b, we get a garbage value !
So, to avoid this we need to typecast a or b to long, so that multiplication is performed in long data type.See the below code snippet.
int a = 2147483646 , b = 2;
long mul = (long)a * b;//you could also do: a * (long)b System.out.println(mul);
Output:4294967292
Comments