Java Tutorial

Tutorial 1: Introduction to Java

Java programming language was originally developed by Sun Microsystems, which was initiated by James Gosling and released in 1995 as a core component of Sun Microsystems’ Java platform (Java 1.0 [J2SE]).
Object-Oriented Programming (OOP): Java supports OOP concepts to create modular and reusable code.

Platform Independence: Java programs can run on any operating system with a JVM.

Robust and Secure: Java ensures reliability and security through strong memory management and exception handling.

Multithreading and Concurrency: Java allows concurrent execution of multiple tasks for efficiency.

Rich API and Standard Libraries: Java provides extensive built-in libraries for various programming needs.

Frameworks for Enterprise and Web Development: Java supports frameworks that simplify enterprise and web application development.

Open-Source Libraries: Java has a wide range of libraries to extend functionality and speed up development.

Maintainability and Scalability: Java’s structured design allows easy maintenance and growth of applications.

A variable in Java is a container used to store data values.
It acts as a named memory location whose value can change during program execution.

Example:
If you store age = 25, then age is a variable.

Syntax of Variable

dataType variableName = value;

 Example:

int age = 25;
Rules for Naming Variables
  • Must start with a letter, _, or $
  • Cannot start with a number
  • Cannot use Java keywords (like int, class)
  • Case-sensitive (ageAge)
public class VariableExample {
    public static void main(String[] args) {
        int age = 25;
        String name = "Kapoor";
        double salary = 50000.50;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
    }
}
Important Points
  • Variables must be declared before use
  • Java is a strongly typed language (data type is mandatory)
  • The value of a variable can be changed during execution
//Demonstrates storing different types of data.

int age = 25;
double price = 99.99;
char grade = 'A';
boolean isActive = true;

System.out.println(age);
System.out.println(price);
System.out.println(grade);
System.out.println(isActive);
//Real-Life Example

public class Employee {
    public static void main(String[] args) {
        String name = "Kapoor";
        int id = 101;
        double salary = 45000;

        System.out.println("Employee Name: " + name);
        System.out.println("Employee ID: " + id);
        System.out.println("Salary: " + salary);
    }
}

A data type in Java defines:

  • What type of data a variable can store
  • How much memory will it use
  • What operations can be performed on it

👉 Example:

int age = 25;

Here, int is the data type that tells Java that age will store integer values.

Data TypeSizeDescription
byte1 byteSmall integer (-128 to 127)
short2 bytesMedium integer
int4 bytesDefault integer type
long8 bytesLarge integer
float4 bytesDecimal number
double8 bytesHigh precision decimal
char2 bytesSingle character
boolean1 bittrue/false
public class datatype1{
    public static void main(String[] args) {
        int age = 25;
        double salary = 50000.75;
        char grade = 'A';
        boolean isActive = true;

        System.out.println(age);
        System.out.println(salary);
        System.out.println(grade);
        System.out.println(isActive);
    }
}
public class Student {
    public static void main(String[] args) {
        int id = 101;
        String name = "Rahul";
        double marks = 85.5;
        boolean passed = true;

        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Marks: " + marks);
        System.out.println("Passed: " + passed);
    }
}
The if Statement

The if statement in Java checks a Boolean expression and executes a specific block of code only if the condition is true.

The if-else Statement

The if-else statement allows Java programs to handle both true and false conditions. If the condition inside the if statement evaluates to false, the else block is executed instead.

Using if-else statements in Java improves decision-making in programs by executing

Most often, conditions are created using comparison operators, like the ones below:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to: a == b
  • Not equal to: a != b
if (condition) {
  // block of code to be executed if the condition is true
}
if (20 > 18) {
  System.out.println("20 is greater than 18");
}