Java Cheat Sheet for WT
Java Cheat Sheet for WT
Example:
public class Example {
public final int NUMBER = 10; // final variable
public final void display() { // final method
System.out.println("This is a final method.");
}
}
public class SubExample extends Example {
public void display() { // compile-time error
System.out.println("This method cannot be overridden.");
}
}
Example:
public class Example {
public static int NUMBER = 10; // static variable
public static void display() { // static method
System.out.println("This is a static method.");
}
}
public class SubExample extends Example {
public void show() {
System.out.println(NUMBER); // accessing static variable
display(); // accessing static method
}
}
J3. ava Super Keyword:
The super keyword in Java is used to access the parent class constructor,
parent class methods, and parent class variables. It is mostly used in
inheritance where a child class is derived from a parent class.
Example:
public class ParentClass {
public void display() {
System.out.println("This is a parent class method.");
}
}
public class ChildClass extends ParentClass {
public void display() {
super.display(); // accessing parent class method
System.out.println("This is a child class method.");
}
}
4. Constructors in java:-
In Java, a constructor is a special method that is used to initialize objects
of a class. It is called when an object of a class is created, and its main
purpose is to set the initial values of the object's instance variables.
Constructors have the same name as the class in which they are defined,
and they don't have a return type.
Constructors are important in Java because they ensure that objects are
properly initialized before they are used. They also make it easy to
create new objects with different initial values, and to create copies of
existing objects.
class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof");
}
}
In the main() method, we create two Animal objects, one of type Dog
and the other of type Cat. When we call the makeSound() method on
each object, Java will use the appropriate implementation of the method
based on the actual type of the object at runtime. This is known as
dynamic method dispatch.
5. Arrays in Java and C share some similarities but also have some
differences.
Java:
int[] arr = new int[5]; // create an integer array of size 5
arr[0] = 1; // set the first element to 1
arr[1] = 2; // set the second element to 2
System.out.println(arr.length); // prints 5
C:
int arr[5]; // create an integer array of size 5
arr[0] = 1; // set the first element to 1
arr[1] = 2; // set the second element to 2
int len = sizeof(arr)/sizeof(arr[0]); // calculate the length of the array
printf("%d", len); // prints 5
In Java, we declare an array using the new keyword and specify its size.
We can then access the elements of the array using square brackets [].
We can also use the built-in method length() to get the length of the
array.
One key difference is that in Java, arrays are objects, while in C, arrays
are a type of data structure. This means that arrays in Java can have
methods and be manipulated using object-oriented principles, while in C,
arrays are typically manipulated using pointers.
One key difference between these two examples is the placement of the
square brackets. In Java, the brackets come after the data type, while in
C, they come after the variable name.
Another difference between Java and C arrays is that in Java, arrays have
a fixed size that is specified when the array is created. In C, arrays can
have a fixed size, but they can also be dynamically allocated at run-time
using functions like malloc() and calloc().
Overall, while there are some similarities between arrays in Java and C,
there are also some significant differences that reflect the different
programming paradigms of the two languages.
6. Characteristics of Java:-
Secure: Java is designed with security in mind and includes features like
sandboxing, which allows applications to run in a restricted environment
and prevents them from accessing system resources.
class Animal {
public void makeSound() {
System.out.println("Animal is making a sound");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Cat is meowing");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Dog is barking");
}
}
public class Test {
public static void main(String[] args) {
Animal animal1 = new Cat();
animal1.makeSound(); // Output: Cat is meowing
In this example, the Animal class has a method called makeSound(). The
Cat and Dog classes override this method to provide their own
implementation. In the main() method, we create an object of the Cat
class and assign it to an Animal reference variable. Similarly, we create
an object of the Dog class and assign it to another Animal reference
variable. When we call the makeSound() method on these reference
variables, the overridden methods of the Cat and Dog classes are called,
respectively. This is an example of dynamic method dispatch in Java.
8. Abstract Class:-
In Java, an abstract class is a class that is declared abstract and cannot
be instantiated. It can only be used as a base class for other classes that
extend it. Abstract classes provide a way to define a common interface
for a set of subclasses. It is like a blueprint for the subclasses, providing a
skeleton or template for them to follow.
For example, let's say we want to create a class called Car which
represents a car in a program. The Car class would have properties such
as make, model, year, color, and methods such as start(), stop(),
accelerate(), brake(), etc.
Once we define the Car class, we can create objects (or instances) of the
Car class. Each object will have its own set of properties and can perform
its own set of behaviors. For example, we can create an object of the Car
class called myCar, with properties such as make = "Toyota", model =
"Camry", year = 2022, color = "Red". We can then call the methods of
the myCar object, such as myCar.start(), myCar.accelerate(), etc.
For example, if we run the program with the following command line
arguments:
Class Loader: Class Loader is responsible for loading classes into the JVM.
It loads the bytecode of the class and creates an internal representation
of the class in the JVM.
c. Method Area: Method Area is used for storing class-level data, such as
the bytecode of the methods, constant pool, and static fields.
Class Loader loads the bytecode of the HelloWorld class into the JVM.
Memory Area allocates memory for the HelloWorld object in the heap
memory and memory for the main() method in the stack memory.
Execution Engine executes the bytecode of the main() method using the
interpreter.
Interpreter executes the System.out.println("Hello, World!") statement,
which prints "Hello, World!" to the console.
Native Method Interface is used to call the native methods of the
operating system for printing to the console.
This is a basic example of how the JVM architecture works.
Nested inner class: A nested inner class is a class defined inside another
class. It can access all members of the outer class, including private
members. Here is an example:
public class OuterClass {
private int x = 10;
public class InnerClass {
public void printX() {
System.out.println("x = " + x);
}
}
}
// Create an instance of the outer class
OuterClass outer = new OuterClass();
// Create an instance of the inner class
OuterClass.InnerClass inner = outer.new InnerClass();
// Access a member of the inner class
inner.printX();
Static inner class: A static inner class is a nested class that is marked as
static. It does not have access to the instance variables of the outer class.
Here is an example:
public class OuterClass {
private static int x = 10;
public static class InnerClass {
public void printX() {
System.out.println("x = " + x);
}
}
}
// Create an instance of the inner class
OuterClass.InnerClass inner = new OuterClass.InnerClass();
// Access a member of the inner class
inner.printX();
Local inner class: A local inner class is a class defined inside a method or
block. It has access to the variables of the enclosing method or block,
but those variables must be final or effectively final. Here is an example:
public class OuterClass {
public void printMessage(String message) {
// Define a local inner class
class InnerClass {
public void print() {
System.out.println(message);
}
}
// Create an instance of the inner class
InnerClass inner = new InnerClass();
// Call a method of the inner class
inner.print();
}
}
// Create an instance of the outer class
OuterClass outer = new OuterClass();
// Call a method of the outer class
outer.printMessage("Hello, world!");