-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.java
More file actions
44 lines (34 loc) · 1.84 KB
/
casting.java
File metadata and controls
44 lines (34 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*Type casting is when you assign a value of one primitive data type to another type.
In Java, there are two types of casting:
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
*/
public class casting {
public static void main(String[] args) {
byte b = 127;
System.out.println("byte: " + b);
/*Widening casting is done automatically when passing a smaller size type to a larger size type:*/
int myInt1 = 9;
double myDouble1 = myInt1; // Automatic casting: int to double
System.out.println(myInt1); // Outputs 9
System.out.println(myDouble1); // Outputs 9.0
/*Narrowing casting must be done manually by placing the type in parentheses () in front of the value:*/
double myDouble2 = 9.78d;
int myInt2 = (int) myDouble2; // Manual casting: double to int
System.out.println(myDouble2); // Outputs 9.78
System.out.println(myInt2); // Outputs 9
// Set the maximum possible score in the game to 500
int maxScore = 500;
// The actual score of the user
int userScore = 423;
/* Calculate the percantage of the user's score in relation to the maximum available score. Convert userScore
to float to make sure that the division is accurate */
float percentage = (float) userScore / maxScore * 100.0f;
System.out.println("User's percentage is " + percentage);
double fahrenheit = 98.6;
double celsius = (fahrenheit - 32) * 5 / 9;
System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");
}
}