Java is a high level, class based, Object Oriented Programming language built by sun microsystems in 1995. It works on the principle of Write Once Run Anywhere (WORA). Features of java:
- Simple
- Object oriented
- Secured
- Platform independent
- Robust
- Portable
- Multithreaded
- Distributed
- Interpreted
- High Performance
- Dynamic
- Architecture neutral
JDK (Java Development Kit): it consists of jre, jvm, javac, documentation etc.
JRE (Java Runtime Environment): it consists of prescribed methods, classes, and packages that are needed to run the programs.
JVM (Java Virtual Machine): it interprets the bytecode.
The code is converted into bytecode by javac and then the bytecode is interpreted by the JVM to CPU. The Bytecode is not directly executed by the CPU.
Java handles memory automatically through a process known as automatic memory management, mainly using Garbage Collection (GC) to free up unused memory.
Java Memory Areas (Memory Model): when a java program runs, memory is divided into the following main areas:
Heap: Stores objects and instance variables. Managed by the garbage collector.
Stack: Stores method calls and local variables. Each method / thread has its own stack.
Method Area: Stores class-level metadata (class name, static variables, method info). Code is loaded into method area.
- Local Variables: Local variables are a variable that are declared inside the body of a method.
- Instance / non-static variables: variable declared inside the class but outside the body of the method, is called an instance variable.
- Class / Static variables: Static variables are initialized only once, at the start of the program execution. Class Demo { Static int a = 1; // static variable Int data = 99; // instance variable void method () { int b = 90; // local variable. } }
Primitive data types are predefined and available within the java language. which include integer, character, Boolean, and float.
Total 8 Types:
Byte 1 bit
Short 2 bit
Int 4 bit
Long 2 bit
Char 2 bit
Float 4 bit
Double 8 bit
Boolean 1 bit
Non-Primitive data types: which include classes, arrays, and interfaces. Type conversion and Type casting:
Case 1: Variable of smaller capacity is assigned to another variable of bigger capacity. \
Double d;
Int i = 10;
d = I;This process is automatic, and non-explicit is known as conversion.
Case 2: Variable of larger capacity is assigned to another variable of smaller capacity.
Double d = 10;
Int I;
I = (int) d; [(int) -> Type cast operator]You must specify the Type Cast Operator. This process is called Type casting.
There are a Total of 51 reserved keywords in java.
Control Flow: If Statements:
- Simple if statement
- if-else statement
- if-else-if ladder
- Nested if-statement
If(condition) {
// code
If(condition) {
// nested if
// code
}
} else if (condition) {
// code
} else {
// code
}Switch Statement:
Switch(val){
case value1: // code
break;
case value2: // code
break;
default: // code
}Methods in Java: A method is a block of code or collection of statements to perform a certain task or operation.
Access Specifier: visibility of the method.
-
Public: The method is accessible by all classes.
-
Private: the method is accessible only in the class in which it is defined.
-
Protected: the method is accessible within the same package or subclasses in a different package.
-
Default: when we don’t use any access specifier, java uses default access specifier by default. It is visible only from the same package.
Types of Methods: Predefined methods: methods that are already defined in the java class libraries are known as predefined methods. Also known as the built-in method.
Ex: length(), equals(), compareTo(), sqrt().
User-defined methods: The method written by user or programmer is known as a user-defined method.
Static method: A static method belongs to the class, not to any specific object. It can be called without creating an object of the class.
Static int add(int a, int b) {}Instance method: An instance method is a method that belongs to an object of a class. It can access instance variables and other instance methods of the class.
Abstract Method: the method does not have the method body in known as abstract method. In other words, without implementation is known as an abstract method. The class itself must be abstract if it has an abstract method.
Final Method: Cannot be overridden by subclasses. Useful to prevent modification.
Constructors: a constructor is a block of code like the method without a return type. It is called when the instance of the class is created. It has the same name as class.
-
Default Constructor: A Constructor with no parameters. Java will create one automatically if not created by user.
-
Parameterized Constructor: A constructor that takes arguments to initialize object values.
-
Constructor Overlading: Multiple constructors with different parameters in the same class.
Constructors can be overloaded with different parameters. Constructors cannot be overridden.
Copy constructor:
this() in constructor: Calls another constructor in same class.
super() in constructor: Calls parent class constructor (in inheritance).
What are Strings?
Strings are nothing but a sequence of characters enclosed within double quotes.
How are Strings represented in Java?
In some other languages the strings are represented as an array of characters. But in case of java, Strings are represented as objects of java.lang.String class.
How do you create Strings in java?
There are two ways to create String objects in java.
- Using String literals
String s1 = “ABC”; String s2 = “123”;
- Using new Operator
java String s3 = new String(“user”);
The String Pool (also called the intern pool) is a special memory area in the Heap where Java stores string literals to optimize memory usage and improve performance.
• Strings are used frequently in most Java applications.
• Strings are immutable (cannot be changed once created).
• Reusing the same string objects saves memory and boosts performance. Both a and b refer to the same object in the String Pool — no new object is created for b.
A new object is created in the Heap, not reused from the pool. Even though the value is the same, it’s a different object.
a == b: true (same reference)
a == c: false (different objects)Exception is an unwanted event that interrupts the normal flow of the program.
Types of exceptions:
-
Checked exceptions: that are checked at the compile-time.
-
Unchecked exceptions: not checked at the compile-time but they are checked at the runtime.
Try: The try keyword is used to specify a block where we should place an exception code.
Catch: The catch block contains code that is executed when the exception handler is invoked.
Finally: The finally block contains code that is executed whether the exception occurs or not.
Throw: throw keyword is used to declare an exception explicitly.
Throws: The throws keyword is used in a method declaration to declare one or more exceptions that the method might throw during execution.
TRY is mandatory, CATCH is also mandatory if NO FINALLY, finally is optional if both try and catch are present finally is mandatory if no catch in present.
Common Java Exception:
- ArithmeticException: if we divide any number with zero.
Int a = 50/0;- NullPointerException: if we have null value in any variable.
String s = null;
Sout(s.length()); //NullPointerException- NumberFormatException: if any variable or number is mismatched.
String s="abc";
Int i= Integer.parseInt(s); - ArrayIndexOutOfBoundsException:
Java interview questions:
- Can we use final keyword for class? Explain why and how?
Object oriented programming is a methodology or programming paradigm to design a program using classes and objects.
What is an object?
An object represents a real-world entity. Objects are the instances of class.
What Is class?
A class is a blueprint or prototype from which objects are created. Collection of objects.
Abstraction: it can be defined as hiding the internal implementation and showing only the required functionality. We use the keyword abstract. It can also be achieved by using abstract classes and interfaces in java. Only method signatures are exposed, implementation details are hidden.
Encapsulation: Encapsulation is the process of binding variables and methods into a single unit, typically a class. It restricts direct access to internal data using access modifiers (like private, protected, and public), ensuring data integrity and security. To access or modify the private data, getter and setter methods are used, which provide controlled and secure access to the class members.
Inheritance: In Java, inheritance is a mechanism that allows one class (child/subclass) to inherit fields and methods from another class (parent/superclass).
Java supports the following types of inheritance:
- Single Inheritance: A subclass inherits from a single superclass.
class Dog extends Animal {}- Multilevel Inheritance: A class inherits from another derived class, forming a chain.
class Animal {}
class Mammal extends Animal {}
class Dog extends Mammal {}- Hierarchical Inheritance: Multiple classes inherit from a single parent class.
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}- Multiple Inheritance (via Interfaces): Java does not support multiple inheritance with classes but allows it using interfaces.
interface Animal {}
interface Bird {}
class Bat implements Animal, Bird {}- Hybrid Inheritance (via Interfaces): A combination of multiple and multilevel inheritance using interfaces.
Class Parents {}
interface Mother {}
interface Father {}
class child extends Parents implements Mother, Father {}Polymorphism: Polymorphism in Java is the ability of an object to take on many forms. It allows method to behave differently based on the object that invokes it.
Java supports two types of polymorphism:
Compile Time (Static Binding / Method Overloading): defining multiple methods in the same class with the same name but different parameters. The decision of which method to call is made at compile-time.
Here, add() method is overloaded with different parameters.
Runtime Polymorphism (Dynamic Binding / Method Overriding):
when a subclass provides a new implementation of a method that is already defined in its superclass, using the same method name, return type, and parameters. The decision of which method to call is made at runtime, based on the object type.







