The `length()` method in Java returns the number of characters in a string.
String str = "Hello"; System.out.println(str.length()); // Output: 5
The `indexOf()` method returns the position of the first occurrence of a character or substring. If the character or substring is not found, it returns -1.
String str = "Hello World!"; System.out.println(str.indexOf("l")); // Output: 2 System.out.println(str.indexOf("Wor")); // Output: 6 System.out.println(str.indexOf("z")); // Output: -1
The `concat()` method combines two strings into one.
String s1 = "Hello"; String s2 = " World!"; String s3 = s1.concat(s2); // s3 contains "Hello World!" System.out.println(s3); // Output: "Hello World!"
The `equals()` method checks if two strings are exactly the same.
String s1 = "Hello"; String s2 = "World"; System.out.println(s1.equals("Hello")); // Output: true System.out.println(s2.equals("Hello")); // Output: false System.out.println(s1.equalsIgnoreCase("hello")); // Output: true
The `charAt()` method returns the character at a specified position in a string.
String str = "This is a string"; System.out.println(str.charAt(0)); // Output: 'T' // Accessing last character safely System.out.println(str.charAt(str.length() - 1)); // Output: 'g'
The `toUpperCase()` method converts all characters in a string to uppercase, while `toLowerCase()` converts them to lowercase.
String str = "Hello World!"; String uppercase = str.toUpperCase(); // uppercase = "HELLO WORLD!" String lowercase = str.toLowerCase(); // lowercase = "hello world!"
The `static` keyword is used to declare variables and methods that belong to the class rather than to any specific instance.
public class ATM { // Static variables public static int totalMoney = 0; public static int numATMs = 0; // Static method public static void averageMoney() { System.out.println(totalMoney / numATMs); } }
Static methods and variables belong to the class as a whole, not to instances of the class. They are accessed using the class name.
public class ATM { // Static variables public static int totalMoney = 0; public static int numATMs = 0; // Static method public static void averageMoney() { System.out.println(totalMoney / numATMs); } public static void main(String[] args) { // Accessing static variable System.out.println("Total number of ATMs: " + ATM.numATMs); // Calling static method ATM.averageMoney(); } }
Static methods cannot access instance variables because they belong to the class, not to any specific object.
class ATM { // Static variables public static int totalMoney = 0; public static int numATMs = 0; public int money = 1; // Static method public static void averageMoney() { // Cannot use this.money because static methods can't access instance variables } }
Both static and non-static methods can access static variables, but static methods cannot access instance variables.
class ATM { // Static variables public static int totalMoney = 0; public static int numATMs = 0; public int money = 1; // Static method modifying a static variable public static void staticMethod() { totalMoney += 1; } // Non-static method modifying a static variable public void nonStaticMethod() { totalMoney += 1; } }
Static methods do not have access to instance variables or non-static methods because they don't have a `this` reference.
public class DemoClass { public int demoVariable = 5; public void demoNonStaticMethod() { // Non-static method } public static void demoStaticMethod() { // Can't use "this.demoVariable" or "this.demoNonStaticMethod()" } }
In Java, `public` and `private` keywords define the visibility of classes, methods, and variables. `public` means accessible from anywhere, while `private` means only accessible within the class where it's declared.
public class CheckingAccount { // Private instance variables private String name; private int balance; private String id; }
Encapsulation is the practice of hiding the internal details of an object and only exposing a controlled interface. This is done using `private` variables and `public` methods.
public class CheckingAccount { private int balance; // Accessor method public int getBalance() { return this.balance; } }
The `private` keyword restricts access to the members of a class, ensuring that they can only be accessed within the class itself.
public class CheckingAccount { private int balance; // Mutator method public void setBalance(int newBalance) { this.balance = newBalance; } }
Accessor methods, or getters, return the value of private instance variables without modifying them.
public class ExampleClass { private int exampleVariable; // Accessor method public int getExampleVariable() { return this.exampleVariable; } }
Mutator methods, or setters, allow you to change the value of private instance variables.
public class Dog { private String name; // Mutator method public void setName(String name) { this.name = name; } }
Local variables are defined inside methods or blocks and can only be used within that scope.
public class ExampleClass { public void exampleMethod() { String message = "Hello"; // Local variable System.out.println(message); } }
The `this` keyword is used to refer to the current instance of a class and can be used to differentiate between instance variables and method parameters with the same name.
The `this` keyword can also be used to call other methods in the same class. It helps to make code more readable and maintainable.
Inheritance allows one class (child class) to inherit fields and methods from another class (parent class). For example, a `Dog` class can inherit properties from an `Animal` class.
class Animal { // Animal class members } class Dog extends Animal { // Dog inherits properties from Animal }
The `main()` method is the entry point of any Java application. It must be defined as `public static void main(String[] args)`.
public class Example { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Method overloading allows a class to have more than one method with the same name but different parameters.
class ExampleClass { public void display(int num) { System.out.println("Number: " + num); } public void display(String str) { System.out.println("String: " + str); } }
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
class Animal { public void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark"); } }
Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. It can be achieved through method overriding and method overloading.
class Animal { public void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.makeSound(); // Output: Bark } }
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!