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

JAVA

The document outlines the features of Java, including simplicity, object-orientation, platform independence, security, robustness, multithreading, portability, high performance, distribution capabilities, and dynamic adaptability. It also covers Java programming concepts such as arrays, jagged arrays, the Java Virtual Machine (JVM), method overloading, inheritance, the super and final keywords, and wrapper classes. Additionally, it provides example code for various Java functionalities, including finding the largest of three numbers and manipulating employee details.

Uploaded by

Janki Sharan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA

The document outlines the features of Java, including simplicity, object-orientation, platform independence, security, robustness, multithreading, portability, high performance, distribution capabilities, and dynamic adaptability. It also covers Java programming concepts such as arrays, jagged arrays, the Java Virtual Machine (JVM), method overloading, inheritance, the super and final keywords, and wrapper classes. Additionally, it provides example code for various Java functionalities, including finding the largest of three numbers and manipulating employee details.

Uploaded by

Janki Sharan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1. A. List out the features of JAVA and explain.

Features of Java

1. Simple:
Java is easy to learn and write. Its syntax is similar to C/C++, but it removes
complex features like pointers.

2. Object-Oriented:
Everything in Java revolves around objects and classes, making code reusable
and modular.

3. Platform-Independent:
Java programs are compiled into bytecode, which can run on any platform with a
Java Virtual Machine (JVM). "Write Once, Run Anywhere."

4. Secure:
Java has built-in security features like bytecode verification, sandboxing, and no
direct memory access (no pointers), reducing risks.

5. Robust:
It handles errors through strong exception handling and has automatic memory
management using Garbage Collection.

6. Multithreaded:
Java supports multithreading, allowing programs to perform multiple tasks
simultaneously for better performance.

7. Portable:
Java bytecode is platform-independent, and its standard libraries work the same
on all systems.

8. High Performance:
Though not as fast as C/C++, Java uses Just-In-Time (JIT) compilation for
improved speed.

9. Distributed:
Java supports networking features, making it easy to develop distributed
applications using APIs like RMI and EJB.

10. Dynamic:
Java adapts to evolving needs, dynamically linking new libraries, methods, and
objects during runtime.
1. B. (i).Write a program to find largest of three numbers using command line
argumument.

public class LargestOfThree {

public static void main(String[] args) {

if (args.length < 3) {

System.out.println("Please provide three numbers as arguments.");

return;

// Parse command-line arguments

int num1 = Integer.parseInt(args[0]);

int num2 = Integer.parseInt(args[1]);

int num3 = Integer.parseInt(args[2]);

// Find the largest number

int largest;

if (num1 > num2) {

if (num1 > num3) {

largest = num1;

} else {

largest = num3;

} else {

if (num2 > num3) {

largest = num2;

} else {
largest = num3;

System.out.println("The largest number is: " + largest);

(ii). Define Array.Discuss jagged array with suitable Example.

Definition of an Array

An array is a collection of elements of the same data type, stored in contiguous


memory locations. It allows random access to elements using their index. Arrays are
commonly used to store multiple values efficiently.

Jagged Array

A jagged array is an array of arrays, where each sub-array can have a different size or
length. Unlike a rectangular 2D array, the rows in a jagged array can have varying
numbers of elements.

Characteristics of Jagged Arrays

1. It is a one-dimensional array where each element is another array.

2. Each sub-array can have a different length.

3. It is useful when data structures of irregular shapes are needed.

Example in Java

java

Copy code

public class JaggedArrayExample {

public static void main(String[] args) {

// Declare a jagged array


int[][] jaggedArray = new int[3][];

// Initialize sub-arrays with different lengths

jaggedArray[0] = new int[]{1, 2};

jaggedArray[1] = new int[]{3, 4, 5};

jaggedArray[2] = new int[]{6, 7, 8, 9};

// Display the jagged array

for (int i = 0; i < jaggedArray.length; i++) {

for (int j = 0; j < jaggedArray[i].length; j++) {

System.out.print(jaggedArray[i][j] + " ");

System.out.println(); // New line after each row

2. A).Define JVM.Elaborate the internal architecture of JVM with neat diagram.

Definition of JVM (Java Virtual Machine)

The Java Virtual Machine (JVM) is a runtime environment that executes Java bytecode.
It acts as an interpreter and provides a platform-independent execution environment.
The JVM is part of the Java Runtime Environment (JRE).

Key Responsibilities of JVM

1. Load Bytecode: Reads the .class file (bytecode) generated by the Java compiler.

2. Verify Bytecode: Ensures the code adheres to Java's security and structure
rules.

3. Execute Bytecode: Executes bytecode using an interpreter or Just-In-Time (JIT)


compiler.
4. Memory Management: Manages memory allocation and garbage collection.

5. Exception Handling: Handles runtime errors or exceptions.

Internal Architecture of JVM

1. Classloader Subsystem

• Loads, links, and initializes Java classes.

• Phases:

o Loading: Finds and loads .class files.

o Linking: Combines classes and checks correctness.

o Initialization: Executes static blocks and initializes static variables.

2. Method Area

• Stores class structure, method metadata, and static variables.

• Shared among all threads.

3. Heap

• Memory for dynamically allocated objects and arrays.

• Garbage Collection happens here.

4. Stack

• Stores method-specific data like local variables, operand stacks, and method
calls.

• Each thread has its own stack.

5. Program Counter (PC) Register

• Holds the address of the current instruction being executed in a thread.

• Each thread has its own PC Register.

6. Native Method Stack

• Stores native (non-Java) method information like C or C++ functions.

7. Execution Engine

• Executes bytecode.

o Interpreter: Executes bytecode line by line.


o JIT Compiler: Converts bytecode into native machine code for faster
execution.

o Garbage Collector (GC): Reclaims unused memory in the heap.

8. Native Method Interface (JNI)

• Provides a bridge for Java code to call native code (C/C++).

9. Native Method Libraries

• Libraries required by native methods for execution.


b). Write a JAVA program to manipulate employee details of EmpID, EmpName,
EmpDesignation and EmpSalary by creating an instance class (Employee) and object
(Emp).

3. A) Write short note on method overloading. Why do we need method overloading


in JAVA. Write a JAVA program to demonstrate method overloading.

Method Overloading

Method Overloading is a feature in Java that allows a class to define multiple


methods with the same name but different parameter lists. The methods must
differ in:

1. Number of parameters

2. Type of parameters

3. Order of parameters
Method overloading is resolved during compile time (also known as compile-
time polymorphism).

Why Do We Need Method Overloading?

1. Improves Code Readability:


Having methods with the same name but different parameters makes the code
easier to understand.

2. Increases Flexibility:
It allows a single method name to handle different types or numbers of inputs.

3. Enhances Reusability:
Reduces the need to write separate methods for similar operations.

Code:

b) Explain Inheritance?Demonstrate the hybrid inheritance with suitable example.


What is Inheritance in Java?

Inheritance is a mechanism in object-oriented programming where a class (child or


subclass) acquires the properties and behaviors (fields and methods) of another class
(parent or superclass). It promotes code reuse and establishes a parent-child
relationship between classes.

Types of Inheritance in Java:

1. Single Inheritance: A subclass inherits from a single superclass.

2. Multilevel Inheritance: A class inherits from another class, which is itself


derived from another class.

3. Hierarchical Inheritance: Multiple classes inherit from a single superclass.

4. Hybrid Inheritance: A combination of two or more types of inheritance.

Summary:

• Single Inheritance: One class inherits from one parent class.

• Multilevel Inheritance: A class inherits from another class that is also a


subclass of another class.

• Hierarchical Inheritance: Multiple classes inherit from a single superclass.

• Hybrid Inheritance: A combination of different inheritance types, typically


involving interfaces and classes, to avoid issues like multiple inheritance (which
Java does not support directly).

Hybrid Inheritance in Java

Java does not support multiple inheritance directly due to ambiguity issues but
achieves hybrid inheritance using interfaces.

Example code:
4 a).Discuss the purpose of super keyword and final keyword with suitable example.

Purpose of super Keyword in Java

The super keyword in Java is used to refer to the parent class (superclass) and
serves multiple purposes:

1. Accessing Parent Class Constructor: The super keyword can be used to call
the constructor of the parent class. This is especially useful when the parent
class does not have a no-argument constructor or when you want to invoke a
constructor with specific parameters from the parent class.

2. Accessing Parent Class Methods and Variables: super is used to access


methods and variables of the superclass when they are overridden by the
subclass. This is helpful if you want to call the original method from the parent
class.
Example for super keyword:

Purpose of final Keyword in Java

The final keyword in Java can be applied to variables, methods, and classes to
impose restrictions:

1. Final Variable:
A variable declared as final cannot be modified once assigned a value. It acts as
a constant.

2. Final Method:
A method declared as final cannot be overridden by subclasses. This ensures
that the behavior of the method remains the same in all subclasses.

3. Final Class:
A class declared as final cannot be subclassed. It cannot be extended by other
classes.
Example for Final keyword:

Example for both keywords:


b) What is Wrapper class? How to string objects from the wrapper objects? Explain
with suitable example.

Wrapper Class in Java

A wrapper class in Java is a class that is used to convert a primitive data type into an
object. In other words, a wrapper class "wraps" the primitive data type inside an
object so that it can be used as an object in collections or other object-oriented
operations.

Java provides a wrapper class for each primitive data type:

• byte → Byte

• short → Short

• int → Integer

• long → Long

• float → Float

Why Wrapper Classes?

• They allow primitive types to be treated as objects.

• They are useful for working with collections like ArrayList, where only objects can
be stored (not primitive types).

• They provide utility methods to perform operations on values.

Converting Wrapper Objects to String Objects

You can convert a wrapper object to a String object using the following methods:

1. Using toString() method: All wrapper classes (like Integer, Double, etc.) override
the toString() method, which returns the string representation of the object.

2. Using String.valueOf() method: This method is used to convert a wrapper object


into a string

EXAMPLE:

You might also like