Skip to content

Commit a034ed7

Browse files
authored
Merge pull request #1 from recovery12/main
Added so cheatsheet information
2 parents c42c45a + cd8bb28 commit a034ed7

File tree

1 file changed

+167
-1
lines changed

1 file changed

+167
-1
lines changed

README.md

+167-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,170 @@ Java Data Types are divided into two groups:
3636
* int
3737
* long
3838
* float
39-
39+
* double
40+
* char
41+
* boolean
42+
* Non-Primitive Data Types
43+
* String
44+
* Array
45+
* Classes
46+
* Interface
47+
48+
## Operators
49+
50+
Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java provides a rich set of operators to manipulate variables.
51+
52+
* Arithmetic Operators
53+
* Relational Operators
54+
* Logical Operators
55+
* Bitwise Operators
56+
* Assignment Operators
57+
* Conditional Operators
58+
59+
## Control Statements
60+
61+
Control statements are used to control the flow of execution in a program. In Java, we have the following control statements:
62+
63+
* if
64+
* if-else
65+
* switch
66+
* while
67+
* do-while
68+
* for
69+
* break
70+
* continue
71+
72+
## Classes and Objects
73+
74+
Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
75+
76+
```java
77+
class Car {
78+
int modelYear;
79+
String modelName;
80+
81+
public Car(int year, String name) {
82+
modelYear = year;
83+
modelName = name;
84+
}
85+
86+
public static void main(String[] args) {
87+
Car myCar = new Car(1969, "Mustang");
88+
System.out.println(myCar.modelYear + " " + myCar.modelName);
89+
}
90+
}
91+
```
92+
93+
## Multi-Threading
94+
95+
Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
96+
97+
```java
98+
class MultiThreadingDemo extends Thread {
99+
public void run() {
100+
try {
101+
System.out.println("Thread " + Thread.currentThread().getId() + " is running");
102+
} catch (Exception e) {
103+
System.out.println("Exception is caught");
104+
}
105+
}
106+
107+
public static void main(String[] args) {
108+
int n = 8;
109+
for (int i = 0; i < n; i++) {
110+
MultiThreadingDemo object = new MultiThreadingDemo();
111+
object.start();
112+
}
113+
}
114+
}
115+
```
116+
117+
## Collections
118+
119+
Java Collections Framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. The JCF provides both interfaces that define various collections and classes that implement them.
120+
121+
* List
122+
* Set
123+
* Map
124+
* Queue
125+
* Deque
126+
127+
## Generics
128+
129+
Generics in Java is similar to templates in C++. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well.
130+
131+
```java
132+
class Test<T> {
133+
T obj;
134+
135+
Test(T obj) {
136+
this.obj = obj;
137+
}
138+
139+
public T getObject() {
140+
return this.obj;
141+
}
142+
}
143+
```
144+
145+
## Exception Handling
146+
147+
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. In Java, an exception is an object that wraps an error event that occurred within a method and contains:
148+
149+
* Information about the error including its type
150+
* The state of the program when the error occurred
151+
* Optionally, other custom information about the error
152+
153+
```java
154+
public class Main {
155+
public static void main(String[] args) {
156+
try {
157+
int a[] = new int[2];
158+
System.out.println("Access element three: " + a[3]);
159+
} catch (ArrayIndexOutOfBoundsException e) {
160+
System.out.println("Exception thrown: " + e);
161+
}
162+
System.out.println("Out of the block");
163+
}
164+
}
165+
```
166+
167+
## File Handling
168+
169+
Java provides the `java.io` package for file handling. File handling in Java can be done by Java I/O API. The Java.io package contains all the classes required for input and output operations.
170+
171+
* File
172+
* FileInputStream
173+
* FileOutputStream
174+
* BufferedInputStream
175+
* BufferedOutputStream
176+
* DataInputStream
177+
* DataOutputStream
178+
* SequenceInputStream
179+
* BufferedInputStream
180+
* BufferedOutputStream
181+
* PrintWriter
182+
* FileReader
183+
* FileWriter
184+
* BufferedReader
185+
* BufferedWriter
186+
187+
```java
188+
import java.io.File;
189+
import java.io.FileWriter;
190+
import java.io.IOException;
191+
192+
public class Main {
193+
public static void main(String[] args) {
194+
try {
195+
FileWriter myWriter = new FileWriter("filename.txt");
196+
myWriter.write("Files in Java might be tricky, but it is fun enough!");
197+
myWriter.close();
198+
System.out.println("Successfully wrote to the file.");
199+
} catch (IOException e) {
200+
System.out.println("An error occurred.");
201+
e.printStackTrace();
202+
}
203+
}
204+
}
205+
```

0 commit comments

Comments
 (0)