Summary - Java
Summary - Java
Summary - Java
UNIT-I Summary with Syntax
1. Introduction to Java
1.1 History of Java
First version (Java 1.0) was released in 1996 with the "Write Once, Run
Anywhere" promise.
Major updates:
Java 17 (2021): Current LTS version with advanced features like sealed
classes and pattern matching.
1. Installing JDK:
2. Choosing an IDE:
Summary - Java 1
System.out.println("Hello, World!");
}
}
4. Learning Basics:
2. Java Programs
2.1 Types of Java Programs
Java Application:
Java Applet:
Key Differences:
3. Variables
3.1 Declaring and Initializing Variables
1. Local Variables:
Summary - Java 2
Example:
2. Instance Variables:
Example:
class MyClass {
int instanceVar; // Instance variable
}
3. Static Variables:
Example:
class MyClass {
static int staticVar = 10;
}
int studentAge;
4. Operators
4.1 Common Operators and Examples
Arithmetic Operators:
Assignment Operators:
int x = 10;
x += 5; // Equivalent to x = x + 5
Relational Operators:
Summary - Java 3
boolean isEqual = (5 == 5); // true
boolean isGreater = (10 > 5); // true
Logical Operators:
Ternary Operator:
5. Data Types
5.1 Primitive Data Types
Arrays:
Interfaces:
interface MyInterface {
void performAction();
}
6. Tokens
6.1 Types of Tokens
1. Keywords:
2. Identifiers:
Summary - Java 4
Names given to variables, methods, classes.
3. Literals:
4. Operators:
Symbols like + , , = .
5. Special Symbols/Separators:
{ } , [ ] , ( ) , ; .
Conclusion:
Unit 1 introduces the basics of Java, including:
4. The concept of tokens, which are the building blocks of a Java program.
Case-sensitive.
Summary - Java 5
public class SubClass extends MyClass {
// Subclass body
}
2. Data Members
2.1 Declaring Data Members:
Variables that hold data associated with a class are declared within the class
but outside methods.
Use meaningful names, follow camelCase, and use uppercase for constants.
int studentAge;
final int MAX_LIMIT = 100;
myObject.age = 25;
System.out.println(myObject.age);
3. Methods
3.1 Using Data Members in Methods:
Summary - Java 6
3.2 Invoking Methods:
Call methods using the object reference and the dot operator.
myObject.setName("John");
Use the object reference followed by the method name and arguments.
myObject.setAge(30);
class MyClass {
int data = 10; // Default access modifier
}
4.2 public:
4.3 private:
4.4 protected:
4.5 static:
4.6 final:
Summary - Java 7
final int MAX_VALUE = 1000;
5. Overloading
5.1 Method Overloading:
public MyClass() {
// Default constructor
}
Interface Example:
Enum Example:
Summary - Java 8
enum Days {
MONDAY, TUESDAY, WEDNESDAY;
}
class OuterClass {
class InnerClass {
// Inner class body
}
}
class OuterClass {
static class NestedClass {
// Static nested class
}
}
if (condition) {
// Code block
} else {
// Code block
}
switch (value) {
case 1:
// Code block
break;
default:
// Default block
break;
}
while (condition) {
// Code block
}
Summary - Java 9
7.5 Do-While Loop:
do {
// Code block
} while (condition);
8. Array
8.1 One-Dimensional Array:
int[][] matrix = {
{1, 2},
{3, 4}
};
arr[0] = 10;
9. String
9.2 Common String Methods:
Summary - Java 10
str.indexOf("e");
str.toUpperCase();
str.toLowerCase();
str.equals("text");
str.split(" ");
10. Inheritance
Single Inheritance Example:
class Parent {
void display() {
System.out.println("Parent");
}
}
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {}
public void methodB() {}
}
11. Interfaces
11.1 Defining an Interface:
Summary - Java 11
interface ParentInterface {
void parentMethod();
}
1. Packages
1.1 Grouping Related Classes and Interfaces
Packages group similar classes and interfaces for better modularity and code
organization.
1.2 Built-in Packages
package mypackage;
import mypackage.MyClass;
Summary - Java 12
}
}
2. Applet
2.1 Small Java Programs That Can Run in Web Browsers
Applets are small programs embedded in HTML pages.
2.2 Applet Lifecycle Methods
import java.applet.Applet;
import java.awt.Graphics;
HTML Example:
Summary - Java 13
<applet code="MyApplet.class" width="300" height="300">
<param name="message" value="Hello, Parameters!">
</applet>
3. Thread
3.1 Concurrent Execution of Multiple Tasks
Threads allow simultaneous execution of tasks.
3.2 Creating Threads
By Extending Thread :
By Implementing Runnable :
Summary - Java 14
System.out.println(i);
}
}
thread.setPriority(Thread.MAX_PRIORITY);
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This block is always executed.");
}
Summary - Java 15
Button button = new Button("Click Me");
TextField textField = new TextField(20);
Label label = new Label("Enter Text:");
setLayout(new FlowLayout());
6. Graphics
6.1 Drawing Shapes, Lines, and Text
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
Conclusion:
This unit focuses on key concepts like:
Summary - Java 16
4. Robust error handling using exceptions.
AWT is a toolkit for creating GUI components and handling user interactions in
Java.
FlowLayout :
frame.setLayout(new FlowLayout());
BorderLayout :
frame.setLayout(new BorderLayout());
GridLayout :
Summary - Java 17
button.addActionListener(e -> System.out.println("Button clic
ked!"));
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked at: " + e.getPoint
());
}
});
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyChar());
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
3. Graphics Class
3.1 Drawing Shapes and Lines
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Hello Graphics!", 50, 50);
Summary - Java 18
Image img = Toolkit.getDefaultToolkit().getImage("example.jp
g");
g.drawImage(img, 50, 50, this);
4. Menus
4.1 Creating Menu Bars, Menus, and Menu Items
Summary - Java 19
5. Image
5.1 Loading and Displaying Images
g.drawImage(img, 0, 0, this);
Java Streams facilitate input and output operations, such as reading/writing files.
6.2 Byte Streams and Character Streams
Summary - Java 20
FileReader reader = new FileReader("input.txt");
FileWriter writer = new FileWriter("output.txt");
int data;
while ((data = reader.read()) != -1) {
writer.write(data);
}
reader.close();
writer.close();
Conclusion:
This unit emphasizes advanced GUI programming in Java with AWT, covering:
Summary - Java 21