Skip to content

Commit d663452

Browse files
authored
Create Type_Casting
1 parent 8e2ff26 commit d663452

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: Type_Casting

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class type_casting {
2+
public static void main(String[] args) {
3+
// TODO: Implicit (widening) casting
4+
// example_1
5+
int num = 100;
6+
long bigNum = num;
7+
float decimal = num;
8+
System.out.println("Original num: " + num);
9+
System.out.println("Big num: " + bigNum);
10+
System.out.println("Decimal num: " + decimal);
11+
// example_2
12+
int intVal = 130;
13+
byte byteVal = (byte) intVal;
14+
System.out.println("int Value: " + intVal);
15+
// example_3
16+
char char_value = 'A';
17+
int int_val = char_value;
18+
System.out.println("char Value: " + char_value);
19+
System.out.println("int Value (ASCII value): " + int_val);
20+
21+
// TODO: Explicit (narrowing) casting
22+
// example_1
23+
double doubleValue = 9.7;
24+
int intValue = (int) doubleValue;
25+
System.out.println("double Value: " + doubleValue);
26+
System.out.println("int Value: " + intValue);
27+
// example_2
28+
double dollarAmount = 9.99;
29+
int cents = (int) (dollarAmount * 100);
30+
System.out.println("Dollar Amount: $" + dollarAmount);
31+
System.out.println("Cents: " + cents);
32+
// example_2
33+
int age1 = 25;
34+
int age2 = 30;
35+
int age3 = 35;
36+
double averageAge = (double) (age1 + age2 + age3) / 3;
37+
System.out.println("Average Age: " + averageAge);
38+
// example_3
39+
double accountBalance = 98765.43;
40+
int roundedBalance = (int) accountBalance;
41+
System.out.println("Account Balance: $" + accountBalance);
42+
System.out.println("Rounded Balance: $" + roundedBalance);
43+
}
44+
}

0 commit comments

Comments
 (0)