-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjava_reference.java
More file actions
991 lines (845 loc) · 31.4 KB
/
java_reference.java
File metadata and controls
991 lines (845 loc) · 31.4 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
// ---------------------------------------------------------------------------------
// Java Reference and Guide
//
// ReferenceCollection.com
// Licensed under CC BY-SA
// ---------------------------------------------------------------------------------
// TABLE OF CONTENTS
// -----------------
// 1. Introduction to Java
// 2. Basic Syntax and Structure
// 3. Data Types and Variables
// 4. Operators
// 5. Control Flow Statements
// 6. Arrays
// 7. Collections
// 8. Methods
// 9. Object-Oriented Programming
// 10. Exception Handling
// 11. File Input/Output (I/O)
// 12. Generics
// ---------------------------------------------------------------------------------
// 1. Introduction to Java
// ---------------------------------------------------------------------------------
// Java is an object-oriented programming language designed for platform independence,
// meaning code runs on any device with a JVM. Known for its simplicity, security, and performance.
// Key Features:
// - Platform Independence: Code runs on any device with a JVM
// - Object-Oriented: Everything is an object (except primitives)
// - Strongly Typed: Type checking at compile time
// - Automatic Memory Management: Garbage collection
// - Rich Standard Library: Extensive built-in functionality
// - Security: Built-in security features
// Getting Started:
// - Install JDK (Java Development Kit)
// - Set up JAVA_HOME environment variable
// - Use 'javac' to compile: javac MyProgram.java
// - Use 'java' to run: java MyProgram
// Java Virtual Machine (JVM):
// A virtual machine that runs java on any platform, it handles memory management, garbage
// collection, and provides a runtime environment for executing Java applications.
// ---------------------------------------------------------------------------------
// 2. Basic Syntax and Structure
// ---------------------------------------------------------------------------------
// A Java program consists of classes, methods, variables, and statements. The 'main' method
// is the entry point for execution.
// What a Java Program Consists Of:
// 1. Classes: Blueprints for creating objects.
// 2. Methods: Functions that perform actions. The 'main' method is essential for execution.
// 3. Variables: Named storage locations for data.
// 4. Statements: Individual actions that perform operations.
// Syntax Highlights:
// - Case Sensitivity: Variable and class names are case-sensitive.
// - Semicolons: Each statement ends with a semicolon ';'.
// - Curly Braces: '{ }' define code blocks.
// - Comments: Use '//' for single-line and '/* ... */' for multi-line comments.
// - Access Modifiers: Keywords like public, private, protected to control accessibility.
// Example Program:
public class HelloWorld { // Class name must match file name 'HelloWorld.java'
public static void main(String[] args) { // Entry point
System.out.println("Hello, World!"); // Print to console
}
}
// ---------------------------------------------------------------------------------
// 3. Variables and Data Types
// ---------------------------------------------------------------------------------
class VariablesAndDataTypes {
public static void main(String[] args) {
/*
* Primitive Data Types:
* The eight basic data types, for storing simple values.
*/
byte byteVariable = 12; // Range: -128 to 127
short shortVariable = 3200; // Range: -32,768 to 32,767
int intVariable = 2147; // Range: -2^31 to 2^31-1
long longVariable = 2335807L; // Range: -2^63 to 2^63-1
float floatVariable = 3.14f; // Range: 1.4e-45 to 3.4e+38
double doubleVariable = 3.1415; // Range: 4.9e-324 to 1.8e+308
char charVariable = 'A'; // Range: '\u0000' to '\uffff'
boolean booleanVariable = true; // true or false
// Constants: values that remain unchanged.
final double PI = 3.14159;
final double RADIUS = 5.0;
/*
* Reference Data Types:
* Used to store data that reference memory locations,
* like arrays, objects, interfaces, etc.
*/
String stringVariable = "Hello, World!";
int[] numbers = {1, 2, 3};
Integer num = new Integer(10);
Double dbl = new Double(3.14);
JavaReference javaReference = new JavaReference();
/*
* Type Conversion (Casting):
* Casting is the process of converting one data type into another.
* Implicit conversion (widening) happens automatically with no data is lost.
* Explicit conversion (narrowing) uses casting operators and may lose data.
*/
int typeInt = 7;
double typeDouble = typeInt; // Implicit (int to double)
System.out.println(typeDouble); // 7.0
double numDouble = 3.14;
int numInt = (int) numDouble; // Explicit (double to int)
System.out.println(numInt); // 3
}
}
// ---------------------------------------------------------------------------------
// 4. Operators
// ---------------------------------------------------------------------------------
class Operators {
public static void main(String[] args) {
int a = 2;
int b = 5;
/*
* Arithmetic Operators:
* Perform basic mathematical operations
*/
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus
/*
* Assignment Operators:
* Assigning values with operations
*/
a += 5; // Add (a = a + 5)
a -= 3; // Subtract (a = a - 3)
a *= 2; // Multiply (a = a * 2)
a /= 5; // Divide (a = a / 5)
a %= 7; // Modulus (a = a % 7)
/*
* Comparison Operators:
* Comparing values, returns true or false
*/
boolean isEqual = (a == b); // Equality
boolean isGreater = (a > b); // Greater than
boolean isLesser = (a < b); // Less than
boolean notEqual = (a != b); // Not equal to
boolean greaterOrEqual = (a >= b); // Greater than or equal to
boolean lesserOrEqual = (a <= b); // Less than or equal to
/*
* Logical Operators:
* Combine multiple conditions, returns true or false
*/
boolean x = true;
boolean y = false;
boolean logicalAnd = x && y; // Logical AND
boolean logicalOr = x || y; // Logical OR
boolean logicalNotX = !x; // Logical NOT
/*
* Bitwise Operators:
* Perform operations at bit level
*/
int bitwiseAnd = a & b; // Bitwise AND
int bitwiseOr = a | b; // Bitwise OR
int bitwiseXor = a ^ b; // Bitwise XOR
int bitwiseComplement = ~a; // Bitwise Complement
int leftShift = a << 1; // Left Shift
int rightShift = a >> 1; // Right Shift
/*
* Unary Operators:
* Unary operators operate on a single operand.
*/
int pre_increment = (++a); // Pre-increment
int pre_decrement = (--a); // Pre-decrement
int post_increment = (a++); // Post-increment
int post_decrement = (a--); // Post-decrement
int positive = (+a); // Positive
int negative = (-a); // Negative
boolean logical = (!true); // Logical negation
/*
* Ternary Operator:
* Concise way to write conditional expressions.
* Syntax: (condition) ? (if-true) : (if-false)
*/
int score = 65;
String result = (score >= 50) ? "Pass" : "Fail";
}
}
// ---------------------------------------------------------------------------------
// 5. Control Flow Statements
// ---------------------------------------------------------------------------------
class ControlFlowStatements {
public static void main(String[] args) {
// Conditional statements:
/*
* if-else statement:
* Executes a code block if a condition is true.
*/
int score = 65;
if (score > 50) {
System.out.println("score > 50");
} else if (score < 50) {
System.out.println("score < 50");
} else {
System.out.println("score = 50");
}
/*
* switch statement:
* Executes a code block based on the value of a variable.
*/
int choice = 2;
switch (choice) {
case 1:
System.out.println("Choice is 1");
break;
case 2:
System.out.println("Choice is 2");
break;
default:
System.out.println("Invalid choice");
}
// Looping statements:
/*
* for statement:
* Repeat a code block until a condition is false.
*/
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
/*
* while statement:
* Repeat a code block until a condition is true.
*/
int i = 1;
while (i <= 5) {
System.out.println("Iteration " + i);
i++;
}
/*
* do-while statement:
* Similar to 'while', but the code block is executed at least once.
*/
int j = 1;
do {
System.out.println("Iteration " + j);
j++;
} while (j <= 5);
/*
* for statement:
* Iterate over arrays or collections.
*/
int[] numbers = {1, 2, 3};
for (int number : numbers) {
System.out.println("Number: " + number);
}
// Transfer statements:
/*
* break statement:
* Exit loop or switch statement.
*/
for (int m = 0; m < 10; m++) {
if (m == 7) {
break;
}
}
/*
* continue statement:
* Skip current iteration, proceed with next.
*/
for (int m = 0; m < 10; m++) {
if (m == 3) {
continue;
}
}
/*
* return statement:
* Exit method, optionally sending back value.
*/
class ReturnStatement {
public int multiply(int a, int b) {
return a * b;
}
}
}
}
// ---------------------------------------------------------------------------------
// 6. Arrays
// ---------------------------------------------------------------------------------
class Arrays {
public static void main(String[] args) {
/*
* Declaration and Initialization:
* Arrays are objects that store multiple variables of the same type.
*/
double[] doubleArray = new double[5]; // Initialization with size 5
int[] intArray = new int[]{1, 2, 3, 4, 5};
int[] fibonacci = {0, 1, 1, 2, 3, 5};
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
String[] stringArray = {"apple", "banana", "orange"};
/*
* Accessing Array Elements:
* Elements are accessed using index numbers, starting from 0.
*/
System.out.println("First element: " + intArray[0]);
System.out.println("Second element: " + intArray[1]);
/*
* Modifying Elements:
* Arrays can be modified by assigning new values to them.
*/
doubleArray[0] = 10;
doubleArray[1] = 20;
// Iterating Through Arrays:
for (int f = 0; f < fibonacci.length; f++) {
System.out.print(fibonacci[f] + " ");
}
/*
* Multidimensional Arrays:
* Arrays can have more than one dimension.
*/
int[][] matrix = new int[3][3]; // Declaration and initialization of a 3x3 matrix
matrix[0][0] = 1; // Assigning value (0,0)
matrix[0][1] = 2; // Assigning value (0,1)
System.out.println("Element at (0,1): " + matrix[0][1]);
}
}
// ---------------------------------------------------------------------------------
// 7. Collections
// ---------------------------------------------------------------------------------
class Collections {
public static void main(String[] args) {
/*
* List: ArrayList:
* Lists maintain the order of elements and allow duplicate elements.
*/
List<String> arrayList = new ArrayList<>();
arrayList.add("one");
arrayList.add("two");
/*
* List: LinkedList
* A sequence of elements, each element is connected to its next and previous elements
*/
List<String> linkedList = new LinkedList<>();
linkedList.add("one");
linkedList.add("two");
/*
* Set: HashSet
* Stores unique elements, without maintaining any order.
*/
Set<String> hashSet = new hashSet<>();
linkedList.add("one");
linkedList.add("two");
/*
* Set: TreeSet
* Sorted set that stores unique elements in ascending order.
*/
Set<String> treeSet = new TreeSet<>();
linkedList.add("one");
linkedList.add("two");
/*
* Map: HashMap
* Stores key-value pairs, allowing fast retrieval of values by their keys.
*/
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("one", 1);
hashMap.put("two", 2);
/*
* Map: TreeMap
* Sorted map that stores key-value pairs in sorted order based on the keys.
*/
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("one", 1);
treeMap.put("two", 2);
/*
* Iterating Through Lists/Sets:
* Use for-each loop or iterators.
*/
for (String item : hashSet) {
System.out.println(item);
}
Iterator<String> arrayListIterator = hashSet.iterator();
while (arrayListIterator.hasNext()) {
System.out.println(arrayListIterator.next());
}
/*
* Iterating Through Maps:
* Use for-each loop or iterators.
*/
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + " > " + entry.getValue());
}
Iterator<Map.Entry<String, Integer>> hashMapIterator = hashMap.entrySet().iterator();
while (hashMapIterator.hasNext()) {
Map.Entry<String, Integer> entry = hashMapIterator.next();
System.out.println(entry.getKey() + " > " + entry.getValue());
}
/*
* Collection Algorithms:
* Java provides different methods to work with collections.
*/
List<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
// Sort
Collections.sort(numbers);
// Sum
int sum = Collections.sum(numbers);
System.out.println("Sum of Numbers: " + sum);
// Reverse
Collections.reverse(numbers);
System.out.println("Reversed Numbers: " + numbers);
}
}
// ---------------------------------------------------------------------------------
// 8. Methods
// ---------------------------------------------------------------------------------
class Methods {
public static void main(String[] args) {
/*
* Method Declaration:
* Methods are reusable blocks of code.
* Syntax: <access modifier> <return type> <method name>(<parameter list>)
*/
public void greet() {
System.out.println("Hello, world!");
}
/*
* Access modifiers:
* Access modifiers define the accessibility of a method.
* - public: Accessible from any class
* - private: Accessible only within the same class
* - protected: Accessible within the same package and subclasses
*/
protected int substract(int x, int y) {
return x - y;
}
/*
* Recursion:
* Method calls itself, known as recursion.
*/
public int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}
}
// ---------------------------------------------------------------------------------
// 9. Object-Oriented Programming
// ---------------------------------------------------------------------------------
class OOP {
public static void main(String[] args) {
/*
* Classes:
* Class is a blueprint for creating objects.
*/
class Car {
// Variables
String brand;
String model;
// Constructor without parameters and with default value
public Car() {
brand = "Unknown";
model = "Unknown";
}
// Constructor with parameters
public Car(String brand, String model) {
this.brand = brand;
this.model = model;
}
// Method
public void displayCarInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
}
}
/*
* Objects:
* Objects are instances of classes.
*/
Car honda = new Car("Honda", "Accord");
// Accessing object property and method
System.out.println("Honda model: " + honda.model);
honda.displayCarInfo();
/*
* Encapsulation:
* Hide the internal state of an object, and only allows access through methods.
*/
class Wallet {
// private variables
private double balance;
// Getter and Setter methods for encapsulated fields
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
Wallet wallet = new Wallet();
wallet.setBalance(1000);
System.out.println("Balance: " + wallet.getBalance());
/*
* Inheritance:
* Allows a class to inherit properties and behavior from another class.
*/
class ElectricCar extends Car {
// specific field to ElectricCar
private int batteryLife;
// Constructor including fields from Car (super) class and ElectricCar class
public ElectricCar(String make, String model, int batteryLife) {
super(make, model);
this.batteryLife = batteryLife;
}
/*
* Method overriding:
* Providing a specific implementation for a method
* already defined in its superclass (car).
*/
@Override
public void displayCarInfo() {
super.displayCarInfo();
System.out.println("Battery Life: " + batteryLife + " miles");
}
/*
* Method overloading:
* Defining multiple methods with the same name but different parameters.
*/
public void displayCarInfo(boolean includeBatteryLife) {
super.displayCarInfo();
if (includeBatteryLife) {
System.out.println("Battery Life: " + batteryLife + " miles");
}
}
// Additional method specific to ElectricCar
public void charge() {
System.out.println("Charging the car...");
}
}
/*
* Polymorphism:
* Lets different objects be treated similarly, even if they're from different classes
*/
Car teslaModelS = new ElectricCar("Tesla", "Model S", 400);
// Calls the overridden displayCarInfo() method in ElectricCar
teslaModelS.displayCarInfo();
/*
* Abstraction:
* Is about hiding implementation details and showing only essential features.
* Abstract classes can contain both implemented and abstract methods.
* Abstract Classes can only extend one abstract class.
*/
abstract class Vehicle {
abstract void operate();
}
class Car extends Vehicle {
@Override
void operate() {
System.out.println("Operating Car");
}
}
class Motorcycle extends Vehicle {
@Override
void operate() {
System.out.println("Operating Motorcycle");
}
}
Vehicle myCar = new Car();
myCar.operate(); // Operating Car
Vehicle myMotorcycle = new Motorcycle();
myMotorcycle.operate(); // Operating Motorcycle
/*
* Interfaces:
* Define a contract for classes, specifying methods they must implement.
* Classes can implement multiple interfaces.
*/
interface MessagingService {
void sendMessage(String message, String recipient);
}
class EmailService implements MessagingService {
@Override
public void sendMessage(String message, String recipient) {
System.out.println("Sending email to " + recipient + ": " + message);
}
}
MessagingService emailService = new EmailService();
emailService.sendMessage("Hello, how are you?", "[email protected]");
}
}
// ---------------------------------------------------------------------------------
// 10. Exception Handling
// ---------------------------------------------------------------------------------
class ExceptionHandling {
public static void main(String[] args) {
/*
* Try-Catch Blocks:
* Handle potential errors or exceptions in code.
*/
int[] intArray = {1, 2, 3, 4, 5};
try {
System.out.println(intArray[10]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());
}
/*
* Multiple Catch Blocks:
* Handling different types of exceptions separately.
*/
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[4]); // Index out of bounds exception
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds");
} catch (Exception e) {
System.out.println("An error occurred");
}
/*
* Finally Block:
* Finally block get executed regardless of an exception or not.
*/
try {
int result = 10 / 0;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero");
} finally {
System.out.println("Inside finally block");
}
/*
* Custom Exceptions:
* You can create your own exceptions to handle specific situations.
*/
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
/*
* Throwing Exceptions:
* You can manually throw exceptions to handle specific cases.
*/
class ThrowExceptionExample {
public static void main(String[] args) {
try {
validateAge(15);
} catch (InvalidAgeException e) {
System.out.println("Invalid age: " + e.getMessage());
}
}
public static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be at least 18");
}
}
}
}
}
// ---------------------------------------------------------------------------------
// 11. File Input/Output (I/O)
// ---------------------------------------------------------------------------------
class InputOuput {
public static void main(String[] args) {
/*
* File Handling:
* Managing files on a computer system.
*/
try {
// Creating a new file
File file = new File("newfile.txt");
if (file.createNewFile()) {
System.out.println("File created successfully.");
} else {
System.out.println("File already exists.");
}
// Deleting the file
if (file.delete()) {
System.out.println("File deleted successfully.");
} else {
System.out.println("Failed to delete the file.");
}
// Check if a file exists
File existingFile = new File("newfile.txt");
if (existingFile.exists()) {
System.out.println("File exists: " + existingFile.getName());
} else {
System.out.println("File does not exist.");
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
}
/*
* Writing to Files:
* Ways of writing data to a file.
*/
try {
String content = "Hello, this is a sample text.";
File file = new File("filename.txt");
// Using FileOutputStream
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes());
fos.close();
// Using FileWriter
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(content);
fileWriter.close();
// Using BufferedWriter
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(content);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
/*
* Reading from Files:
* Ways of reading data from a file.
*/
try {
File file = new File("filename.txt");
// Using FileInputStream
FileInputStream fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
fis.close();
// Using FileReader
FileReader fileReader = new FileReader(file);
int data;
while ((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
fileReader.close();
// Using BufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
// Using Scanner
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// ---------------------------------------------------------------------------------
// 12. Generics
// ---------------------------------------------------------------------------------
// Create classes, interfaces, and methods that operate with parameterized types.
// The letter 'T' represents the type parameter.
class Generics {
public static void main(String[] args) {
/*
* Generic Classes
* Classes with type parameters for flexible and type-safe code.
*/
class Shape<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
Shape<String> stringBox = new Shape<>();
stringBox.set("Hello World");
Shape<Integer> integerBox = new Shape<>();
integerBox.set(100);
/*
* Generic Methods:
* Methods with type parameters for flexibility.
*/
class GenericMethod {
public static <T> void printList(List<T> list) {
for (T item : list) {
System.out.println(item);
}
}
public static void main(String[] args) {
List<String> stringArrayList = new ArrayList<>();
stringArrayList.add("one");
List<Integer> integerArrayList = new ArrayList<>();
integerArrayList.add(100);
printList(stringArrayList);
printList(integerArrayList);
}
}
/*
* Wildcards:
* Flexible typing for methods with unknown types.
* There are three types of wildcards: ?, ? extends T, and ? super T.
*/
class Wildcards {
public static void printListWildcard(List<?> list) {
for (Object item : list) {
System.out.println(item);
}
}
public static void main(String[] args) {
List<String> stringArrayList = new ArrayList<>();
stringArrayList.add("one");
List<Integer> integerArrayList = new ArrayList<>();
integerArrayList.add(100);
printListWildcard(stringArrayList);
printListWildcard(integerArrayList);
}
}
/*
* Bounded Type Parameters:
* You can restrict the types that can be passed as type arguments
* There are two kinds of bounds: upper bound and lower bound.
*/
class MathUtils {
// Upper Bound: Accepts types that extend Number
public static <T extends Number> double findMaxValue(List<T> list) {
double max = Double.NEGATIVE_INFINITY;
for (T num : list) {
if (num.doubleValue() > max) {
max = num.doubleValue();
}
}
return max;
}
// Lower Bound: Accepts types that are superclasses of Integer
public static int sum(List<? super Integer> list) {
int sum = 0;
for (Object obj : list) {
if (obj instanceof Integer) {
sum += (Integer) obj;
}
}
return sum;
}
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
integerList.add(5);
integerList.add(10);
integerList.add(15);
System.out.println("Sum of integers: " + MathUtils.sum(integerList));
System.out.println("Maximum value: " + MathUtils.findMaxValue(integerList));
}
}
}
}