Monday, July 22, 2024

Type Casting in Java

 

Type Casting:

Type casting in programming refers to the process of converting one data type into another.

In Java, there are two types of casting: implicit casting and explicit casting.

 1. Implicit Casting (Automatic Casting) (or) Widening:

//                           Widening Casting (automatically) - converting a smaller type to a larger type size

//                           byte -> short -> char -> int -> long -> float -> double

//. It is also known as implicit conversion or casting down

//  conversion is done by compiler automatically

//it is safe because there is no chance to lose data.

ex: convert int --> long   , long --> float

 

package TypeCastingBasics;

public class ConversionIntToLongType {

               public static void main(String[] args) {

                             

                             

                              //automatically converts the integer type into long type 

                              int i = 10;

                              long l = i;

                             

                              //automatically converts the long type into float type 

                               

                              long l2 =  234;

                              float f = l2;

                              System.out.println("Before conversion, int value "+i); 

                              System.out.println("After conversion, long value "+l); 

                              System.out.println("After conversion, float value "+f);

               }

}

o/p:

---

Before conversion, int value 10

After conversion, long value 10

After conversion, float value 234.0

 ex2 :   convert int to float ?

// 1. Convert int to float value ---> widening-  Compiler will do type casting by default

                              //    4  ->  4.0    -- dont lose any data

package TypeCastingBasics;

public class ConversionIntToFloatType {

               public static void main(String[] args) {

                              // 1. Convert int to float value ---> widening-  Compiler will do type casting by default

                              //    4  ->  4.0    -- dont lose any data

                              //  smaller data type --> bigger data type

                              int i =4;

                              float f = i;//  widening

                              //         4

                              //        4 --> 4.0

                              //   f = 4.0 but not 4

                              System.out.println("f= "+f);

                              // f= 4.0              

               }

}

2. Explicit Casting (Manual Casting) (or) Narrowing:

//                           1. Narrowing Casting (manually) - converting a larger type to a smaller size type

//                           double -> float -> long -> int -> char -> short -> byte

////  conversion is done by programmer

//It is also known as explicit conversion or casting up.

 If we do not perform casting then the compiler reports a compile-time error.

ex:// convert float value to int val?          

                                            

package TypeCastingBasics;

public class ConversionFloatToInt {

               public static void main(String[] args) {

                              // convert float value to int val?               

                              //   4.32f  -->  4   --  lost of data i.e decimal part value .32

                              //  4B           4Bytes 

                              float f = 4.32f;

                             

//                           int i = f;// Type mismatch: cannot convert from float to int

                              // Type mismatch: cannot convert from float to int

                              //  i1  =      4.32 f

                              int  i = (int) f;

                              //    (int)  4.32f    // Programmer

                              //    i=     4

                              System.out.println("i="+i);

                                                                                          // i=4

               }

}

                             

//                           i = f1;// Type mismatch: cannot convert from float to int

                              // compile will now allow bigger Data type to smaller

                              //  CE:   programmer has to do type casting explicitly

                              i = (int) f1; //

                              //         3.5 -->  remove decimal points  3  

                              System.out.println("i="+ i);// 3 -- data lost  

                             

---------------------

//                           byte -> short -> char -> int -> long -> float -> double

                              // smaller data types                        bigger data type  -->  widening

package TypeCastingBasics;

public class ConvertByteToChar {

               public static void main(String[] args) {

//                           byte -> short -> char -> int -> long -> float -> double

                              // smaller data types                        bigger data type  -->  widening

  

                              // convert  byte val to char

                                             // 65 --> A

                              byte b =65;

//                           chat ch  = b;// CE

                              // Do explicit type casting

                              char ch  =(char) b;

                              //             (char)  65  --->  A

                              System.out.println("ch="+ ch);

               }

}

--------------------------------------------                           

              

//                           double -> float -> long -> int -> char -> short -> byte

                              // convert char to int

                             

                             

package TypeCastingBasics;

public class ConvertCharToInt {

               public static void main(String[] args) {

                              // convert char to int

                              char ch = 'A';

                              int i = ch;

                              //      'A'

                              //       65

                              //   i = 65

                              System.out.println("i="+i);          

                              //  convert int val to boolean ---not valid

                              int i2 =10;

                              //boolean b1 = i2;// Type mismatch: cannot convert from int to boolean

                              //    10 --> true  we cannot convert

                              //  20 --> false

                             

//                           boolean b1 = (boolean)i2;//CE:  Cannot cast from int to boolean

                             

               }

}

              

                              // HW convert int to double value  5--> 5.0

                              // HW convert double val to int    5.4 ---> 5  

FAQ :What is type casting?

FAQ Difference b/w Widening and narrowing?

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...