Introduction to Java
1.Features of Java
Java is a popular, secure, and powerful high level programming language.
Here are its key features:
1. Platform Independent: Java programs run on any operating system (Windows, Mac,
Linux).Uses JVM (Java Virtual Machine) to make it independent.
2. Simple & Easy to learn.
3. Object-Oriented: Everything in Java is based on objects and classes (e.g., Car, Student).
4. Secure: Java does not allow direct memory access, preventing viruses and hacking.
5. Portable: Write once, run anywhere! Java code can be moved between different computers
without changes.
6. Multithreading: Java allows multiple tasks to run at the same time (e.g., music playing
while typing).
7. Automatic Memory Management (Garbage Collection): Java automatically removes
unused objects, so developers don’t need to manage memory manually.
2. Writing Your First Java Program
class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!"); // Prints output
}
}
System.out.println() → Prints text on the screen.
main() → The starting point of every Java program.
3. Comments in Java
Used to explain code (ignored by the compiler).
Single-line comment: // This is a comment
Multi-line comment:
/* This is
a multi-line comment */
4. Literals & Variables
Literals → Fixed values ie. Constants(e.g., 10, 3.14, 'A', "Java", true).
Variables → Containers for storing values.
Examples of Variables
int age = 13;
double price = 99.99;
char grade = 'A';
String name = "John";
boolean isStudent = true;
A variable is used to store data. Java has different types of variables:
• int → Stores numbers (e.g., 10, 25).
• float,double → Stores decimal numbers (e.g., 5.6, 9.99).
• char → Stores single characters (e.g., 'A', 'B').
• String → Stores text (e.g., "Hello").
• boolean → Stores true or false.
Example:
class VariablesExample {
public static void main(String[] args) {
int age = 13;
double price = 99.99;
char grade = 'A';
String name = "John";
boolean isStudent = true;
System.out.println("Name: " + name); System.out.println("Age: " + age);
System.out.println("Grade: " + grade); System.out.println("Price: " + price);
System.out.println("Student: " + isStudent);
}}
5. Naming Conventions for Variables & Literals
Use meaningful names: studentName, maxScore
Camel Case: firstName (not FirstName)
Constants in UPPERCASE: final int MAX_SPEED = 120;
Reserved words that cannot be used as variable names
6. Keywords in Java
Reserved words that cannot be used as variable names
Keywords Examples
Data Types int, double, boolean, char, String
Control Flow if, else, switch, for, while
Class & Object class, new, public, private, static
Others return, void, final, break, continue
7. Operators in Java
Types of Operators
Operator Type Examples
Arithmetic +-*/%
Relational > < >= <= == !=
Logical &&
Assignment = += -= *= /= %=
Example Usage
int a = 10, b = 5;
System.out.println(a + b); // 15
System.out.println(a > b); // true
System.out.println(a == b); // false
8. Taking User Input
We use Scanner to take input from the user.
import java.util.Scanner;
class UserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", Age: " + age);
}
}
9. Conditional Statements
Used for decision-making (if-else statements).
if (age >= 18) {
System.out.println("You can vote!");
} else {
System.out.println("You cannot vote yet.");
}
10. Math Functions in Java
Math.min(a, b) → Returns the smaller number
Math.max(a, b) → Returns the larger number
Math.pow(a, b) → Returns a raised to the power of b
Example Usage
class MathFunctions {
public static void main(String[] args) {
System.out.println(Math.min(5, 10)); // 5
System.out.println(Math.max(5, 10)); // 10
System.out.println(Math.pow(2, 3)); // 8.0
}
}
11.Types of Errors in Java
Errors occur when there is a problem in the program. They are classified into three types:
1. Syntax Errors (Compilation Errors)
Occurs when Java code does not follow correct syntax (rules of Java).
Found by the compiler before running the program.
Example:
class Example {
public static void main(String[] args) {
System.out.println("Hello World") // ERROR: Missing semicolon (;)
}
}
Fix: Add a semicolon (;) after "Hello World".
2. Runtime Errors (Exceptions)
Errors that occur while the program is running.
Caused by invalid operations like division by zero or accessing an invalid index.
Example:
class Example {
public static void main(String[] args) {
int num = 10 / 0; // ERROR: Division by zero
System.out.println(num);
}
}
Fix: Check for division by zero before performing the operation.
3. Logical Errors
The program runs without an error, but the output is incorrect.
Hardest to detect because the code looks correct but gives the wrong result.
Example:
class Example {
public static void main(String[] args) {
int num1 = 5, num2 = 10;
int sum = num1 - num2; // ERROR: Should be num1 + num2
System.out.println("Sum: " + sum); // Output will be -5 instead of 15
}
}
Fix: Use correct logic (num1 + num2 instead of num1 - num2).
Program 1:
/* For adding two numbers*/
class AddNumbers {
public static void add(int a, int b)
{
int sum = a + b;
System.out.println("Sum: " + sum);
}
public static void main(String[] args) { add(5, 10);
}}
Program 2:
/* For subtracting two numbers*/
class SubtractNumbers {
public static void diff(int a, int b)
{
int difference = a - b;
System.out.println("difference: " + difference);
}
public static void main(String[] args) {
diff(5, 10); }}
Program 3:
/* For multiplying two numbers*/
class MultiplyNumbers {
public static void prod(int a, int b)
{
int product = a * b;
System.out.println("product: " + product);
}
public static void main(String[] args) {
prod(5, 10);
}}
Program 4:
/* Using Scanner Class */
import java.util.Scanner;
class UserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", Age: " + age);
}
}
Program 5
/* Using Scanner Class and Math functions*/
import java.util.Scanner;
class MathFunctions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter b: ");
int b = sc.nextInt();
System.out.println(Math.min(a, b));
System.out.println(Math.max(a, b));
System.out.println(Math.pow(a, b));
}}