forked from abhinav1602/JAVA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaBasics.java
More file actions
362 lines (313 loc) · 12.7 KB
/
JavaBasics.java
File metadata and controls
362 lines (313 loc) · 12.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* JavaBasics.java
*
* A comprehensive introduction to Java fundamentals covering:
* - Primitive data types
* - Variables and constants
* - Basic operators
* - Control flow statements
* - Basic input/output
*
* This file serves as the starting point for beginners before diving into
* the advanced modules (Collections, Multithreading, Streams, etc.)
*
* @author Abhinav
* @version 1.0
*/
public class JavaBasics {
/**
* Main method - Entry point of the Java application
*/
public static void main(String[] args) {
System.out.println("=".repeat(60));
System.out.println(" JAVA BASICS - FUNDAMENTAL CONCEPTS");
System.out.println("=".repeat(60));
System.out.println();
// Run all demonstration methods
demonstratePrimitiveTypes();
demonstrateVariablesAndConstants();
demonstrateArithmeticOperators();
demonstrateRelationalOperators();
demonstrateLogicalOperators();
demonstrateConditionalStatements();
demonstrateLoops();
demonstrateTypeConversion();
System.out.println("\n" + "=".repeat(60));
System.out.println(" END OF JAVA BASICS DEMONSTRATION");
System.out.println("=".repeat(60));
}
/**
* Demonstrates all 8 primitive data types in Java
*
* Primitive Types:
* - byte (8-bit integer)
* - short (16-bit integer)
* - int (32-bit integer)
* - long (64-bit integer)
* - float (32-bit floating point)
* - double (64-bit floating point)
* - char (16-bit Unicode character)
* - boolean (true/false)
*/
private static void demonstratePrimitiveTypes() {
System.out.println("\n1. PRIMITIVE DATA TYPES");
System.out.println("-".repeat(50));
// Integer types
byte byteVar = 127; // Range: -128 to 127
short shortVar = 32767; // Range: -32,768 to 32,767
int intVar = 2147483647; // Range: -2^31 to 2^31-1
long longVar = 9223372036854775807L; // Range: -2^63 to 2^63-1 (note the 'L')
// Floating-point types
float floatVar = 3.14159f; // 32-bit (note the 'f')
double doubleVar = 3.141592653589793; // 64-bit (default for decimals)
// Character and boolean types
char charVar = 'A'; // Single character in single quotes
boolean booleanVar = true; // true or false
// Display all primitive types
System.out.println("byte: " + byteVar + " (8-bit)");
System.out.println("short: " + shortVar + " (16-bit)");
System.out.println("int: " + intVar + " (32-bit)");
System.out.println("long: " + longVar + " (64-bit)");
System.out.println("float: " + floatVar + " (32-bit)");
System.out.println("double: " + doubleVar + " (64-bit)");
System.out.println("char: " + charVar + " (16-bit Unicode)");
System.out.println("boolean: " + booleanVar + " (true/false)");
}
/**
* Demonstrates variables and constants in Java
*
* Variables: Can be changed after initialization
* Constants: Use 'final' keyword, cannot be changed
*/
private static void demonstrateVariablesAndConstants() {
System.out.println("\n2. VARIABLES AND CONSTANTS");
System.out.println("-".repeat(50));
// Variables (can be modified)
int count = 10;
System.out.println("Initial count: " + count);
count = 20; // Modified
System.out.println("Modified count: " + count);
// Constants (cannot be modified)
final double PI = 3.14159;
final int MAX_USERS = 100;
System.out.println("Constant PI: " + PI);
System.out.println("Constant MAX_USERS: " + MAX_USERS);
// Uncommenting the line below would cause a compilation error:
// PI = 3.14; // Error: cannot assign a value to final variable PI
// Variable naming conventions
System.out.println("\nNaming Conventions:");
System.out.println("- Variables: camelCase (e.g., userName, totalCount)");
System.out.println("- Constants: UPPER_SNAKE_CASE (e.g., MAX_SIZE, PI)");
System.out.println("- Classes: PascalCase (e.g., JavaBasics, MyClass)");
}
/**
* Demonstrates arithmetic operators
*
* Operators: +, -, *, /, %, ++, --
*/
private static void demonstrateArithmeticOperators() {
System.out.println("\n3. ARITHMETIC OPERATORS");
System.out.println("-".repeat(50));
int a = 20, b = 7;
System.out.println("a = " + a + ", b = " + b);
System.out.println("Addition (a + b): " + (a + b));
System.out.println("Subtraction (a - b): " + (a - b));
System.out.println("Multiplication (a * b): " + (a * b));
System.out.println("Division (a / b): " + (a / b));
System.out.println("Modulus (a % b): " + (a % b));
// Increment and Decrement
int x = 5;
System.out.println("\nIncrement/Decrement:");
System.out.println("x = " + x);
System.out.println("x++ (post-increment): " + (x++)); // Returns 5, then increments to 6
System.out.println("x is now: " + x);
System.out.println("++x (pre-increment): " + (++x)); // Increments to 7, then returns 7
System.out.println("x-- (post-decrement): " + (x--)); // Returns 7, then decrements to 6
System.out.println("x is now: " + x);
}
/**
* Demonstrates relational (comparison) operators
*
* Operators: ==, !=, >, <, >=, <=
* Result: boolean (true/false)
*/
private static void demonstrateRelationalOperators() {
System.out.println("\n4. RELATIONAL (COMPARISON) OPERATORS");
System.out.println("-".repeat(50));
int a = 10, b = 20;
System.out.println("a = " + a + ", b = " + b);
System.out.println("a == b (equal to): " + (a == b));
System.out.println("a != b (not equal to): " + (a != b));
System.out.println("a > b (greater than): " + (a > b));
System.out.println("a < b (less than): " + (a < b));
System.out.println("a >= b (greater than or equal): " + (a >= b));
System.out.println("a <= b (less than or equal): " + (a <= b));
}
/**
* Demonstrates logical operators
*
* Operators: && (AND), || (OR), ! (NOT)
* Used to combine multiple boolean expressions
*/
private static void demonstrateLogicalOperators() {
System.out.println("\n5. LOGICAL OPERATORS");
System.out.println("-".repeat(50));
boolean x = true, y = false;
System.out.println("x = " + x + ", y = " + y);
System.out.println("x && y (Logical AND): " + (x && y)); // Both must be true
System.out.println("x || y (Logical OR): " + (x || y)); // At least one must be true
System.out.println("!x (Logical NOT): " + (!x)); // Negates the value
// Practical example
int age = 25;
boolean hasLicense = true;
boolean canDrive = (age >= 18) && hasLicense;
System.out.println("\nPractical Example:");
System.out.println("Age: " + age + ", Has License: " + hasLicense);
System.out.println("Can Drive: " + canDrive);
}
/**
* Demonstrates conditional statements
*
* Statements: if, if-else, if-else-if, switch, ternary operator
*/
private static void demonstrateConditionalStatements() {
System.out.println("\n6. CONDITIONAL STATEMENTS");
System.out.println("-".repeat(50));
// if statement
int score = 85;
System.out.println("Score: " + score);
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
// switch statement
int dayOfWeek = 3;
String dayName;
switch (dayOfWeek) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
System.out.println("\nDay " + dayOfWeek + " is: " + dayName);
// Ternary operator (shorthand for if-else)
int number = 15;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println("\n" + number + " is: " + result);
}
/**
* Demonstrates different types of loops
*
* Loops: for, while, do-while, enhanced for (for-each)
*/
private static void demonstrateLoops() {
System.out.println("\n7. LOOPS");
System.out.println("-".repeat(50));
// for loop
System.out.println("For Loop (1 to 5):");
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
System.out.println();
// while loop
System.out.println("\nWhile Loop (countdown from 5):");
int count = 5;
while (count > 0) {
System.out.print(count + " ");
count--;
}
System.out.println();
// do-while loop (executes at least once)
System.out.println("\nDo-While Loop:");
int num = 1;
do {
System.out.print(num + " ");
num++;
} while (num <= 5);
System.out.println();
// Enhanced for loop (for-each)
System.out.println("\nEnhanced For Loop (array iteration):");
int[] numbers = {10, 20, 30, 40, 50};
for (int n : numbers) {
System.out.print(n + " ");
}
System.out.println();
// Break and Continue
System.out.println("\nBreak and Continue:");
System.out.print("With break (stops at 5): ");
for (int i = 1; i <= 10; i++) {
if (i == 6) break; // Exit loop when i is 6
System.out.print(i + " ");
}
System.out.println();
System.out.print("With continue (skips 5): ");
for (int i = 1; i <= 10; i++) {
if (i == 5) continue; // Skip iteration when i is 5
System.out.print(i + " ");
}
System.out.println();
}
/**
* Demonstrates type conversion and casting
*
* Automatic (Widening): smaller to larger type (e.g., int to double)
* Manual (Narrowing): larger to smaller type (e.g., double to int) - requires casting
*/
private static void demonstrateTypeConversion() {
System.out.println("\n8. TYPE CONVERSION AND CASTING");
System.out.println("-".repeat(50));
// Automatic type conversion (widening)
int intValue = 100;
double doubleValue = intValue; // int to double (automatic)
System.out.println("Automatic Conversion (int to double):");
System.out.println("int: " + intValue + " → double: " + doubleValue);
// Manual type casting (narrowing)
double pi = 3.14159;
int intPi = (int) pi; // double to int (manual casting, loses decimal part)
System.out.println("\nManual Casting (double to int):");
System.out.println("double: " + pi + " → int: " + intPi + " (decimal part lost)");
// Char and int conversion
char letter = 'A';
int asciiValue = letter; // char to int (gets ASCII value)
System.out.println("\nChar to int conversion:");
System.out.println("char: '" + letter + "' → ASCII: " + asciiValue);
char fromAscii = (char) 66; // int to char
System.out.println("int: 66 → char: '" + fromAscii + "'");
// String to number conversion
String numberString = "123";
int parsedInt = Integer.parseInt(numberString);
System.out.println("\nString to int:");
System.out.println("String: \"" + numberString + "\" → int: " + parsedInt);
// Number to String conversion
int value = 456;
String valueString = String.valueOf(value);
System.out.println("\nint to String:");
System.out.println("int: " + value + " → String: \"" + valueString + "\"");
}
}