Type Casting
Type Casting
In Java, the process of changing data from one data type to another is called type casting. There are two kinds of casting in Java: explicit (manual) and implicit (automatic).
Implicit casting(automatic)
Involves moving from a smaller data type to a larger one, a process that the compiler can perform automatically. When no data loss is anticipated, like when converting from byte to int or from int to double, this usually happens.
byte -> short -> char -> int
-> long -> float -> double
Explicit Casting (manually)
however, requires converting a larger data type to a smaller one and is done manually. Because data loss may occur from this operation, the programmer must specifically define the desired data type translation. Explicit casting is needed, for instance, when converting from double to int or from long to short.
Example:
01:
In this example, there is an implicit cast—myInt is instantly converted to a double for myDouble. On the other hand, precision may be lost when anotherDouble is explicitly cast to an int for anotherInt. Recall that changing from a larger-sized type to a smaller-sized type necessitates explicit casting.
public class TypeCastingExample {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
double anotherDouble = 9.78;
int anotherInt = (int) anotherDouble; // Manual casting: double to int
System.out.println(anotherDouble); // Outputs 9.78
System.out.println(anotherInt); // Outputs 9
}
}
-------------------------------------------------------------------------------------------------------------------
02:
/* ASCII - American Standard Code For Information Interchange
Computers can only understand numbers,
so an ASCII code is the numerical representation of a character such as 'a' or '@' etc.
The first 32 characters in the ASCII-table
are unprintable control codes and are used to control peripherals such as printers.
Codes 32-127 are common for all the different variations of the ASCII table, they are
called printable characters, represent letters, digits, punctuation marks,
and a few miscellaneous symbols.
65-90 A-Z
97-122 a-z
48-57 0-9
Space 32
*/
import java.util.Scanner; //Package
public class ascii {
public static void main(String args[])
{
Scanner in = new Scanner(System.in); //Input Class
System.out.println(" Enter a number : ");
int alpha = in.nextInt();
System.out.println(alpha+ " is the numeric value of the alphabet"
+ "/character " +(char)alpha); //explicit conversion
}
}
Output:
Enter a number :
72
72 is the numeric value of the alphabet/character H
------------------------------------------------------------------------------------------------------------------------------
03:
//Type Casting in Java
import java.lang.*;
class casting
{
public static void main(String args[])
{
int a=10;
double b=a,// Widening casting
d=25.5385;
int c=(int)d; //Narrowing casting
System.out.println("Int : "+a);
System.out.println("Double : "+b);
System.out.println("Double : "+d);
System.out.println("Int : "+c);
}
}
Output:
Int : 10
Double : 10.0
Double : 25.5385
Int : 25
0 Comments