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 ArrayLists and LinkedLists to the
Java cheat sheet:
ArrayLists
ArrayList<dataType> listName = new ArrayList<>();: Declare and
initialize an ArrayList
listName.add(item);: Add an item to the ArrayList
listName.get(index);: Retrieve an item by index
listName.set(index, newItem);: Update an item at a specific index
listName.remove(index);: Remove an item by index
listName.size();: Get the size of the ArrayList
listName.contains(item);: Check if the ArrayList contains a specific
item
listName.indexOf(item);: Get the index of the first occurrence of an
item
LinkedLists
LinkedList<dataType> listName = new LinkedList<>();: Declare and
initialize a LinkedList
listName.add(item);: Add an item to the end of the LinkedList
listName.addFirst(item);: Add an item to the beginning of the
LinkedList
listName.get(index);: Retrieve an item by index
listName.set(index, newItem);: Update an item at a specific index
listName.remove(index);: Remove an item by index
listName.removeFirst();: Remove the first item from the LinkedList
listName.removeLast();: Remove the last item from the LinkedList
listName.size();: Get the size of the LinkedList
listName.contains(item);: Check if the LinkedList contains a
specific item
listName.indexOf(item);: Get the index of the first occurrence of an
item
The main differences between ArrayLists and LinkedLists are:
ArrayLists are based on arrays, which provide constant-time
access to elements by index, but inserting or removing
elements can be slow.
LinkedLists are based on a doubly-linked list, which provides
constant-time access to the first and last elements, but
accessing elements by index can be slower.
ArrayLists are better suited for random access, while
LinkedLists are better suited for frequent insertions and
deletions at the beginning or end of the list.
When choosing between an ArrayList and a LinkedList, consider the
specific requirements of your application and the operations you'll
be performing on the list.
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.