Java Crash Course
Java Crash Course
TO JAVA
WHAT IS JAVA?
Java is a high-level, object-oriented
programming language.
Declaration Syntax:
dataType variableName = value;
RULES FOR NAMING
VARIABLES:
Can start with a letter, $, or _ (but not a
number).
Cannot use Java keywords (e.g., int, class).
Uses camelCase naming convention (e.g.,
studentAge).
TYPES OF VARIABLES IN JAVA
Local Variables: Declared inside methods,
accessible only within that method.
Instance Variables: Belong to an object,
declared inside a class but outside
methods.
Static Variables: Declared with static,
shared across all instances of a class.
TYPES OF VARIABLES IN JAVA
Example:
class Student {
String name; // Instance variable
static String school = "ABC School"; // Static variable
void show() {
int age = 20; // Local variable
System.out.println(name + " studies at " + school);
}
}
DATA TYPES IN JAVA
✔ Java has two categories of data types:
Primitive Data Types (stores simple values)
Non-Primitive Data Types (stores objects and
complex structures)
DATA TYPES IN JAVA
DATA TYPES IN JAVA
Important Points About Data Types
✔ By default, decimal values are stored as double.
✔ To store a float value, add f or F at the end
(float x = 5.6f;).
✔ Integer values are stored as int by default.
✔ long values must have an L at the end (long
num = 100000L;).
✔ boolean can only be true or false (not 0 or 1).
DATA TYPES IN JAVA
Non-Primitive Data Types
✔ Non-primitive types are user-defined or Java-
built objects.
✔ Examples: Strings, Arrays, Classes, Interfaces,
etc.
Example:
String name = "Java";
int[] numbers = {10, 20, 30}; // Array
OPERATORS IN JAVA
Operators are used to perform operations on
variables and values.
INTRODUCTION TO
CONTROL FLOW
STATEMENTS
CONTROL FLOW STATEMENT
✔ Control flow statements determine how Java
programs execute instructions.
Syntax:
dataType [ ] var_name = new dataType[number];
DIFFERENT ARRAYS METHOD
ITERATING OVER ARRAYS
(FOR-EACH LOOP)
Enhanced for-loop simplifies array traversal.
Syntax:
for (dataType var_name : array_name) {
//perform desired operation
}
MULTI-DIMENSIONAL ARRAYS
2D Arrays (Matrix Representation)
Syntax:
returnType methodName(dataType...variableName) {
// Method body
}
Key Points:
(1) int... numbers allows multiple arguments of type int.
(2) Inside the method, numbers behaves like an array.
RECURSION IN JAVA
Recursion is a technique where a method calls itself until a
base condition is met. It is useful for problems like factorial,
Fibonacci series, and tree traversal.
Key Points:
(1) Every recursive function must have a base case to stop
recursion.
(2) Recursion is useful but can cause stack overflow if not
handled correctly.
RECURSION IN JAVA
RECURSION IN JAVA
INTRODUCTION
TO OOP
INTRODUCTION TO OOP
Object-Oriented Programming (OOP) is a
programming paradigm that organizes code using
objects and classes. Java follows the OOP
approach, which helps in modularity, reusability,
and maintainability of code.
CLASSES AND OBJECTS
What is a Class?
A class is a blueprint for creating objects. It defines
attributes (variables) and behaviors (methods)
that objects of that class will have.
What is an Object?
An object is an instance of a class. It represents a
real-world entity with properties and behaviors.
CONSTRUCTORS
A constructor is a special method used to initialize
objects. It has the same name as the class and is
automatically called when an object is created.
Types of Constructors:
class Example {
public int publicVar = 10;
private int privateVar = 20;
protected int protectedVar = 30;
int defaultVar = 40; // Default access
void display() {
System.out.println("Private Variable: " + privateVar);
}
}
ACCESS MODIFIERS
public class Main {
public static void main(String[] args) {
Example obj = new Example();
System.out.println("Public Variable: " + obj.publicVar);
// System.out.println(obj.privateVar); // Error: privateVar
is private
System.out.println("Protected Variable: " +
obj.protectedVar);
System.out.println("Default Variable: " + obj.defaultVar);
}
}
GETTERS AND SETTERS
Since private variables cannot be accessed directly
outside the class, we use getter and setter methods to
access and modify them.
INHERITANCE
Inheritance allows a class to acquire the properties
and behaviors of another class. The existing class is
called the parent (superclass), and the new class is
called the child (subclass).
Example of Inheritance:
class Animal {
void sound() {
System.out.println("Animals make sounds");
}
}
INHERITANCE
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Example of Encapsulation:
class BankAccount {
private double balance;
// Setter method
void setBalance(double amount) {
if (amount > 0)
balance = amount;
}
ENCAPSULATION
// Getter method
double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.setBalance(1000);
System.out.println("Account Balance: " + acc.getBalance());
}
}
Output:
Account Balance: 1000.0
ABSTRACT CLASS
An abstract class is a class that cannot be instantiated and may contain both abstract
(unimplemented) and concrete (implemented) methods.
interface Animal {
// Abstract method (public & abstract by default)
void makeSound();
// Default method (introduced in Java 8)
default void sleep() {
System.out.println("Animal is sleeping.");
}
}
INTERFACE IN JAVA
class Dog implements Animal {
// Implementing abstract method
@Override
public void makeSound() {
System.out.println("Dog barks.");
}
} public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Output: Dog barks.
dog.sleep(); // Output: Animal is sleeping.
}
}
EXCEPTION
HANDLING IN JAVA
EXCEPTION HANDLING IN JAVA
Exception Handling in Java is a mechanism to handle runtime errors and
prevent program crashes. It allows the program to continue execution
smoothly.
Thread Lifecycle
1. New – Thread created but not started.
2. Runnable – Ready to run but waiting for CPU allocation.
3. Running – Thread is executing.
4. Blocked/Waiting – Waiting for another thread to release resources.
5. Terminated – Thread has finished execution.
CREATING A THREAD IN
JAVA
Method 1: Extending Thread Class
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start(); // Start the thread
}
}
Output: Thread is running...
PACKAGES IN
JAVA
PACKAGES IN
JAVA
What is a Package in Java?
A package in Java is a way to group related classes,
interfaces, and sub-packages together. It helps in:
✔ Organizing large projects.
✔ Avoiding class name conflicts.
✔ Controlling access using access modifiers.