0% found this document useful (0 votes)
2 views

Java Notes

The document explains the roles of JVM, JRE, and JDK in Java programming, highlighting that JVM executes Java programs, JRE provides the necessary environment to run them, and JDK is used for development. It discusses Java's platform independence through bytecode, which allows Java applications to run on any system with a JVM. Additionally, it covers object-oriented programming principles, control statements, and provides examples of Java code for adding numbers and using a switch statement.

Uploaded by

Rabi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Notes

The document explains the roles of JVM, JRE, and JDK in Java programming, highlighting that JVM executes Java programs, JRE provides the necessary environment to run them, and JDK is used for development. It discusses Java's platform independence through bytecode, which allows Java applications to run on any system with a JVM. Additionally, it covers object-oriented programming principles, control statements, and provides examples of Java code for adding numbers and using a switch statement.

Uploaded by

Rabi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. What are JVM, JRE, and JDK? Explain their roles in Java programming.

• JVM (Java Virtual Machine):


It provides a runtime environment to execute Java programs. It converts bytecode into
machine-specific code and ensures platform independence.
Role: Responsible for interpreting or compiling the bytecode and managing runtime
operations like garbage collection.
• JRE (Java Runtime Environment):
It is a software package that provides the libraries and JVM required to run Java
applications.
Role: Contains JVM and class libraries but cannot compile Java programs.
• JDK (Java Development Kit):
It is a development environment containing tools like a compiler (javac), JRE, and
debugging tools.
Role: Used for developing, compiling, and running Java programs.

2. Explain the features of Java that make it platform-independent. Or Define


bytecode and its significance in Java.

• Platform Independence in Java:


Java programs are compiled into bytecode, which is a platform-neutral intermediate
code. Bytecode is interpreted by the JVM, which converts it into machine code for the
underlying platform.
This allows Java to run on any system with a JVM, making it platform-independent.
• Significance of Bytecode:
o Acts as a universal code that can be executed on any platform.
o Reduces dependency on the hardware or operating system.
o Improves program security as the code runs within the JVM.

3. Write a simple Java program to add two numbers using user input.
java
Copy code
import java.util.Scanner; // Importing Scanner class

public class AddTwoNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");


int num1 = scanner.nextInt();

System.out.print("Enter second number: ");


int num2 = scanner.nextInt();
int sum = num1 + num2; // Adding the numbers
System.out.println("The sum is: " + sum);
}
}

4. Discuss the concept of object-oriented programming and its principles with


examples.

Object-Oriented Programming (OOP): A programming paradigm based on the concept of


objects containing data (fields) and methods.

Principles of OOP:

1. Encapsulation: Wrapping data and methods into a single unit (class).

java
Copy code
class Person {
private String name; // Encapsulation
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

2. Inheritance: Allows one class to inherit properties of another.

java
Copy code
class Animal {
void eat() { System.out.println("This animal eats food."); }
}
class Dog extends Animal {
void bark() { System.out.println("This dog barks."); }
}

3. Polymorphism: Enables methods to behave differently based on the object.

java
Copy code
class Animal { void sound() { System.out.println("Animal makes a
sound."); } }
class Dog extends Animal { void sound() { System.out.println("Dog
barks."); } }

4. Abstraction: Hides unnecessary details and shows only essential features.

java
Copy code
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing a circle."); }
}

5. What are the different control statements in Java? Give examples of each.

Control statements in Java manage the flow of program execution.

1. Conditional Statements:
o if-else: Executes code based on a condition.

java
Copy code
if (x > 0) { System.out.println("Positive"); } else {
System.out.println("Negative"); }

2. Switch Statement:
o Executes one block of code among many options.

java
Copy code
switch (day) { case 1: System.out.println("Monday"); break; }

3. Looping Statements:
o for: Iterates a block of code a fixed number of times.

java
Copy code
for (int i = 0; i < 5; i++) { System.out.println(i); }

o while: Iterates based on a condition.

java
Copy code
while (x > 0) { x--; }

o do-while: Executes at least once before checking the condition.

java
Copy code
do { x--; } while (x > 0);

4. Jump Statements:
o break: Exits a loop or switch.
o continue: Skips the current iteration.
6. Explain the use of the “switch” statement in Java with a sample program.

Switch Statement: It evaluates an expression and executes the matching case block. If no
match is found, the default block is executed.

Example Program:

java
Copy code
public class SwitchExample {
public static void main(String[] args) {
int day = 3;

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
}
}

Output:
Wednesday

You might also like