Java Tutorial


Tutorial 1: Introduction to Java

Java is a high-level, object-oriented programming language developed by Sun Microsystems. It was initiated by James Gosling and officially released in 1995 as a core component of the Java platform.

Java is widely used for developing:

  • Desktop applications
  • Web applications
  • Mobile applications
  • Enterprise software
  • Cloud-based systems

Tutorial 2: Features of Java

  • Object-Oriented Programming (OOP)
  • Platform Independence
  • Robust and Secure
  • Multithreading and Concurrency
  • Rich API and Standard Libraries
  • Framework Support
  • Open-Source Libraries
  • Maintainability and Scalability
  • High Performance

Tutorial 3: Hello World Program

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Tutorial 4: Java Comments

Comments can be used to explain Java code and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line comments

public class Main {
  public static void main(String[] args) {
    // This is a comment - Single-line Comments
    System.out.println("Hello World");
  }
}

Multi-line comments

public class Main {
  public static void main(String[] args) {
    /* 
    Java Multi-line Comments
    System.out.println("Hello 1");
    System.out.println("Hello 2");
    
    */
    System.out.println("Hello World");
  }
}

Tutorial 5: Variables in Java

Variables are used to store data in a program.

In Java, a variable is a container that holds values during program execution.

dataType variableName = value;

int age = 25;
String name = "John";
double salary = 45000.50;

Rules for Naming Variables

  • Variable names should start with a letter, $, or _
  • Cannot start with a number
  • Cannot use Java keywords
  • Use meaningful names
public class VariablesDemo {

    public static void main(String[] args) {

        int age = 25;
        String name = "Kapoor";

        System.out.println(age);
        System.out.println(name);
    }
}

Tutorial 6: Data Types in Java

Data types define the type of data a variable can store in Java.

Types of Data Types in Java

Java data types are divided into:

  1. Primitive Data Types
  2. Non-Primitive Data Types
Data TypeSizeExample
byte1 byte10
short2 bytes100
int4 bytes5000
long8 bytes100000L
float4 bytes10.5f
double8 bytes20.99
char2 bytes‘A’
boolean1 bittrue
int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean status = true;

2. Non-Primitive Data Types

Non-primitive data types store references to objects.

Examples:

  • String
  • Array
  • Class
  • Interface
String name = "Java";
int[] numbers = {1, 2, 3};

Important Points

  • int is the most commonly used integer type
  • double is used for decimal values
  • char stores single characters
  • boolean stores true or false values
  • String is widely used for text data
public class DataTypesDemo {

    public static void main(String[] args) {

        int age = 25;
        double salary = 55000.75;
        char grade = 'A';
        boolean passed = true;
        String name = "Kapoor";

        System.out.println(age);
        System.out.println(salary);
        System.out.println(grade);
        System.out.println(passed);
        System.out.println(name);
    }
}