- Get link
- X
- Other Apps
1 : What is Java?
2 : What is the difference between JDK, JRE, and JVM?
3 : What are the access modifiers in Java? Explain each one.
4 : What are the different types of variables in Java?
5 : What is the difference between static and non-static methods in Java?
6 : What is an interface in Java?
7 : What is the difference between an abstract class and an interface in Java?
8 : What is a constructor in Java? Explain its types.
9 : What is the difference between an exception and an error in Java?
10 : What is the difference between ArrayList and LinkedList in Java?
11 : What is the difference between a HashMap and a Hashtable in Java?
12 : What is a thread in Java? Explain its life cycle.
13 : What is synchronization in Java?
14 : What is the difference between wait() and sleep() methods in Java?
15 : What is garbage collection in Java? How does it work?
# What is Java?
Java is a general-purpose, high-level programming language that is designed to be platform-independent, meaning it can run on different operating systems without needing to modify the code. It was developed by James Gosling at Sun Microsystems (later acquired by Oracle) and first released in 1995. Java is object-oriented, meaning it uses objects to represent data and the operations that can be performed on that data. It is widely used for developing mobile applications, web applications, desktop applications, and server-side applications. Java code is compiled into bytecode, which can be executed by a Java Virtual Machine (JVM).
# What is the difference between JDK, JRE, and JVM?
JDK, JRE, and JVM are all essential components of the Java platform. Here's a brief overview of each:
JVM (Java Virtual Machine): The JVM is an abstract machine that provides a runtime environment for executing Java bytecode. It is responsible for interpreting the compiled bytecode and translating it into native machine code that the operating system can understand. The JVM also manages memory allocation and garbage collection.
JRE (Java Runtime Environment): The JRE is a subset of the JDK that includes the JVM and other essential components needed to run Java applications but does not include development tools such as the Java compiler. The JRE is required to run any Java application or applet.
JDK (Java Development Kit): The JDK is a software development kit that includes the JRE and additional tools and libraries for developing Java applications. The JDK includes the Java compiler, debugger, and other development tools needed to create Java applications.
In summary, the JVM is the virtual machine that executes Java bytecode, the JRE is the runtime environment required to run Java applications, and the JDK is the development kit that includes everything needed to develop and build Java applications, including the JRE and development tools.
# What are the access modifiers in Java? Explain each one.
In Java, there are four access modifiers that can be used to control the access to classes, methods, and variables. These access modifiers are:
Public: Public is the most permissive access modifier and allows unrestricted access to a class, method, or variable from any other class, whether it's in the same package or a different one. Any class, method, or variable that is declared public can be accessed by any other class or code.
Private: Private is the most restrictive access modifier and allows access only within the same class. If a class member is declared private, it cannot be accessed from outside of that class, even from other classes in the same package.
Protected: Protected allows access to class members within the same class and within any subclasses, even if they are in different packages. However, protected members cannot be accessed by code outside of the class hierarchy.
Default: Default (also known as package-private) is the access modifier that is used when no other access modifier is specified. It allows access to class members within the same package but not from any other package.
It's important to note that access modifiers can be applied to classes, class members (such as variables, constructors, and methods), and interfaces. By controlling access to class members, access modifiers help to ensure the integrity and security of Java code.
# What are the different types of variables in Java?
In Java, there are three types of variables:
Local variables: Local variables are declared inside a method or a block and are only accessible within that method or block. They are not visible to other methods or blocks and are destroyed once the method or block is exited. Local variables must be initialized before they can be used.
Instance variables: Instance variables are declared inside a class, but outside of any method or block. They are also known as non-static variables because they belong to a specific instance of a class. Instance variables are visible to all methods, constructors, and blocks of the same class and can be accessed using an object of that class.
Static variables: Static variables are also declared inside a class, but with the static keyword. They are also known as class variables because they belong to the class rather than any specific instance of the class. Static variables are created when the class is loaded into memory and are destroyed when the program ends. They are visible to all methods, constructors, and blocks of the same class and can be accessed using the class name, rather than an object of the class.
It's important to note that variables in Java must have a specific data type, such as int, double, or String, and can be initialized with a value at the time of declaration.
# What is the difference between static and non-static methods in Java?
In Java, static and non-static methods are two different types of methods that serve different purposes:
- Static methods:
Static methods are methods that are associated with the class, not with any instance of the class. They can be called using the class name, without the need to create an object of the class. Static methods can access only static variables and cannot access non-static variables or instance methods.
For example, the following is an example of a static method that simply returns the sum of two numbers:
javapublic class Example {
public static int sum(int num1, int num2) {
return num1 + num2;
}
}
This method can be called using the class name, like this:
javaint result = Example.sum(2, 3);
- Non-static methods:
Non-static methods are methods that are associated with a particular instance of a class. They can only be called on an object of the class, not using the class name. Non-static methods can access both static and non-static variables and methods.
For example, the following is an example of a non-static method that calculates the area of a rectangle:
javapublic class Rectangle {
private int width;
private int height;
public int getArea() {
return width * height;
}
}
This method can only be called on an instance of the Rectangle class, like this:
javaRectangle rect = new Rectangle();
rect.width = 5;
rect.height = 10;
int area = rect.getArea();
In summary, the key difference between static and non-static methods in Java is that static methods are associated with the class and can be called using the class name, while non-static methods are associated with an instance of the class and can only be called on an object of the class.
- Get link
- X
- Other Apps

Comments
Post a Comment