Comprehensive Java Tutorial
Beginner to Advanced Level with Syntax, Explanations, and Examples
Table of Contents
1. Introduction to Java
2. Java Basics
3. Operators in Java
4. Java Packages
5. Math Functions in Java
6. Control Statements
7. Methods in Java
8. Object-Oriented Programming (OOPs) Concepts
9. Sorting in Java (Bubble Sort)
10. Summary & Practice Exercises
1. Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun
Microsystems (now owned by Oracle).
Features of Java include: Platform Independent, Object-Oriented, Robust, Secure,
Multithreaded, Portable.
JVM (Java Virtual Machine): Executes Java bytecode; JRE (Java Runtime Environment):
Provides libraries & JVM; JDK (Java Development Kit): Includes compiler + JRE.
Example program: public class HelloWorld { public static void main(String[] args) {
[Link]("Hello, World!"); } }
2. Java Basics
Variables: Named memory locations; Identifiers: names for variables, classes, methods.
Keywords: reserved words like class, public, static.
Data Types: int, float, double, char, boolean, String, Arrays (1D, 2D, Jagged).
Casting: Implicit (widening) and Explicit (narrowing).
3. Operators in Java
Arithmetic (+, -, *, /, %).
Relational (==, !=, >, <, >=, <=).
Logical (&&, ||, !).
Assignment (=, +=, -=, *=, /=).
Unary (++ , --).
Bitwise (&, |, ^, ~, <<, >>, >>>).
Ternary (?:).
Precedence: Unary > Multiplicative > Additive > Relational > Equality > Logical >
Assignment.
4. Java Packages
[Link]: String, Object, Math, Wrapper classes.
[Link]: Scanner, Collections, ArrayList, HashMap.
Importing: import [Link];
Custom packages can be created with 'package' keyword.
5. Math Functions in Java
abs(int/double): returns absolute value.
max(a,b), min(a,b).
sqrt(double), pow(a,b).
ceil(x), floor(x), round(x).
random(): returns double between 0.0 and 1.0.
Each function belongs to [Link].
6. Control Statements
Decision-making: if, if-else, nested if, switch.
Loops: for, while, do-while, enhanced for.
Jump: break, continue, return.
Example: for(int i=0;i<5;i++){ [Link](i); }
7. Methods in Java
Definition: block of code that performs a task.
Declaration: returnType methodName(params).
Actual vs Formal arguments.
Pure function: no side-effects, same output for same input.
Impure function: may have side effects (like modifying global state).
8. Object-Oriented Programming (OOPs) Concepts
Class: blueprint of objects.
Object: instance of a class.
Constructor: initializes object; types: default, parameterized, copy.
Encapsulation: wrapping data + methods (using private variables + getters/setters).
Inheritance: code reuse (extends keyword).
Polymorphism: compile-time (method overloading), runtime (method overriding).
Abstraction: hiding details (abstract classes, interfaces).
Enum: fixed set of constants.
9. Sorting in Java (Bubble Sort)
Bubble Sort compares adjacent elements and swaps them if out of order.
Code: int arr[]={5,3,1,4,2}; for(int i=0;iarr[j+1]){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp;
}}}
10. Summary & Practice Exercises
Summary: Java basics, operators, packages, OOPs, and sorting.
Practice: Write a program to find factorial using recursion.
Practice: Create a class Car with attributes and methods, demonstrate encapsulation.
Practice: Implement Bubble Sort on user input array.