Java
Java is a high-level, object-oriented programming language that was
first released by Sun Microsystems in 1995.
It's designed to be portable and secure, with its most famous principle
being "Write Once, Run Anywhere" (WORA).
This means that Java code compiled on one platform (like a Windows
computer) can run on any other platform (like macOS or Linux) that has a
Java Virtual Machine (JVM).
The JVM acts as an interpreter, translating the compiled Java code
(bytecode) into machine-readable instructions.
Variables
A variable is a container which holds the value.A variable is the name of
reserved area allocated in memory. In other words, it is aname of the
memory location. It is a combination of "vary + able" which means its value
can be changed.
Types:
1. Local variable
2. Instance Variable
3. Static Variable
Local Variable:
A variable declared inside the body of the method is called local
variable.
Eg:
public class MyProgram {
public void myMethod() {
// 'message' is a local variable
String message = "Hello from a local variable!";
[Link](message);
}
}
Instance Variable:
The variables that are declared inside the class but outside the scope of
any method are called instance variables in Java.
Eg:
public class Car {
// 'color' is an instance variable
String color=”White”;
public void start() {
[Link]("The car color is “ +color);
}
}
Static variable:
A static variable is declared with the static keyword inside a class but outside
any method, constructor, or block. There is only one copy of a static variable
per class, shared by all objects of that class. They are often called global
variables.
Eg:
public class BankAccount {
// 'interestRate' is a static variable, shared by all accounts
public static double interestRate = 0.05;
public double calculateInterest(double balance) {
return balance * interestRate;
}
}
Keywords:
A keyword is a reserved word that has a predefined meaning to the
compiler and cannot be used as a variable, method, or class name. They are
fundamental building blocks of the language syntax.
Class:
The class is a blueprint (plan) of the object. It can be defined as a logical
template that share common properties and methods.
Syntax
Access_Specifier class Class_name
{
fields;
Methods;
}
Eg:
Public class Main{
public static void main(String[] args) {
[Link]("Hello World");
}
}
Object:
The object is an instance of a class. It is an entity that has
behaviour and state. Method defines the behaviour of the
object. and the class defines the state of the object.
Syntax:
Class_name object_name = new Constructor();
Example :
Main obj = new Main();
Method:
A method in Java is a block of code that, when called, performs specific
actions mentioned in it.
Syntax:
Access_specifier return_type method_name(){\
//block of code
}
Example:
public void add() {
//Block of codes
}
Looping Statements:
In Java, loops are control flow statements that enable the repeated execution of
a block of code based on a specified condition.
Types:
1. for loop
2. while loop
3. do-while loop
for loop
This loop is used when the number of iterations is known in advance. It consists
of an initialization, a condition, and an increment/decrement step.
Eg:
for (int i = 0; i < 5; i++) {
[Link](i);
}
While loop
This loop repeatedly executes a block of code as long as a given condition
remains true. The condition is checked before each iteration.
Eg:
int count = 0;
while (count < 3) {
[Link]("Count: " + count);
count++;
}
Do-while loop
Similar to the while loop, but the code block is executed at least once before the
condition is checked.
Eg:
int num = 0;
do {
[Link]("Number: " + num);
num++;
} while (num < 2);
Exception Handling
Exception handling in Java is a mechanism for managing runtime errors and
ensuring the application's normal flow continues despite unexpected events.
Key Concepts and Constructs:
Exceptions: An exception is an event that disrupts the normal flow of a
program. It is an object that is thrown at runtime.
Checked Exceptions: These are checked at compile time and must be
handled by the programmer (e.g., IOException, SQLException).
Unchecked Exceptions (Runtime Exceptions): These are not checked at
compile time and typically indicate programming errors (e.g.,
NullPointerException, ArithmeticException).
Keywords:
Java uses five keywords for exception handling:
1. Try
2. Catch
3. Finally
4. Throw
5. throws.
try block:
Encloses the code that might throw an exception.
Eg:
try {
// Code that might throw an exception
}
catch block:
Follows a try block and handles a specific type of exception.
Eg:
try {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
}
finally block:
Contains code that will execute regardless of whether an exception occurred or
was handled. It’s often used for cleanup operations.
Eg:
try {
// Code
} catch (ExceptionType e) {
// Handle exception
} finally {
// Code to execute always
}
throw keyword:
Used to explicitly throw an exception object.
Eg:
throw new MyCustomException("Something went wrong");
throws keyword:
Used in a method signature to declare that a method might throw one or
more specified checked exceptions, requiring the calling method to handle
them.
Eg:
public void readFile() throws IOException {
// Code that might throw IOException
}