最后活跃于 1739827005

Java Variables, Operators, and Types

kristofer's Avatar kristofer 修订了这个 Gist 1739827005. 跳至此修订

1 file changed, 172 insertions

varsopstypes.md(file created)

@@ -0,0 +1,172 @@
1 + # Java Variables, Operators, and Types
2 +
3 + ## Primitive Data Types
4 + Java has eight primitive data types that store simple values. Here's a comprehensive overview:
5 +
6 + ```java
7 + public class PrimitiveTypes {
8 + public static void main(String[] args) {
9 + // Integer types
10 + byte smallNumber = 127; // 8-bit, range: -128 to 127
11 + short mediumNumber = 32000; // 16-bit, range: -32,768 to 32,767
12 + int standardNumber = 2000000000; // 32-bit, range: ~-2B to ~2B
13 + long bigNumber = 9000000000L; // 64-bit, needs 'L' suffix
14 +
15 + // Floating-point types
16 + float decimalNumber = 3.14f; // 32-bit, needs 'f' suffix
17 + double preciseNumber = 3.14159; // 64-bit, default for decimals
18 +
19 + // Character type
20 + char letter = 'A'; // 16-bit Unicode character
21 +
22 + // Boolean type
23 + boolean isActive = true; // true or false
24 + }
25 + }
26 + ```
27 +
28 + ## Variable Declaration and Initialization
29 +
30 + Variables can be declared and initialized in several ways:
31 +
32 + ```java
33 + public class VariableDeclaration {
34 + public static void main(String[] args) {
35 + // Single declaration
36 + int age;
37 + age = 25;
38 +
39 + // Declaration with initialization
40 + String name = "John";
41 +
42 + // Multiple declarations
43 + int x, y, z;
44 + x = 1; y = 2; z = 3;
45 +
46 + // Multiple declarations with initialization
47 + double height = 5.9, weight = 68.5;
48 +
49 + // Constants (final variables)
50 + final double PI = 3.14159;
51 + final int MAX_USERS = 100;
52 + }
53 + }
54 + ```
55 +
56 + ## Type Conversion and Casting
57 +
58 + Java supports both implicit (automatic) and explicit (manual) type conversion:
59 +
60 + ```java
61 + public class TypeConversion {
62 + public static void main(String[] args) {
63 + // Implicit conversion (widening)
64 + int smallNum = 100;
65 + long bigNum = smallNum; // int to long
66 + float floatNum = bigNum; // long to float
67 +
68 + // Explicit conversion (narrowing)
69 + double price = 99.99;
70 + int roundedPrice = (int) price; // Loses decimal part
71 +
72 + // String conversion
73 + String strNumber = "123";
74 + int parsedInt = Integer.parseInt(strNumber);
75 + double parsedDouble = Double.parseDouble("123.45");
76 +
77 + // Converting numbers to String
78 + String strValue = String.valueOf(parsedInt);
79 + String anotherStr = Integer.toString(parsedInt);
80 + }
81 + }
82 + ```
83 +
84 + ## Operators
85 +
86 + ### Arithmetic Operators
87 + ```java
88 + public class ArithmeticOperators {
89 + public static void main(String[] args) {
90 + int a = 10, b = 3;
91 +
92 + System.out.println("Addition: " + (a + b)); // 13
93 + System.out.println("Subtraction: " + (a - b)); // 7
94 + System.out.println("Multiplication: " + (a * b)); // 30
95 + System.out.println("Division: " + (a / b)); // 3
96 + System.out.println("Modulus: " + (a % b)); // 1
97 +
98 + // Increment and decrement
99 + int count = 5;
100 + System.out.println(count++); // Prints 5, then increments
101 + System.out.println(++count); // Increments, then prints 7
102 + }
103 + }
104 + ```
105 +
106 + ### Comparison Operators
107 + ```java
108 + public class ComparisonOperators {
109 + public static void main(String[] args) {
110 + int x = 5, y = 8;
111 +
112 + System.out.println(x == y); // Equal to: false
113 + System.out.println(x != y); // Not equal to: true
114 + System.out.println(x > y); // Greater than: false
115 + System.out.println(x < y); // Less than: true
116 + System.out.println(x >= y); // Greater than or equal: false
117 + System.out.println(x <= y); // Less than or equal: true
118 + }
119 + }
120 + ```
121 +
122 + ### Logical Operators
123 + ```java
124 + public class LogicalOperators {
125 + public static void main(String[] args) {
126 + boolean isValid = true;
127 + boolean isActive = false;
128 +
129 + System.out.println(isValid && isActive); // AND: false
130 + System.out.println(isValid || isActive); // OR: true
131 + System.out.println(!isValid); // NOT: false
132 +
133 + // Short-circuit evaluation
134 + int num = 10;
135 + if (num > 0 && num++ < 20) {
136 + System.out.println(num); // Prints 11
137 + }
138 + }
139 + }
140 + ```
141 +
142 + ### Assignment Operators
143 + ```java
144 + public class AssignmentOperators {
145 + public static void main(String[] args) {
146 + int value = 10;
147 +
148 + value += 5; // value = value + 5
149 + value -= 3; // value = value - 3
150 + value *= 2; // value = value * 2
151 + value /= 4; // value = value / 4
152 + value %= 3; // value = value % 3
153 +
154 + // Bitwise assignment operators
155 + int flags = 5;
156 + flags &= 3; // Bitwise AND assignment
157 + flags |= 4; // Bitwise OR assignment
158 + flags ^= 1; // Bitwise XOR assignment
159 + }
160 + }
161 + ```
162 +
163 + Remember these key points about Java variables and operators:
164 + - Variables must be declared before use
165 + - Java is strongly typed, meaning type checking happens at compile time
166 + - The type of a variable determines what operations can be performed on it
167 + - Implicit type conversion only works when going from a smaller to a larger data type
168 + - Explicit type conversion (casting) is required when going from a larger to a smaller data type
169 + - Operators follow a specific precedence order when evaluating expressions
170 + - Short-circuit evaluation in logical operators can improve performance
171 +
172 + Practice with these examples to better understand how variables, types, and operators work together in Java programs.
更新 更早