Java Basics
Java Basics
1.Features of Java
1. Platform Independent: Java programs run on any operating system (Windows, Mac,
Linux).Uses JVM (Java Virtual Machine) to make it independent.
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).
class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!"); // Prints output
}
}
3. Comments in Java
/* 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
Example:
class VariablesExample {
}}
6. Keywords in Java
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
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
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
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
}
}
Errors occur when there is a problem in the program. They are classified into three types:
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)
Example:
class Example {
public static void main(String[] args) {
int num = 10 / 0; // ERROR: Division by zero
System.out.println(num);
}
}
3. Logical Errors
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));
}}