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:-
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.
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:-
Keyword | Use Case |
---|---|
abstract | Used to declare an abstract class or abstract method |
assert | Used to check assertions during debugging |
boolean | Represents a boolean value (true or false) |
break | Exits from a loop or a switch statement |
byte | Represents a signed 8-bit integer |
case | Used in a switch statement to define a case |
catch | Catches exceptions thrown in a try block |
char | Represents a 16-bit Unicode character |
class | Declares a class |
const* | Not used in Java, reserved for future use |
continue | Skips the rest of the loop and starts the next iteration |
default | Used in a switch statement as a default case |
do | Starts a do-while loop |
double | Represents a 64-bit double-precision floating-point number |
else | Used in an if-else statement |
enum | Declares an enumeration type |
exports | Used in module declarations to specify exported packages |
extends | Indicates a class is derived from another class |
final | Declares a variable, method, or class as final (unchangeable) |
finally | Defines a block of code to be executed after try-catch |
float | Represents a 32-bit single-precision floating-point number |
for | Starts a for loop |
goto* | Not used in Java, reserved for future use |
if | Used in an if statement |
implements | Indicates a class is implementing an interface |
import | Imports classes, packages, or individual members |
instanceof | Tests if an object is an instance of a specific class |
int | Represents a 32-bit integer |
interface | Declares an interface |
long | Represents a 64-bit integer |
module* | Defines a module, reserved for future use |
native | Indicates a method is implemented in platform-specific code |
new | Creates a new object |
open | Used in module declarations to specify open packages |
opens | Used in module declarations to specify opened packages |
private | Defines a private access modifier |
protected | Defines a protected access modifier |
provides | Used in module declarations to specify service providers |
public | Defines a public access modifier |
requires | Used in module declarations to specify required modules |
return | Exits a method and returns a value |
short | Represents a 16-bit integer |
static | Declares a static variable or method |
strictfp | Ensures consistent floating-point calculations |
super | Refers to the parent class |
switch | Selects one of many code blocks to be executed |
synchronized | Defines a synchronized block or method |
this | Refers to the current instance of the class |
throw | Throws an exception |
throws | Declares exceptions that a method may throw |
to | Used in switch expressions to specify case values |
transient | Indicates a member variable should not be serialized |
while | Starts a while loop |
transitive | Used in module declarations to specify transitive dependencies |
try | Defines a block of code to be tested for exceptions |
uses | Used in module declarations to specify service uses |
void | Defines a method that does not return a value |
volatile | Indicates a variable may be modified by multiple threads |
with | Used in switch expressions to specify pattern matching |
_ | Reserved for future use |
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming language All those words are collectively called Java Buzzwords. It is commonly used for programming web-based, window, enterprise, and mobile applications. This Java Cheat Sheet article has been written by experts in Java and based on the experience of students who have recently undergone Java interviews.
This Core Java Cheat Sheet has been designed by Java experts, based on the experience of students who have recently undergone Java interviews. Whether you are a beginner or an experienced Java developer, this Java Cheat Sheet is a valuable resource for quickly accessing essential syntax, concepts, and best practices related to Java Programming.