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

Rest of Java 2

Uploaded by

manglamdubey2011
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)
15 views

Rest of Java 2

Uploaded by

manglamdubey2011
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/ 9

INDEX:

1.Explain the architecture of java.

2.Explain the concept of abstract and inner class in

java.

3.What are the different ways in java to read console

inputs?

4.Define synchronization in java.


1. Explain the architecture of java.
ANS- Java Architecture is a collection of components, i.e., JVM,
JRE, and JDK. It integrates the process of interpretation and compilation.
The following figure represents the Java Architecture in which each step is
elaborate graphically.

Components of Java Architecture


The Java architecture includes the three main components:
o Java Virtual Machine (JVM)
o Java Runtime Environment (JRE)
o Java Development Kit (JDK)
The JVM is an abstract computing machine that enables a computer to run a Java
program. It acts as an intermediary between the Java bytecode and the underlying
hardware.
The JVM is a fundamental part of Java's "Write Once, Run Anywhere" philosophy.
By converting Java bytecode into machine-specific instructions, it allows Java
applications to run on any device equipped with a compatible JVM, ensuring cross-
platform compatibility and efficient execution.
Java Runtime Environment
It provides an environment in which Java programs are executed. JRE takes our Java
code, integrates it with the required libraries, and then starts the JVM to execute it.
Java Development Kit
It is a software development environment used in the development of Java
applications and applets. Java Development Kit holds JRE, a compiler, an interpreter
or loader, and several development tools in it.
2. Explain the concept of abstract and inner class in java.
ANS-
Abstract Class
An abstract class in Java is a class that cannot be instantiated on its own and is meant
to be subclassed. It can contain both abstract methods (without a body) and concrete
methods (with a body).
abstract class Animal {
// Abstract method (does not have a body)
public abstract void makeSound();
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof");
}}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Output: Woof
}
}
Inner Class
An inner class in Java is a class that is defined within another class. It can access the
members (including private members) of its enclosing class.
class OuterClass {
private String message = "Hello, World!";
// Member Inner Class
class InnerClass {
public void printMessage() {
System.out.println(message); // Accessing outer class private member
}}}
public class Main {
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
inner.printMessage(); // Output: Hello, World!
}
}
3. What are the different ways in java to read console inputs?
ANS-
Using Scanner Class

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Reading a String
System.out.print("Enter a string: ");
String str = scanner.nextLine();
System.out.println("You entered: " + str);

scanner.close();
}
}
Using BufferedReader Class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {


public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));

try {
// Reading a String
System.out.print("Enter a string: ");
String str = reader.readLine();
System.out.println("You entered: " + str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Using Console Class
The Console class is suitable for reading passwords or other sensitive data as it can
mask input.
import java.io.Console;

public class Main {


public static void main(String[] args) {
Console console = System.console();

if (console != null) {
// Reading a String
String str = console.readLine("Enter a string: ");
System.out.println("You entered: " + str);
} else {
System.out.println("No console available");
}
}
}
4. Define synchronization in java.
ANS-
Synchronization in Java is a mechanism that controls access to shared resources by
multiple threads. It's essential in a multithreaded environment to prevent thread
interference and ensure data consistency.
public class Counter {
private int count = 0;

public synchronized void increment() {


count++;
}
}

You might also like