Java Cheat Sheet
Java Cheat Sheet
here's a Java cheat sheet that covers some of the most common and
useful syntax and concepts for Java beginners:
Basics
class ClassName { ... }: Defines a new class
public static void main(String[] args) { ... }: Main method entry
point
System.out.println("Hello, World!");: Prints a message to the
console
Variables and Data Types
dataType variableName = value;: Declares and initializes a variable
int, double, boolean, String, etc.: Common data types
final dataType CONSTANT_NAME = value;: Declares a constant
Operators
+, -, *, /, %: Arithmetic operators
=, +=, -=, *=, /=, %=: Assignment operators
<, >, <=, >=, ==, !=: Comparison operators
&&, ||, !: Logical operators
Control Flow
if (condition) { ... } else { ... }: Conditional statement
switch (expression) { case value: ... default: ... }: Switch
statement
for (initialization; condition; increment) { ... }: For loop
while (condition) { ... }: While loop
do { ... } while (condition);: Do-while loop
break;, continue;: Flow control statements
Arrays
dataType[] arrayName = {value1, value2, ...};: Array declaration and
initialization
arrayName.length: Get the length of an array
Methods
returnType methodName(parameters) { ... }: Method declaration
methodName(arguments);: Method call
Exception Handling
try { ... } catch (ExceptionType e) { ... }: Try-catch block
throw new ExceptionType("message");: Throw an exception
finally { ... }: Finally block
Access Modifiers
public, private, protected, default: Access levels for classes,
methods, and variables
Object-Oriented Programming
class ClassName { ... }: Class definition
ClassName objectName = new ClassName();: Create an object
extends: Inheritance
implements: Interfaces
Strings
String str = "Hello, World!";: String declaration and initialization
str.length(), str.charAt(index), str.substring(start, end), etc.: String
methods
Collections
List<dataType> listName = new ArrayList<>();: Create an ArrayList
Map<keyType, valueType> mapName = new HashMap<>();: Create a
HashMap
This cheat sheet covers the basics of Java programming, including
data types, control flow, arrays, methods, exception handling, and
object-oriented programming. As you continue to learn Java, you can
expand on this foundation by exploring more advanced topics and
concepts.
Remember, the best way to learn is by practicing. Try to write
simple programs and experiment with the different features and
syntax covered in this cheat sheet.
let's add some information about file handling in Java to the cheat
sheet:
File Handling
Reading from a File
File file = new File("filename.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = bufferedReader.readLine();
while (line != null) { /* process line */ line =
bufferedReader.readLine(); }
bufferedReader.close();
Writing to a File
File file = new File("filename.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("Hello, World!");
bufferedWriter.newLine();
bufferedWriter.close();
Appending to a File
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("Append this text.");
bufferedWriter.newLine();
bufferedWriter.close();
Checking File Existence
File file = new File("filename.txt");
if (file.exists()) { /* file exists */ }
Creating a New File
File file = new File("filename.txt");
if (file.createNewFile()) { /* file created successfully */ }
Deleting a File
File file = new File("filename.txt");
if (file.delete()) { /* file deleted successfully */ }
Handling Exceptions
try { /* file operations */ } catch (IOException e) { /* handle
exceptions */ }
This covers the basics of file handling in Java, including reading
from, writing to, and appending to files, as well as checking file
existence, creating new files, and deleting files. Remember to
always handle exceptions when working with files to ensure your
program handles errors gracefully.