Mca 1 Unit Notes JAVA
Mca 1 Unit Notes JAVA
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.
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.
Structure:
java
Copy code
public class ClassName {
public static void main(String[] args) {
// Program logic here
}
}
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
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 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 of Characters:
char[] chars = {'J', 'a', 'v', 'a'};
String Handling
. 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
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
Definition:
The String class in Java is used to create immutable (unchangeable) sequences of
characters.
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.
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