Use System.out.println() to print to the console.
System.out.println("Hello, world!"); // Output: Hello, world!
Comments are ignored by the compiler and improve code readability.
// Single-line comment /* Multi-line comment */
The main() method is the entry point for any Java application.
public class Person { public static void main(String[] args) { System.out.println("Hello, world!"); } }
A class in Java is a type of blueprint for creating objects. The class name should match the filename for public classes, and the main() method must be included in a public class with the same name as the file to run the application.
public class Person { public static void main(String[] args) { System.out.println("I am a person, not a computer."); } }
Compile a Java class file with javac and run it with java.
# Compile: javac hello.java # Run: java hello
Whitespace in Java is used to separate statements and declarations, making code more readable. Ensure the class name matches the filename for public classes. Extra whitespace is ignored by the compiler but should be used to improve code formatting.
System.out.println("Example of a statement"); System.out.println("Another statement");
Statements in Java are individual instructions that the compiler executes. They end with a semicolon and include expressions and commands.
System.out.println("Java Programming ☕️");
Java compiles programs into .class files containing bytecode. This bytecode is executed by Java Virtual Machine (JVM), which abstracts away the underlying hardware, contributing to both security and portability.
The .class file is not directly executable by the operating system. It requires the JVM to run.
Bytecode runs on the Java Virtual Machine (JVM), which provides a layer of abstraction between the code and the hardware. This separation enhances portability across different operating systems and devices. Additionally, Java's security features help prevent unauthorized access and ensure safe execution of code.
Java applications can run on any device with a JVM, ensuring consistent behavior across platforms.
The boolean type holds true or false values.
boolean result = true; boolean isMarried = false;
Strings in Java are objects that hold sequences of characters.
String name = "Bob"; System.out.println(name.equals("bob")); // false
The int type holds integer values.
int num1 = 10; int num2 = -5; int num3 = 0;
The char type holds a single character enclosed in single quotes.
char answer = 'y';
Java has various primitive data types, including int, char, boolean, byte, long, short, float, and double.
int age = 28; char grade = 'A'; boolean late = true; byte b = 20; long num1 = 1234567; short no = 10; float k = (float)12.5; double pi = 3.14;
Java uses static typing, which checks types at compile time.
int i = 10; char ch = 'a'; char name = "Lil"; // compile error
The final keyword makes a variable constant.
final double PI = 3.14;
The double type holds decimal values.
double PI = 3.14; double price = 5.75;
Basic math operations include addition, subtraction, multiplication, division, and modulus.
int a = 20; int b = 10; int result; result = a + b; // 30 result = a - b; // 10 result = a * b; // 200 result = a / b; // 2 result = a % b; // 0
Comparison operators include >, <, >=, <=, ==, and !=.
int a = 5; int b = 3; boolean result = a > b; // true
Operators like +=, -=, *=, /=, and %= modify the variable in place.
int number = 5; number += 3; // 8 number -= 4; // 4 number *= 6; // 24 number /= 2; // 12 number %= 7; // 5
Use ++ to increment and -- to decrement.
int numApples = 5; numApples++; // 6 int numOranges = 5; numOranges--; // 4
Operators follow the order of precedence to determine the order of operations.
double numDouble = 12.99; System.out.println((int)numDouble); // 12 int numInt = 9; System.out.println((double)numInt); // 9.0
Casting converts a variable from one type to another.
int i = (int) 12.99; double d = (double) 10;
Java objects have state (fields) and behavior (methods).
public class Person { int age; String name; public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } public static void main(String[] args) { Person p = new Person(); p.set_value(); p.get_value(); } }
Instances of a class are created using the class's constructor.
public class Person { int age; String name; public Person(int age, String name) { this.age = age; this.name = name; } public static void main(String[] args) { Person Bob = new Person(31, "Bob"); Person Alice = new Person(27, "Alice"); } }
Dot notation accesses fields and methods of an object.
public class Person { int age; public static void main(String[] args) { Person p = new Person(); p.age = 20; System.out.println("Age is " + p.age); } }
Constructors initialize objects when they are created. They have the same name as the class and no return type.
public class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) { Person p = new Person("Alice", 30); System.out.println(p.name + " is " + p.age + " years old."); } }
Use the 'new' keyword to create a new instance of a class.
Person p = new Person(); p.name = "Bob"; p.age = 25;
Reference data types hold references to objects rather than actual values.
String str = "Hello, World!"; Person p = new Person(); p.name = "Alice";
Method overloading lets you define several methods with the same name, provided each one has a unique set of parameters.
public class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
Methods define the behavior of objects and can be called to perform actions.
public class MathOperations { public int add(int a, int b) { return a + b; } public static void main(String[] args) { MathOperations mo = new MathOperations(); int result = mo.add(5, 10); System.out.println(result); } }
Access modifiers determine the level of visibility as well as the accessibility for classes, methods, and variables.
public class MyClass { private int x; protected void display() { System.out.println(x); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.display(); } }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!