Introduction: Genesis and Evolution of Java Language
Genesis:
o Developed by Sun Microsystems in the early 1990s, primarily by James Gosling.
o Initially called Oak, later renamed Java.
o Designed to be simple, object-oriented, and platform-independent.
Evolution:
o Java 1.0 (1995): WORA (Write Once, Run Anywhere) capability introduced.
o Java 2 (1998): Introduced Swing, collections framework, and JIT compiler.
o Continued evolution through versions, with the current versions supporting
features like lambdas, streams, modules, etc.
Internet & Java
Java was designed with networking in mind, making it ideal for the web.
Features like applet support, servlets, and JSP (Java Server Pages) emphasize Java’s
integration with the Internet.
Popular for building dynamic web applications due to its robust libraries and frameworks.
Bytecode and its Features
Bytecode: Intermediate code generated by the Java Compiler.
o Stored in .class files.
o Platform-independent and executed by the Java Virtual Machine (JVM).
Features:
o Ensures portability.
o Facilitates security as it runs in a sandbox environment.
o Enables optimization through Just-In-Time (JIT) compilation.
Java Program Structure and Class Library
Structure:
java
Copy code
public class ClassName {
public static void main(String[] args) {
// Program logic here
}
}
Java Class Library:
o Rich set of pre-written classes and methods.
o Includes packages like java.lang, java.util, java.io, etc.
Data Types, Variables, and Operators
Data Types:
o Primitive Types: int, float, double, char, boolean, etc.
o Reference Types: Arrays, Classes, Interfaces.
Variables:
o Must be declared before use.
o Example: int num = 10;
Operators:
o Arithmetic: +, -, *, /, %
o Relational: >, <, >=, <=, ==, !=
o Logical: &&, ||, !
Operator Precedence
Determines the order of execution in complex expressions.
Example: * and / have higher precedence than + and -.
Control Statements
Selection Statements:
o if, if-else, switch.
Scope of Variables:
o Local Scope: Declared inside methods or blocks.
o Class Scope: Declared at the class level (instance/static variables).
Iterative Statements:
o for, while, do-while.
Defining Classes and Methods
Defining a Class:
class MyClass {
int num;
void display() {
System.out.println("Number: " + num);
}
}
MyClass obj = new MyClass();
obj.num = 10;
obj.display();
Garbage Collection:
o Automatic memory management in Java.
o JVM's Garbage Collector removes unreferenced objects.
Arrays and Strings
Arrays
Collection of elements of the same data type.
o Example:
java
Copy code
int[] numbers = {1, 2, 3, 4};
Arrays of Characters:
char[] chars = {'J', 'a', 'v', 'a'};
String Handling
String Class: Immutable sequences of characters.
o Example:
java
Copy code
String str = "Hello, Java!";
Common String Methods:
o length(), substring(), concat(), replace(), toUpperCase(), etc.
. Arrays
Definition:
Arrays are collections of data items of the same type stored in contiguous memory
locations.
Declaration and Initialization:
Single-Dimensional Arrays
int[] numbers = {10, 20, 30, 40};
Multi-Dimensional Arrays
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Key Operations:
Accessing Elements
System.out.println(numbers[0]); // Output: 10
Traversing
for (int num : numbers) {
System.out.println(num);
}
. Arrays of Characters
An array of characters can represent a sequence of characters or a string-like structure.
Example:
char[] chars = {'J', 'a', 'v', 'a'};
System.out.println(chars); // Output: Java
Conversion to String
String str = new String(chars);
System.out.println(str); // Output: Java
3. String Handling Using the String Class
Definition:
The String class in Java is used to create immutable (unchangeable) sequences of
characters.
Declaration and Initialization:
String str1 = "Hello";
String str2 = new String("World");
Common Operations:
Concatenation:
String result = str1 + " " + str2;
System.out.println(result); // Output: Hello World
OR
String result = str1.concat(" ").concat(str2);
Length:
System.out.println(str1.length()); // Output: 5
Character Access:
System.out.println(str1.charAt(0)); // Output: H
Substring:
java
Copy code
String sub = str1.substring(1, 4);
System.out.println(sub); // Output: ell
Replace:
String replaced = str1.replace('l', 'p');
System.out.println(replaced); // Output: Heppo
Case Conversion:
System.out.println(str1.toUpperCase()); // Output: HELLO
System.out.println(str1.toLowerCase()); // Output: hello
Trim:
String str = " Java ";
System.out.println(str.trim()); // Output: Java
Comparison:
System.out.println(str1.equals(str2)); // Output: false
4. StringBuffer Class
Definition:
The StringBuffer class is used to create mutable (modifiable) sequences of characters.
It is preferred when multiple modifications to a string are needed.
Declaration and Initialization:
java
Copy code
StringBuffer sb = new StringBuffer("Hello");
Key Operations:
Append:
sb.append(" World");
System.out.println(sb); // Output: Hello World
Insert:
sb.insert(6, "Java ");
System.out.println(sb); // Output: Hello Java World
Replace:
sb.replace(6, 10, "C++");
System.out.println(sb); // Output: Hello C++ World
Delete:
sb.delete(6, 9);
System.out.println(sb); // Output: Hello World
Reverse:
sb.reverse();
System.out.println(sb); // Output: dlroW olleH
Capacity:
System.out.println(sb.capacity()); // Default is 16 + length of the string
Set Length:
sb.setLength(5);
System.out.println(sb); // Output: Hello
Comparison: String vs StringBuffer
Feature String StringBuffer
Mutability Immutable Mutable
Performance Slower for modifications Faster for modifications
Thread-Safety Not thread-safe Thread-safe