Skip to content

Commit 1ebf85e

Browse files
committed
feat: Add TypeCasting class demonstrating manual casting and type promotion
Add a class that: - Demonstrates explicit type casting from float to int - Shows automatic type promotion in arithmetic expressions - Combines various primitive types in a mixed expression - Illustrates how Java handles data type conversion and expression evaluation.
1 parent d269a60 commit 1ebf85e

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Section5OperatorExpression/src/TypeCasting.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,44 @@
33
public class TypeCasting {
44
public static void main(String[] args) {
55
Scanner input = new Scanner(System.in);
6-
// float num = input.nextFloat();
7-
// int num = input.nextInt();
8-
// System.out.println(num);
6+
7+
/*float num = input.nextFloat();
8+
int num = input.nextInt();
9+
System.out.println(num);*/
910

1011
// type casting
1112
int num = (int)(67.56f);
12-
// System.out.println(num);
13+
System.out.println(num);
14+
15+
/*automatic type promotion in expressions*/
16+
17+
/*int a = 257;
18+
byte b = (byte)(a); // 257 % 256 = 1
1319
14-
// automatic type promotion in expressions
15-
// int a = 257;
16-
// byte b = (byte)(a); // 257 % 256 = 1
20+
byte a = 40;
21+
byte b = 50;
22+
byte c = 100;
23+
int d = a * b / c;
1724
18-
// byte a = 40;
19-
// byte b = 50;
20-
// byte c = 100;
21-
// int d = a * b / c;
22-
//
23-
// System.out.println(d);
25+
System.out.println(d);
2426
25-
// byte b = 50;
26-
// b = b * 2;
27+
byte b = 50;
28+
b = b * 2;
2729
28-
// int number = 'A';
29-
// System.out.println("你好");
30+
int number = 'A';
31+
System.out.println("你好");
3032
31-
// System.out.println(3 * 6);
33+
System.out.println(3 * 6);*/
3234

3335
byte b = 42;
3436
char c = 'a';
3537
short s = 1024;
3638
int i = 50000;
3739
float f = 5.67f;
3840
double d = 0.1234;
41+
3942
double result = (f * b) + (i / c) - (d * s);
43+
4044
// float + int - double = double
4145
System.out.println((f * b) + " " + (i / c) + " " + (d * s));
4246
System.out.println(result);

0 commit comments

Comments
 (0)