Java Quick Reference Guide

Java, a high-level, class-based, object-oriented programming language, is widely used for building enterprise-scale applications. This quick reference guide will help you get started with Java, providing essential syntax, features, and best practices.

Environment Setup
Before you start coding, ensure you have the Java Development Kit (JDK) installed. You can download it from the official Oracle website. Once installed, set the JAVA_HOME and PATH environment variables accordingly.

Hello World in Java
Here's the classic "Hello, World!" program in Java:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
To run this program, save it as HelloWorld.java, then compile it using the command javac HelloWorld.java and run it with java HelloWorld.
Java Syntax and Basic Types
- Variables and Data Types: Java is statically typed. Declare variables with their data types, e.g.,
int x = 10;. - Operators: Java supports arithmetic, logical, bitwise, and assignment operators.
- Control Structures: Use
if-else,switch,for,while, anddo-whileloops for decision-making and iteration.

Object-Oriented Programming (OOP)
Java follows OOP principles. Here's a simple class definition:
public class Dog {
String breed;
String color;
public void bark() {
System.out.println("Woof!");
}
}
Create an object using the new keyword, e.g., Dog myDog = new Dog();.

Methods and Access Modifiers
Methods are defined using the public, private, or protected access modifiers. The main method, which is the entry point of any Java application, must be defined as public static void main(String[] args).




















Java Collections Framework
| Interface/Class | Description |
|---|---|
| List (ArrayList, LinkedList) | Ordered collection (allows duplicates). |
| Set (HashSet, TreeSet) | Unordered collection (no duplicates). |
| Map (HashMap, TreeMap) | Key-value pairs (no duplicate keys). |
Best Practices
- Follow naming conventions: variable names in camelCase, class names in PascalCase.
- Use meaningful variable and method names.
- Minimize the use of global variables.
- Comment your code using
//for single-line comments and/* ... */for multi-line comments.