java
java
Java was initiated in 1991 by James Gosling and his team at Sun Microsystems. Originally named Oak, it
was intended for embedded devices and set-top boxes. The language was rebranded as Java in 1995,
aligning with the launch of the Java 1.0 platform. Java quickly gained popularity due to its promise of
"write once, run anywhere" (WORA), allowing developers to create applications that could run on any
device with a JVM. Over the years, Java has evolved through numerous versions, adding features like
generics, annotations, lambda expressions, and modules, keeping it relevant for modern software
development.
Java is often characterized by certain key phrases that define its core principles:
- **Object-Oriented**: Java supports the OOP paradigm, facilitating code reusability and modularity.
- **Platform-Independent**: Java code is compiled into bytecode, which can run on any JVM, making it
portable across platforms.
- **Secure**: Java provides a secure environment through its class loader, bytecode verifier, and
security manager.
- **Robust**: Strong type checking, exception handling, and garbage collection contribute to Java’s
robustness.
- **Interpreted**: Java programs are interpreted at runtime by the JVM, which increases flexibility.
- **High Performance**: Through Just-In-Time (JIT) compilation, Java can achieve high performance.
The conditional operator is a shorthand for the `if-else` statement. It evaluates a boolean expression and
returns one of two values based on the result.
**Syntax**:
```java
```
**Example**:
```java
```
Iteration statements enable you to execute a block of code multiple times. The main types include:
- **do-while loop**: Executes at least once and then continues based on a condition.
**Example**:
```java
// For loop
// While loop
int i = 1;
while (i <= 5) {
i++;
}
// Do-while loop
int j = 1;
do {
j++;
```
An array is a data structure that holds a fixed number of values of a single type. It is indexed, meaning
each element can be accessed via its position.
```java
```
```java
```
- `==` (equal to), `!=` (not equal to), `>` (greater than), `<` (less than), `>=`, `<=`.
- `&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (left shift), `>>` (right shift).
- **continue**: Skips the current iteration and continues with the next iteration of the loop.
- **return**: Exits from the current method and optionally returns a value.
**Examples**:
```java
// break example
// continue example
System.out.println(i); // Prints 0, 1, 3, 4
}
```
Command line arguments allow passing information to a Java program at runtime. They are received in
the `main` method as an array of `String`.
**Example Program**:
```java
```
You would run this program from the command line like this:
```
java AddNumbers 5 10
```
A data type defines the kind of data a variable can hold, including its size and the operations that can be
performed on it.
**Declaration**:
```java
int number = 5; // Declaring an integer variable
```
**Rules**:
1. Must start with a letter (a-z, A-Z), dollar sign `$`, or underscore `_`.
Bitwise operators are used to perform operations on bits. They are useful for low-level programming
tasks.
**Example**:
```java
int a = 5; // 0101
int b = 3; // 0011
```
**Example Structure**:
```java
System.out.println("Hello, World!");
```
Java supports various types of programs, including console applications, GUI applications, and web
applications.
In Java, the main method serves as the entry point of any standalone application. The correct signature
must be:
**Invalid Declarations**:
- **Logical Operators**:
- **Unary Operators**: Operate on a single operand (e.g., increment `++`, decrement `--`).
- **Conditional Statements**:
- **Looping Statements**:
- `for`, `while`, `do-while`: Repeat a block of code multiple times based on conditions.
**a) Encapsulation**: This principle restricts direct access to some of an object’s components. It is
achieved by using private variables and providing public methods to access and modify those variables.
**b) Polymorphism**: Allows methods to do different things based on the object they are acting upon.
This can be achieved through method overriding and method overloading.
**c) Inheritance**: Enables a new class (subclass) to inherit attributes and methods from an existing
class (superclass), promoting code reuse.
Constructors are special methods invoked when an object is instantiated. They can be parameterized or
default.
**Example**:
```java
class MyClass {
MyClass() {
// Default constructor
MyClass(int x) {
// Parameterized constructor
```
**a) Overloading**: Refers to defining multiple methods with the same name but different parameters
(type or number).
- **Method Overloading**:
```java
void display(int a) { }
void display(String b) { }
```
- **Constructor Overloading**:
```java
MyClass() { }
MyClass(int a) { }
```
Recursion is a programming technique where a method calls itself to solve smaller instances of a
problem. It is essential to define a base case to prevent infinite recursion.
```java
```
This program computes the factorial of 5 by repeatedly calling the `factorial` method.
- **Java API**: A collection of pre-written classes and interfaces that facilitate various programming
tasks.
- **Development Tools**: Such as `javadoc` for documentation, `jar` for packaging applications, etc.
1. **Memory Management**: C requires manual memory management using pointers; Java uses
automatic garbage collection.
2. **Platform Dependency**: C code must be compiled for each specific platform; Java bytecode runs
on any platform with a JVM.
3. **Syntax**: Java’s syntax is stricter, especially with regards to type checking and object-oriented
principles.
4. **Exception Handling**: Java has built-in support for exceptions; C relies on return codes and manual
checks.
1. **Memory Management**: C++ allows for manual memory management; Java relies on garbage
collection.
2. **Multiple Inheritance**: C++ supports multiple inheritance; Java allows it only through interfaces.
3. **Pointers**: C++ utilizes pointers for memory management, while Java abstracts this with
references.
4. **Syntax and Structure**: Java enforces a strict object-oriented model, while C++ supports both
procedural and object-oriented programming.
- **Documentation comment**: `/** This is a documentation comment */` used for generating API
documentation.
### 21. Data Types Supported in Java
- **Reference Types**: Used for objects and arrays, pointing to memory locations where data is stored.
- **JVM (Java Virtual Machine)**: The engine that executes Java bytecode, providing a platform-
independent execution environment.
- **Bytecode**: An intermediate representation of Java source code, compiled by the Java compiler. It
is platform-independent and can be executed by any JVM.
2. **Inheritance**: Enables new classes to inherit properties and behaviors from existing classes,
facilitating code reuse.
3. **Polymorphism**: Allows methods to be called in different contexts and behave differently based
on the object.
A class is a blueprint for creating objects, encapsulating data and methods. An instance is a specific
realization of a class.
**Example**:
```java
class Dog {
String breed;
void bark() {
System.out.println("Woof!");
}
```
Objects are instances of classes that represent real-world entities. You create an object using the `new`
keyword.
**Creating Objects**:
```java
```
If you have more specific aspects you'd like to dive into, just let me know!
Java was developed by Sun Microsystems in the mid-1990s, originally called Oak. It was designed for
interactive television but evolved into a versatile programming language suitable for various
applications. The first public release was in 1995, and it emphasized portability, security, and
robustness, becoming the backbone of many enterprise applications and Android development.
The conditional operator (ternary operator) takes three operands and is used for decision-making.
**Example**:
```java
```
Iteration statements are used for looping through a block of code. Common types include `for`, `while`,
and `do-while`.
**Example**:
```java
```
An array is a collection of elements of the same type, stored in contiguous memory locations.
**Example**:
```java
```
**Example**:
```java
```
**Declaration**:
```java
int number = 5;
```
**Rules**:
**Example**:
```java
int a = 5; // 0101
int b = 3; // 0011
```
Java programs are structured into classes and methods. A basic program includes:
```java
// Code here
```
- **Invalid**: `public void main();`, `public static void main(String );`, `public static int main(String
args[]);`
Operators include:
**a) Encapsulation**: Bundling data and methods that operate on data within a single unit (class).
**b) Polymorphism**: Ability of a method to perform differently based on the object that it is acting
upon.
**c) Inheritance**: Mechanism to create new classes from existing ones, inheriting fields and methods.
Constructors are special methods called when an object is instantiated. They can be overloaded.
**Example**:
```java
class MyClass {
```
```java
if (n == 1) return 1;
```
1. **Memory Management**: C++ allows manual memory management; Java uses garbage collection.
2. **Multiple Inheritance**: C++ supports multiple inheritance; Java does not (interfaces instead).
A class is a blueprint for creating objects. An instance is a concrete object created from the class.
```java
class Dog {
```
**Creating Objects**:
```java
```