Java Programming Tutorial – Learn Java from Scratch
- Tutorial 1: Introduction to Java
- Tutorial 2: Features of Java
- Tutorial 3: Hello World Program
- Tutorial 4: Java Comments
- Tutorial 5: Variables in Java
- Tutorial 6: DataTypes in Java
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:
- Primitive Data Types
- Non-Primitive Data Types
| Data Type | Size | Example |
|---|---|---|
| byte | 1 byte | 10 |
| short | 2 bytes | 100 |
| int | 4 bytes | 5000 |
| long | 8 bytes | 100000L |
| float | 4 bytes | 10.5f |
| double | 8 bytes | 20.99 |
| char | 2 bytes | ‘A’ |
| boolean | 1 bit | true |
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
intis the most commonly used integer typedoubleis used for decimal valuescharstores single charactersbooleanstores true or false valuesStringis 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);
}
}