-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathq3.java
More file actions
70 lines (60 loc) · 3.09 KB
/
q3.java
File metadata and controls
70 lines (60 loc) · 3.09 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Mayank Yadav Q3
public class q3 {
public static void main(String[] args) {
System.out.println("=== Java Literals and Operators Demo ===\n");
int decimalLiteral = 42;
int binaryLiteral = 0b101010;
int octalLiteral = 052;
int hexLiteral = 0x2A;
float floatLiteral = 3.14f;
double doubleLiteral = 2.718281828;
char charLiteral = 'A';
char unicodeLiteral = '\u0041';
boolean boolLiteral = true;
String stringLiteral = "Hello, Java!";
System.out.println("=== Part 1: Literals Demo ===");
System.out.println("Decimal literal: " + decimalLiteral);
System.out.println("Binary literal: " + binaryLiteral);
System.out.println("Octal literal: " + octalLiteral);
System.out.println("Hexadecimal literal: " + hexLiteral);
System.out.println("Float literal: " + floatLiteral);
System.out.println("Double literal: " + doubleLiteral);
System.out.println("Character literal: " + charLiteral);
System.out.println("Unicode character: " + unicodeLiteral);
System.out.println("Boolean literal: " + boolLiteral);
System.out.println("String literal: " + stringLiteral);
int counter = 10;
System.out.println("\n=== Part 2: Increment/Decrement Operators ===");
System.out.println("Initial counter value: " + counter);
System.out.println("Post-increment (counter++): " + (counter++));
System.out.println("Counter after post-increment: " + counter);
System.out.println("Pre-increment (++counter): " + (++counter));
System.out.println("Counter after pre-increment: " + counter);
System.out.println("Post-decrement (counter--): " + (counter--));
System.out.println("Counter after post-decrement: " + counter);
System.out.println("Pre-decrement (--counter): " + (--counter));
System.out.println("Counter after pre-decrement: " + counter);
int intVar = 5;
double doubleVar = 2.5;
float floatVar = 1.5f;
char charVar = 'B';
int result1 = intVar + 3;
double result2 = intVar + doubleVar;
double result3 = floatVar + doubleVar;
int result4 = charVar + intVar;
System.out.println("\n=== Part 3: Data Type of Expressions ===");
System.out.println("int + int = " + result1 + " (Type: int)");
System.out.println("int + double = " + result2 + " (Type: double)");
System.out.println("float + double = " + result3 + " (Type: double)");
System.out.println("char + int = " + result4 + " (Type: int, 'B' has ASCII value 66)");
int a = 20, b = 10, c = 5;
int result5 = a - b - c;
int x, y, z;
x = y = z = 15;
System.out.println("\n=== Part 4: Operator Associativity ===");
System.out.println("Left-to-right: 20 - 10 - 5 = " + result5);
System.out.println("Evaluation: (20 - 10) - 5 = 10 - 5 = " + result5);
System.out.println("Right-to-left assignment: x = y = z = 15");
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
}
}