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
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home