JAVA
JAVA
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.
if (args.length < 3) {
return;
int largest;
largest = num1;
} else {
largest = num3;
} else {
largest = num2;
} else {
largest = num3;
Definition of an Array
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.
Example in Java
java
Copy code
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).
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.
1. Classloader Subsystem
• Phases:
2. Method Area
3. Heap
4. Stack
• Stores method-specific data like local variables, operand stacks, and method
calls.
7. Execution Engine
• Executes bytecode.
Method Overloading
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).
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:
Summary:
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.
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.
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:
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.
• byte → Byte
• short → Short
• int → Integer
• long → Long
• float → Float
• They are useful for working with collections like ArrayList, where only objects can
be stored (not primitive types).
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.
EXAMPLE: