Java Unit-1
Java Unit-1
Introduction of java
Java is a widely-used, high-level, object-oriented programming language developed by Sun
Microsystems (now owned by Oracle Corporation) in 1995. It was designed to be platform-
independent, meaning that programs written in Java can run on any device that has a Java Virtual
Machine (JVM) installed, regardless of the underlying hardware and operating system. This "write
once, run anywhere" capability has made Java immensely popular for developing a variety of
software applications, ranging from enterprise systems to mobile apps and embedded devices.
Key features of Java include:
1. Object-oriented: Java follows the object-oriented programming (OOP) paradigm,
allowing developers to create modular, reusable code by organizing data into objects and
defining their behavior through classes and methods.
2. Platform independence: Java programs are compiled into bytecode, which can be
executed on any device with a Java Virtual Machine (JVM). This enables developers to
write code once and deploy it across multiple platforms without modification.
3. Robust and secure: Java's strong type system, automatic memory management (garbage
collection), and exception handling mechanisms contribute to its robustness. Additionally,
Java's security features, such as the sandbox environment for executing untrusted code,
help prevent malicious attacks.
4. Multi-threaded: Java provides built-in support for concurrent programming through its
multithreading capabilities, allowing developers to create applications that can perform
multiple tasks simultaneously.
5. Rich standard library: Java comes with a comprehensive standard library (Java API) that
provides ready-to-use classes and methods for common programming tasks, including
input/output operations, networking, data manipulation, and GUI development.
6. Community support: Java has a large and active community of developers, which
contributes to its extensive ecosystem of frameworks, libraries, and tools. This ecosystem
includes popular frameworks like Spring, Hibernate, and Apache Struts, as well as
integrated development environments (IDEs) like Eclipse and IntelliJ IDEA.
Java's combination of versatility, portability, and robustness has made it a cornerstone of modern
software development, powering a wide range of applications across various industries.
Java Environment
The Java environment consists of several components that collectively enable the development,
execution, and management of Java applications. Here are the key components of the Java
environment:
1. Java Development Kit (JDK): The JDK is a software development kit that provides the
tools and libraries necessary for developing Java applications. It includes the Java Compiler
(javac) for compiling Java source code into bytecode, the Java Virtual Machine (JVM) for
executing bytecode on various platforms, and a set of standard libraries and development
tools (such as the Java Development Kit (JDK)). The JDK also includes the Java Runtime
Environment (JRE), which is needed to run Java applications on a computer.
2. Java Runtime Environment (JRE): The JRE is a runtime environment that provides the
necessary libraries and components to run Java applications. It includes the JVM, which
interprets and executes Java bytecode, as well as core libraries and runtime support files.
The JRE does not include development tools like the JDK; it is intended for end-users who
only need to run Java applications.
3. Java Virtual Machine (JVM): The JVM is a crucial component of the Java environment
responsible for executing Java bytecode. It abstracts the underlying hardware and operating
system, providing a platform-independent execution environment for Java applications.
The JVM interprets bytecode or, in some cases, just-in-time (JIT) compiles it into native
machine code for improved performance.
4. Java Standard Library: Java comes with a comprehensive standard library (also known
as the Java API or Java SDK), which provides a wide range of classes and packages for
common programming tasks. The standard library includes classes for input/output
operations, networking, collections, concurrency, GUI development (with AWT and
Swing), database connectivity (with JDBC), and more.
5. Integrated Development Environments (IDEs): IDEs are software applications that
provide developers with tools and features to write, debug, and deploy Java code more
efficiently. Popular Java IDEs include Eclipse, IntelliJ IDEA, NetBeans, and Oracle
JDeveloper. These IDEs typically offer features such as code editors with syntax
highlighting, code completion, debugging tools, version control integration, and project
management capabilities.
6. Build Tools: Build tools automate the process of compiling, testing, and packaging Java
applications. Apache Maven and Gradle are widely-used build automation tools in the Java
ecosystem. They manage project dependencies, execute build tasks, and generate artifacts
such as JAR files and WAR files. These tools also support integration with continuous
integration (CI) and continuous delivery (CD) pipelines.
7. Application Servers: For developing and deploying enterprise Java applications,
application servers provide a runtime environment that supports features such as
transaction management, security, scalability, and reliability. Examples of Java application
servers include Apache Tomcat, WildFly (formerly JBoss), Apache TomEE, and IBM
WebSphere.
These components collectively form the Java environment, providing developers with the tools,
libraries, and runtime support necessary for building and running Java applications across different
platforms and environments.
// 2. Class Declaration
public class MyClass {
// 4. Program Logic
System.out.println("Hello, World!");
}
Statements
A statement is a single line or a block of code that performs a specific action. Java statements are
the building blocks of Java programs, and they are executed sequentially, one after another, unless
control flow statements alter the normal flow of execution. There are several types of statements
in Java:
1. Declaration Statements: Declaration statements are used to declare variables or constants.
They typically include the variable's data type followed by the variable name, and
optionally, an initial value. For example:
int age; double pi = 3.14; final int MAX_VALUE = 100;
2. Expression Statements: Expression statements are used to evaluate expressions.
Expressions can be simple variables, method calls, or more complex mathematical
calculations. For example:
int sum = a + b; System.out.println("Hello, World!");
3. Assignment Statements: Assignment statements are used to assign values to variables.
They typically include the variable name followed by the assignment operator (=) and the
value to be assigned. For example:
int x = 10;
4. Control Flow Statements: Control flow statements are used to control the flow of
execution in a Java program. They include conditional statements (such as if, else, switch),
looping statements (such as for, while, do-while), and branching statements (such as
break, continue, return).
For example:
if (x > 0) {
System.out.println("Positive");
}
else
{
System.out.println("Non-positive");
}
5. Block Statements: Block statements are used to group multiple statements together. They
are enclosed within curly braces {}. Block statements are commonly used with control flow
statements to execute multiple statements conditionally or repeatedly. For example:
int a = 10; int b = 20;
int sum = a + b;
System.out.println("Sum: " + sum);
These are some of the fundamental types of statements in Java, and they are used extensively to
define the behavior of Java programs.
Data Types
1. Primitive Data Types: Primitive data types represent basic values and are predefined by
the Java language. They are divided into four categories: integer types, floating-point types,
boolean type, and character type.
Integer Types:
byte: 8-bit signed integer. Range: -128 to 127.
short: 16-bit signed integer. Range: -32,768 to 32,767.
int: 32-bit signed integer. Range: -2^31 to 2^31 - 1.
long: 64-bit signed integer. Range: -2^63 to 2^63 - 1.
Floating-Point Types:
float: 32-bit floating-point number. Range: ±1.4E-45 to ±3.4E+38. Suffix:
f or F.
double: 64-bit floating-point number. Range: ±4.9E-324 to ±1.7E+308.
Boolean Type:
boolean: Represents true or false values only. Size: Not precisely defined.
Character Type:
char: 16-bit Unicode character. Range: '\u0000' (0) to '\uffff' (65,535).
2. Reference Data Types: Reference data types are used to refer to objects. They include
classes, interfaces, arrays, and enumerations.
Classes: User-defined data types created using the class keyword.
Interfaces: Similar to classes but define a contract for the behavior of a class.
Arrays: Ordered collections of elements of the same data type.
Enumerations: Special types that define a set of constants.
Data Type Size (in bits) Range Default Value Wrapper Class
byte 8 -128 to 127 0 Byte
short 16 -32,768 to 32,767 0 Short
Integer
int 32 -2^31 to 2^31 - 1 0 Integer
long 64 -2^63 to 2^63 - 1 0 Long
float 32 ±1.4E-45 to ±3.4E+38 0.0 Float
double 64 ±4.9E-324 to ±1.7E+308 0.0 Double
boolean Not precisely Not precisely defined false Boolean
defined
char 16 '\u0000' (0) to '\uffff' '\u0000' Character
(65,535)
Constants:
Constants are values that do not change during the execution of a program.
They are fixed and immutable.
Constants are typically used to represent values that remain constant throughout the
program, such as mathematical constants (e.g., pi), configuration parameters, or
fixed values used in calculations.
In Java, constants are often declared using the final keyword to indicate that their
value cannot be modified once initialized.
Example
final double PI = 3.14159; final int MAX_SIZE = 100;
2. Variables:
Variables are named memory locations used to store data that can vary or change
during the execution of a program.
They are used to hold values that may be modified or updated as the program runs.
Variables have a data type (such as int, double, String) that determines the type of
data they can hold.
In Java, variables must be declared with a specific data type before they can be
used, and they can be assigned values using the assignment operator =.
Example:
int age = 25; double temperature = 98.6; String name = "John";
Constants represent fixed values that do not change, while variables represent mutable data that
can be modified during program execution. Both constants and variables are essential for storing
and manipulating data in Java programs.
Operators
1. Arithmetic Operators:
Used to perform mathematical operations.
Include addition +, subtraction -, multiplication *, division /, modulus % (remainder), and
unary minus -.
Example:
int a = 10, b = 5;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus
int negation = -a; // Unary minus
Relational Operators:
Used to compare values.
Include equal to ==, not equal to !=, greater than >, less than <, greater than or equal to >=,
and less than or equal to <=.
Return boolean values (true or false).
Example:
int x = 10, y = 5;
boolean isEqual = (x == y); // Equal to
boolean isNotEqual = (x != y); // Not equal to
boolean isGreater = (x > y); // Greater than
boolean isLess = (x < y); // Less than
boolean isGreaterOrEqual = (x >= y); // Greater than or equal to
boolean isLessOrEqual = (x <= y); // Less than or equal to
Logical Operators:
Used to perform logical operations on boolean values.
Include logical AND &&, logical OR ||, and logical NOT !.
Example:
boolean condition1 = true, condition2 = false;
boolean resultAnd = condition1 && condition2; // Logical AND
boolean resultOr = condition1 || condition2; // Logical OR
boolean resultNot = !condition1; // Logical NOT
Assignment Operators:
Used to assign values to variables.
Include simple assignment =, compound assignment +=, -= , *=, /=, and %=.
Example:
int x = 10;
x += 5; // Equivalent to x = x + 5;
Bitwise Operators:
Used to perform operations at the bit-level.
Include bitwise AND &, bitwise OR |, bitwise XOR ^, bitwise complement ~, left shift <<,
right shift >>, and zero-fill right shift >>>.
Example:
int a = 5, b = 3;
int bitwiseAnd = a & b; // Bitwise AND
int bitwiseOr = a | b; // Bitwise OR
int bitwiseXor = a ^ b; // Bitwise XOR
int bitwiseComplement = ~a; // Bitwise complement
int leftShift = a << 1; // Left shift
int rightShift = a >> 1; // Right shift
Special Operators:
Include the ternary operator ? : and the instanceof operator instanceof.
Example:
int result = (x > y) ? x : y; // Ternary operator
boolean isString = (obj instanceof String); // instanceof operator