A for loop lets you run a block of code a certain number of times. It has three parts: starting point, condition to keep running, and a way to update the count.
for (int i = 0; i < 5; i++) { System.out.println(i); } // Output: 0 1 2 3 4
A while loop keeps running a block of code as long as a condition is true. It checks the condition before running the code.
int i = 0; while (i < 5) { System.out.println(i); i++; } // Output: 0 1 2 3 4
A do-while loop is like a while loop, but it runs the code block at least once before checking the condition.
int i = 0; // Runs the block first, then checks if i is less than 5 do { System.out.println(i); i++; } while (i < 5); // Output: 0 1 2 3 4
The charAt method allows you to get a specific character from a string by its position. The position starts at 0.
String text = "Hello"; System.out.println(text.charAt(0)); // Output: H
The concat method joins two strings into one.
String first = "Hello"; String second = "World"; System.out.println(first.concat(second)); // Output: HelloWorld
The equals method checks if two strings are exactly the same.
String first = "Hello"; String second = "Hello"; System.out.println(first.equals(second)); // Output: true
The length method tells you how many characters are in a string.
String text = "Hello"; System.out.println(text.length()); // Output: 5
The toLowerCase method changes all characters in a string to lowercase.
String text = "Hello World"; System.out.println(text.toLowerCase()); // Output: hello world
The toUpperCase method changes all characters in a string to uppercase.
String text = "Hello World"; System.out.println(text.toUpperCase()); // Output: HELLO WORLD
Access levels control who can see or use classes, methods, and variables. There are four levels: public, protected, default (package-private), and private.
public class Example { // public variable public int publicVar; // private variable private int privateVar; // protected variable protected int protectedVar; // default variable int defaultVar; }
Encapsulation means keeping data (variables) and the methods that work with it in one place. In Java, this is done with access levels and getter/setter methods.
public class Person { // private variables private String name; private int age; // getter for name public String getName() { return name; } // setter for name public void setName(String name) { this.name = name; } // getter for age public int getAge() { return age; } // setter for age public void setAge(int age) { this.age = age; } }
Static methods belong to the class itself rather than to any specific object. You can call them without creating an instance of the class. They can access static variables and other static methods.
public class MathUtils { // static method public static int add(int a, int b) { return a + b; } public static void main(String[] args) { // call static method System.out.println(MathUtils.add(5, 10)); // Output: 15 } }
Inheritance allows a new class to use features (methods and variables) from an existing class. The new class is called a subclass, and the existing class is called a superclass.
class Animal { void eat() { System.out.println("This animal eats food."); } } class Dog extends Animal { void bark() { System.out.println("The dog barks."); } } public class TestInheritance { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited method dog.bark(); } }
Polymorphism lets you use the same method name to perform different tasks, depending on the object. This can be done through method overriding or overloading.
class Animal { void sound() { System.out.println("Animal makes a sound."); } } class Dog extends Animal { void sound() { System.out.println("Dog barks."); } } public class TestPolymorphism { public static void main(String[] args) { Animal myDog = new Dog(); myDog.sound(); // Calls the overridden method in Dog class } }
A two-dimensional array is an array that contains other arrays. It's like a table or grid with rows and columns.
int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; System.out.println(matrix[0][0]); // Output: 1 System.out.println(matrix[1][2]); // Output: 6
You can set up a two-dimensional array by using nested braces. Each inner set of braces represents a row.
int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
To get an element from a two-dimensional array, use two indices: one for the row and one for the column.
int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; System.out.println(grid[1][1]); // Output: 5
You can loop through a two-dimensional array using nested loops: the outer loop for rows and the inner loop for columns.
int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { System.out.print(grid[i][j] + " "); } System.out.println(); }
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!