1/20/26, 11:48 PM java.
md
Difference between JDK, JRE, and JVM - all are platform (OS) dependent
JVM = provides a runtime environment
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it
doesn't physically exist. It is a specification that provides a runtime environment in which Java
bytecode can be executed.
It's a part of JRE and is platform-dependent but provides a platform-independent execution
environment.
Converts bytecode into machine code and executes it on the underlying hardware.
JRE = JVM + Class libraries
Java Runtime Environment (JRE) is a software package that provides Java Virtual Machine (JVM),
class libraries and other components to run applications in Java.
Used by end-users to execute Java applications without needing development tools.
JDK = JRE + Development tools
The Java Development Kit (JDK) is a software development environment that is used to develop Java
applications and applets. It physically exists. It contains JRE + development tools.
The JDK contains
a private Java Virtual Machine (JVM)
and a few other resources,
such as an interpreter/loader (java),
a compiler (javac),
an archiver (jar),
a documentation generator (Javadoc), etc., to complete the development of a Java
Application.
Used by developers to write, compile, and debug Java programs.
If the class is not public then the file name can be different from class name.
file - [Link]
[Link] 1/14
1/20/26, 11:48 PM [Link]
class Simple {
public static void main (String args[]) {
}
}
javac [Link] // to compile
java Simple // to execute
What happens at compile time?
At compile time, the Java file is compiled by Java compiler (It does not interact with OS) and
converts the Java code into bytecode.
class File
class Loader
It is the subsystem of JVM that is used to load class files.
bytecode verification
Checks the code fragments for illegal code that can violate access rights to objects.
interpreter
Read bytecode stream then execute the instructions.
runtime
hardware
Program to convert UTF-8 to Unicode
In the above code, a Class UnicodeDemo is created. At the start, a Unicode String str1 is
converted into a UTF-8 form using the getBytes() method. After that, the byte array is again
converted into Unicode and the value of newstr is displayed on the console.
public class UnicodeDemo
{
public static void main(String ar[]) throws Exception
{
[Link] 2/14
1/20/26, 11:48 PM [Link]
String str1 = "Unicode Sytem\u00A9";
byte[] charset = [Link]("UTF-8");
String newstr = new String(charset, "UTF-8");
[Link](newstr);
}
}
16-bits encoding was able to represent only 65,536 characters that were not sufficient for all the
characters available around the world.
So, the Unicode system was extended up to 1,112,064 characters. The characters that are larger
than 16-bits are called Supplementary characters and are defined by Java using a pair of char
values.
java features
[Link]
There is a deficiency of pointers that prevent security issues.
Java is faster than traditional interpreted programming languages because Java bytecode is "close"
to native code.
it is still slightly slower than compiled languages, such as C++
?? Java is designed for distribution, allowing users to develop distributed applications
effectively. RMI and EJB facilitate the creation of these distributed applications. This feature
enables access to files by invoking methods from any machine on the Internet.
while there's an initial compilation step (source to bytecode), the real performance magic happens
at runtime, where the JIT compiler turns frequently used bytecode into blazing-fast native machine
code.
?? what is multiple inheritance. why doesn't java support it? how does c++ support it.
what happens if a class implements two interfaces with same method signature. c
[Link] 3/14
1/20/26, 11:48 PM [Link]
class has to implement it, simple
?? what is operator overloading
read again jvm architecture
[Link]
The left shift operator << is a bitwise operator. When you left-shift a number x by y positions (x
<< y), it's equivalent to multiplying x by 2^y
revisit signed and unsigned numbers and 2's complement
Java static keyword is used to indicate that a variable or method is a class method. The static
keyword in Java is mainly used for memory management.
Java instanceof keyword is used to test whether the object is an instance of the specified class
or implements an interface.
Java enum keyword is used to define a fixed set of constants. Enum constructors are always private
or default.
volatile keyword
The volatile keyword cannot be used with classes or methods. However, it is used with variables.
Essentially, it guarantees visibility and atomicity of reads and writes to the variable. It
prevents the compiler from the reordering of code.
When you use the volatile keyword with that toy, it's like putting a big, bright "LOOK AT ME!"
sticker on it.
[Link] 4/14
1/20/26, 11:48 PM [Link]
"Everyone always sees the newest version, only one person can touch or alter it at a time, and the
computer can't try to guess what it is; it must look directly at it every time."
The switch statement works with byte, short, int, long, enum types, String and some wrapper types
like Byte, Short, Int, and Long. Since Java 7, we can use strings in the switch statement.
In Java, the switch statement does not allow variables as case labels. Each case label must be a
constant expression, meaning its value must be known at compile time. This includes literal values
or final variables initialized with a constant value.
The Java switch statement is fall-through. It means it executes all statements after the first
match if a break statement is not present.
It's worth noting that starting from Java 12, switch statements have been enhanced to support
switch expressions, which allow the switch statement to be used as an expression that returns a
value. This feature provides more flexibility and can lead to more concise and readable code in
certain situations.
A labelled for loop in Java is a for loop that has been assigned a label. Labels in Java provide a
way to identify a block of code, making it possible to break out of or continue an outer loop from
within a nested loop. This feature is particularly useful when working with nested loops and we
need to control the flow of the outer loop from within an inner loop.
break block_label
Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the
do-while check the condition at the end of loop body. The Java do-while loop is executed at least
once because condition is checked after loop body.
We can use continue statement with a label. This feature is introduced since JDK 1.5. So, we can
continue any loop in Java now whether it is outer loop or inner.
continue block_label
[Link] 5/14
1/20/26, 11:48 PM [Link]
to generate java doc html use the command `javadoc [Link]`
q. Inheritance is used to achieve runtime polymorphism. What is runtime polymorphism and how is it
achieved by inheritance ???
Inheritance - When one object acquires all the properties and behaviors of a parent object
Polymorphism - If one task is performed in different ways. speak() - woof, quack, meow
In Java, we use method overloading and method overriding to achieve polymorphism.
Abstraction - Hiding internal implementation and showing relevant functionality to the user
In Java, we use abstract class and interface to achieve abstraction.
Encapsulation - Binding (or wrapping) code and data together into a single unit
Coupling refers to the knowledge or information or dependency of another class. It arises when
classes are aware of each other. If a class has the details information of another class, there is
strong coupling. In Java, we use private, protected, and public modifiers to display the
visibility level of a class, method, and field. We can use interfaces for the weaker coupling
because there is no concrete implementation.
Encapsulation involves hiding the implementation details of a class and providing a public
interface.
The protected access modifier restricts access to the members of a class within the same package
and its subclasses.
The abstract keyword is used to declare a class that cannot be instantiated and to define methods
that must be implemented by subclasses.
q. when would you use abstract class vs interface?
q. [Link] - aggregation and composition ?
[Link] 6/14
1/20/26, 11:48 PM [Link]
Within a class, Java allows two different kinds of blocks: instance blocks, commonly referred to
as initialization blocks and static blocks. Static blocks, which are usually used for static
initialization, are only executed once when the class is loaded into memory. Instance blocks can
be used to initialize instance variables and are executed each time a class object is generated.
Nested Class and Interface ?????
Java permits the nesting of classes and interfaces inside other classes and interfaces. The
members (fields, methods) of the enclosing class are accessible to nested classes, which can be
static or non-static. Nested interfaces can be used to logically group related constants and
methods together because they are implicitly static.
The new keyword is used to allocate memory at runtime. All objects get memory in the Heap memory
area.
Diff ways to create objects
1. By new Keyword
2. By newInstance() Method
This method is part of the [Link] class and is used to create a new instance of a class
dynamically at runtime. It invokes the no-argument constructor of the class. For example,
ClassName obj = (ClassName) [Link]("ClassName").newInstance();.
3. By clone() Method
The clone() method creates a copy of an existing object by performing a shallow copy. It returns a
new object that is a duplicate of the original object. For example,
ClassName obj2 = (ClassName) [Link]();.
4. By Deserialization
[Link] 7/14
1/20/26, 11:48 PM [Link]
Objects can be created by deserializing them from a stream of bytes. We can achieve it by using
the ObjectInputStream class in Java. The serialized object is read from a file or network, and
then the readObject() method is called to recreate the object.
5. By factory Method
Factory methods are static methods within a class that return instances of the class. They provide
a way to create objects without directly invoking a constructor and can be used to encapsulate
object creation logic. For example,
ClassName obj = [Link]().
Anonymous simply means nameless. An object that has no reference is known as an anonymous object.
It can be used at the time of object creation only.
new Main().fact(5);
create multiple objects of one type only
Rectangle r1=new Rectangle(), r2=new Rectangle();//creating two objects
3. How does the 'new' keyword function in Java when creating an object?
It allocates memory for a new object and initializes it.
q. when do we define static methods?
5. What access level must an overridden method have compared to the method it overrides?
An overridden method must have the same or a less restrictive access level compared to the method
it overrides to ensure that the subclass method is accessible where the superclass method is
accessible.
A Java constructor cannot be abstract, static, final, and synchronized.
Note: We can use access modifiers while declaring a constructor. It controls the object creation.
A constructor may be private (used in Singleton), protected, public or default.
[Link] 8/14
1/20/26, 11:48 PM [Link]
[Link]
Constructor chaining in Java is a practice where one constructor calls another constructor of the
same class or a superclass during object creation. It is typically done using either this() to
call another constructor in the same class or super() (which must be the first statement in the
constructor) to call a constructor in the superclass.
Constructors can perform other tasks beyond initialization, such as opening database connections,
starting threads, or calling methods.
read about [Link]
Java provides a Constructor class that can be used to get the internal information of a
constructor in the class. It belongs to [Link] package.
q. how do you create deep copy of an object in java? what is deep and shallow copy?
If no constructor call is explicitly written, the compiler implicitly inserts a call to super() in
every constructor. It ensures that the superclass constructor is called, initializing the
superclass part of the object. All classes inherit from the Object Class.
q. why is the java main method static?
q. why can't static methods use non static variables?
Java Static Block
It is used to initialize the static data member.
It is executed before the main() method at the time of class loading.
q. static nested classes and their practical examples
q. advantages of static keyword ??
q. What is a key restriction for static methods in Java? verify !!??
[Link] 9/14
1/20/26, 11:48 PM [Link]
They can be overridden in subclasses
They can be declared in abstract classes
chars of `this` keyword
Returns the Current Object: A method can return this, allowing method chaining, which is common in
fluent APIs. ????
during constructor chaining Compile Time Error: Call to this must be the first statement in the
constructor
Abstract classes and interfaces provide blueprints for subclasses. Use abstract classes when you
want to provide a default implementation or share code among related subclasses. Use interfaces to
define contracts that classes can implement, enabling multiple inheritance through interfaces.
Abstract classes in Java can have instance variables, constructors, and both abstract and concrete
methods. Interfaces, on the other hand, can only declare constants and abstract methods. They
cannot have instance variables, constructors, or concrete methods.
q. Static methods cannot be overridden but can hide other static methods from the superclass.????
check appendix.p1
In Java, Method Overloading is not possible by changing the return type of the method only.
it is possible to overload the main() method in Java, but it won't be considered as the entry
point for the Java Virtual Machine (JVM) to start the execution of the program.
Changing the access modifier of the method is enough for method overloading ???
Can we override the static method?
No, a static method cannot be overridden in Java. When a subclass defines a static method with the
same signature as a static method in its superclass, it is simply hiding the superclass method,
[Link] 10/14
1/20/26, 11:48 PM [Link]
not overriding it. This means that the method invoked is determined at compile time based on the
reference type, not at runtime based on the object's type. Therefore, static methods do not
exhibit polymorphic behavior like instance methods do.
while instance methods are kept in the object's heap area and are specific to each instance of the
class, static methods are kept in the method region of the JVM's memory, which is shared by all
instances of the class.
Note: super() is added in each class constructor automatically by the compiler if there is no
super() or this().
[Link]
Instance intializer block is invoked at the time of object creation. The Java compiler copies the
instance initializer block in the constructor after the first statement super(). So firstly,
constructor is invoked.
Rule: Runtime polymorphism can't be achieved by data members.
It is due to the fact that member variables in Java are not polymorphism-prone; rather, compile-
time member variable access is determined by the reference type. Because it's the reference to the
speedlimit member variable, the Bike class is where it comes from even though obj refers to a
Honda3 object.
```java
class Bike{
int speedlimit=90;
}
class Honda extends Bike{
int speedlimit=150;
}
public class Main{
public static void main(String args[]){
[Link] 11/14
1/20/26, 11:48 PM [Link]
Bike obj=new Honda();
[Link]([Link]);//90
}
}
```
Runtime polymorphism is resolved during the runtime of the program, not at compile-time, which
allows dynamic method dispatch.
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at
runtime rather than compile-time. Binding of a method call to the method body at runtime.
When type of the object is determined at compiled time(by the compiler), it is known as static
binding.
If there is any private, final or static method in a class, there is static binding.
When type of the object is determined at run-time, it is known as dynamic binding.
instanceof - gives false if compared with a null reference
```java
class Animal{}
class Dog1 extends Animal{//Dog inherits Animal
public static void main(String args[]){
Dog1 d=new Dog1();
[Link](d instanceof Animal);//true
}
}
```
q. read again - downcasting with instanceof - [Link]
instanceof-operator
[Link] 12/14
1/20/26, 11:48 PM [Link]
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the method.
# appendix
## p1
```java
class A {
public static void display() {
[Link]("Static method in A");
}
public void show() {
[Link]("Instance method in A");
}
}
class B extends A {
public static void display() {
[Link]("Static method in B");
}
@Override
public void show() {
[Link]("Instance method in B");
}
}
public class Test {
public static void main(String[] args) {
A a = new B();
[Link] 13/14
1/20/26, 11:48 PM [Link]
[Link]();
[Link]();
}
}
```
output
Static method in A, Instance method in B
Explanation: Static methods are not overridden but hidden, so [Link]() calls A's static method.
The show method is overridden, so [Link]() calls B's instance method.
# hyperskill tips
In Java, a static method is a method that is associated with a class rather than an instance of a
class.
It can be accessed directly using the class name and does not require the creation of a class
instance. A static method can only access static fields and other static methods within the same
class, and cannot refer to the this keyword because there is no instance context. Static methods
are often used as utility methods that provide common functionality for the entire project. They
can be called from instance methods, but not the other way around.
[Link] 14/14