Java is a high-level, object-oriented programming language developed by James Gosling in 1991. It can run on any operating system and follows the Write Once, Run Anywhere (WORA) principle. Java’s key features, known as Java Buzzwords, include:
- Platform-independent: runs on any OS with a JVM
- Object-oriented: based on classes and objects
This Java Cheat Sheet article has been written by experts in Java and is based on the experience of students who have recently undergone Java interviews.
1. Java Programming Terminologies
- JVM: executes the bytecode generated by the compiler.
- Bytecode: The Javac compiler of JDK compiles the Java source code into bytecode so that it can be executed by JVM.
- JDK: Itis a complete Java development kit that includes everything including compiler, Java Runtime Environment (JRE), java debuggers, java docs, etc.
- JRE: allows the Java program to run, however, we cannot compile it.
- Garbage Collector: To delete or recollect that memory JVM has a program called Garbage Collector.
- Finalize method: thisfunction is triggered by the garbage collector just before an object is deleted or destroyed.
2. Java Basics
Now, we will explore some of the fundamental concepts often utilized in the Java programming language.
Object
An object refers to an entity that possesses both behavior and state, such as a bike, chair, pen, marker, table, and car. These objects can be either tangible or intangible, including the financial system as an example of an intangible object.
There are three characteristics of an object:
- State: The data (value) of an object is represented by its state.
- Behaviour: The functionality of an object, such as deposit, withdrawal, and so on, is represented by the term behaviour.
- Identity: A unique ID is often used to represent an object’s identification. The value of the ID is hidden from the outside user. The JVM uses it internally to uniquely identify each object.
Class
A class is a collection of objects with similar attributes. It’s a blueprint or template from which objects are made. It’s a logical thing. It can’t be physical. In Java, a class definition can have the following elements:
- Modifiers: A class can be private or public, or it can also have a default access level
- class keyword: To construct a class, we use the class keyword.
- class name: The name of the class should usually start with a capital letter.
- Superclass (optional): If the class has any superclass, we use the extends keyword and we mention the name of the superclass after the class name.
- Interface (optional): If the class implements an interface, we use the implements keyword followed by the name of the interface after the class name.
Constructors
In Java, a constructor is a block of code similar to a method. Whenever a new class instance is created, the constructor is called. The memory allocation for the object only happens when the constructor is invoked.
There are two types of constructors in Java. They are as follows:
1. Default Constructor: A default constructor is a type of constructor that does not require any parameters. When we do not declare a constructor for a class, the compiler automatically generates a default constructor for the class with no arguments.
2. Parameterised Constructor: A parameterized constructor is a type of constructor that requires parameters. It is used to assign custom values to a class's fields during initialization.
Keyword
In Java, Reserved words are also known as keywords. These are particular terms that hold specific meanings. Java has 61 Reserved Keywords that are predefined and cannot be used as variable, object, or class names. Here's a list of the keywords used in Java:
To know more about java keyword -> Java keyword
3. Java Program to Print "Hello World"
class GFG {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Output
Hello World!