Java
Java
What is Java?
Java is a programming language. Java is a high-level, object-oriented, and
secure programming language.
Need Of Java
It is a fast, secure, reliable programming language for coding everything from
mobile apps and enterprise software to big data applications and server-side
technologies.
Java has built-in security features like the Java Security Manager, which helps
restrict unauthorized access to system resources, and strong encryption
libraries, making it a preferred choice for applications that require high
security.
Introductions to Java
Java is a widely used, high-level, object-oriented programming language
designed to be simple, portable, secure, and efficient. It was created by
James Gosling and his team at Sun Microsystems in the mid-1990s and is
now owned by Oracle Corporation.
Java has become one of the most popular programming languages in the
world, largely due to its ability to run on a variety of platforms, its extensive
Java 1
ecosystem, and its widespread use in enterprise applications, mobile
development, web applications, and more.
Before Java, its name was ‘Oak.’ Oak was already a registered company, so
James Gosling and his team decided to change the name to ‘Java’.
Platform Independence:
Java is famous for its "Write Once, Run Anywhere" (WORA) philosophy.
When a Java program is compiled, it is converted into bytecode rather
than machine-specific code. This bytecode can then be run on any device
that has a Java Virtual Machine (JVM), which is available for virtually
every operating system.
Java 2
Java is designed to be robust, meaning it is reliable and minimizes errors.
It has strong memory management and error-handling mechanisms like
exception handling and automatic garbage collection.
Java is best known for its security. With Java, we can develop virus-free
systems. Java is secured because:
No explicit pointer
Class Loader
Java was designed to be easy to use and understand, even for beginners.
It simplifies certain aspects of programming (such as memory
management and syntax) compared to older languages like C++, which
makes it more accessible to developers.
Multithreading:
Java has built-in support for multithreading, which allows multiple tasks
to run concurrently within a program. This is particularly useful for
applications that require high performance, like games, servers, or media
applications.
Memory Management:
Java provides a vast collection of libraries and APIs that cover everything
from basic data structures to advanced features like networking, file I/O,
database access, GUI (Graphical User Interface) components, and more.
Java 3
The Java Standard Library (also known as the Java API) is one of the key
strengths of the language.
Portability:
As mentioned earlier, Java programs can be run on any platform that has a
JVM. This makes Java an excellent choice for cross-platform
development, including applications for Windows, macOS, Linux, and
mobile platforms like Android.
Developers write Java code using a simple text editor or a more advanced
Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA,
or NetBeans.
Java code is saved in files with the .java extension. These files are then
compiled into bytecode by the Java Compiler ( javac ), producing .class
files.
The bytecode is executed by the Java Virtual Machine (JVM). The JVM
translates the bytecode into machine code, which can be executed by the
operating system.
/*
Java 4
This is a multi-line comment
*/
1. Comments:
Comments are used to explain the code and are ignored by the compiler.
There are two types of comments:
2. Class Declaration:
3. Main Method:
public static void main(String[] args) : This is the entry point of any Java
application. The Java Virtual Machine (JVM) looks for this method when
executing a Java program.
public : Access modifier indicating that the method can be called from
anywhere.
static: This means that the method belongs to the class, rather than
instances of the class.
Java 5
void : Indicates that the method does not return any value.
: This is an array of
String[] args String objects that can hold
command-line arguments.
4. Statements:
Points to be noted:-
The file name should match the class name (e.g., MyFirstJavaProgram.java ), and
the class must be declared public if it is in a public file.
Variable
A variable is a container that holds data that can be changed during the execution
of a program. Variables allow you to store and manipulate data.
Types of Variable
There are three types of variables in Java:
local variable:- A variable declared inside the body of the method is called a
local variable. You can use this variable only within that method and the other
methods in the class aren't even aware that the variable exists.
instance variable:- A variable declared inside the class but outside the body
of the method is called an instance variable.
Java 6
among all the instances of the class. Memory allocation for static variables
happens only once when the class is loaded in the memory.
public class V
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}
1. Primitive data types: The primitive data types include boolean, char, byte,
short, int, long, float, and double.
Java 7
int 4 byte - 2,147,483,648 to
Operators
Operators in Java are special symbols that perform operations on variables and
values. They can be categorized based on the type of operation they perform.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 5 / 3 = 1
% Modulus 5 % 3 = 2
Java 8
< Less than 5 < 3 returns false
3. Logical Operators
Logical operators are used to perform logical operations on boolean values.
4. Assignment Operators
Assignment operators are used to assign values to variables.
x += 5; (equivalent to x = x +
+= Adds the value to the variable
5; )
x *= 5; (equivalent to x = x *
*= Multiplies the variable by a value
5; )
x /= 5; (equivalent to x = x /
/= Divides the variable by a value
5; )
x %= 5; (equivalent to x = x %
%= Takes the modulus of the variable
5; )
5. Unary Operators
Unary operators perform operations on a single operand. They can be used to
increment/decrement a value, invert a boolean, or change the sign.
Java 9
++ Increment (increases value by 1) x++ or ++x
6. Bitwise Operators
Bitwise operators perform operations on individual bits of integer values.
<< Left shift 5 << 1 results in 10 (in binary: 0101 << 1 = 1010 )
>> Right shift 5 >> 1 results in 2 (in binary: 0101 >> 1 = 0010 )
8. New Operator
The new operator is used to create new objects or arrays.
Java 10
Conditions
1. If Statement
It is the most basic statement among all control flow statements in Java. It
evaluates a Boolean expression and enables the program to enter a block of code
if the expression evaluates to true.
The syntax of if statement is given below.
if(condition) {
statement 1; //executes when condition is true
}
2. if-else statement
The if-else statement is an extension to the if-statement, which uses another
block of code, i.e., else block. The else block is executed if the condition of the if-
block is evaluated as false.
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Java 11
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
4. Switch Statement
The switch statement is an alternative to a series of if-else if statements when
you have many possible conditions based on a single variable. It evaluates an
expression and compares it to the values of each case in the switch block.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
default:
// Code to execute if no cases match
}
Loop
Java 12
loops are used to repeatedly execute a block of code as long as a specified
condition is true . There are three main types of loops in Java:
1. For Loop
2. While Loop
3. Do-While Loop
1. For Loop
The for loop is typically used when the number of iterations is known
beforehand. It consists of three parts: initialization, condition, and
increment/decrement.
Syntax:
Example:
Java 13
for (Type element : collection) {
// Code to execute
}
Example:
2. While Loop
The while loop is used when you don't know in advance how many times the loop
will run, but you want to keep looping as long as a condition is true. The condition
is checked before each iteration.
Syntax:
while (condition) {
// Code to execute repeatedly
}
Example:
int i = 0;
while (i < 5) {
System.out.println("i = " + i);
Java 14
i++;
}
3. Do-While Loop
The do-while loop is similar to the while loop, but the condition is checked after
the loop body is executed. This ensures that the loop will execute at least once,
even if the condition is false initially.
Syntax:
do {
// Code to execute
} while (condition);
4. Break Statement
The break statement is used to exit a loop (or a switch statement).
Example:
Java 15
}
5. Continue Statement
The continue statement is used to skip the current iteration of a loop and proceed
to the next iteration.
Example:
Explanation:
When i == 3 , the continue statement causes the loop to skip printing that value
and jump to the next iteration.
Methods
In Java,
methods are blocks of code that perform a specific task. They allow you to write
reusable code that can be executed when called. Methods in Java are similar to
functions in other programming languages.
method also provides the easy modification and readability of code, just by
adding or removing a chunk of code. The method is executed only when we call
or invoke it.
Java 16
The most important method in Java is the main() method.
2. Method Invocation/Calling
5. Static Methods
1. Method Declaration/Definition
A method is defined with the following components:
Access Modifier: Determines the visibility of the method (e.g., public , private ,
protected ).
Return Type: Specifies the type of value the method returns (e.g., int , String ,
void ).
Method Name: The name used to call the method (e.g., addNumbers ).
Parameters (Optional): Input values that are passed to the method (e.g., int
a, int b ).
Method Body: The block of code that defines what the method does.
Syntax:
Example:
Java 17
public int addNumbers(int a, int b) {
return a + b;
}
2. Method Invocation/Calling
To execute a method, you need to invoke it. Methods can be called in two main
ways:
Instance Method Call: If the method is not static , you need to create an
object of the class to call the method.
Static Method Call: If the method is static , you can call it directly using the
class name without creating an object.
Java 18
public class Calculator {
public static int multiplyNumbers(int a, int b) {
return a * b;
}
Arguments are the actual values passed to the method when it's called.
Example:
void: This means the method does not return any value.
Return Type (e.g., int, String, etc.): Specifies the type of value the method
returns.
Java 19
Example of a method with return type:
In this example, int is the return type, meaning the method will return an integer
value. The return keyword is used to return the result.
7. Static Methods
A static method belongs to the class rather than to any specific object. Static
methods can be called without creating an instance of the class.
Accessing static methods: Static methods can be called directly using the
class name.
Java 20
public static void main(String[] args) {
int result = MathUtils.add(10, 20); // Calling a sta
tic method
System.out.println(result); // Output: 30
}
}
OOP’s
OOP’s
Java 21