The Java Development Kit (JDK) is essential for writing and running Java programs. You can download the latest version of the JDK from the official Oracle website or use an open - source alternative like OpenJDK.
After installation, you need to set up the JAVA_HOME
, PATH
, and CLASSPATH
environment variables. On Windows, you can do this through the System Properties. On Linux or macOS, you can add the following lines to your .bashrc
or .zshrc
file:
export JAVA_HOME=/path/to/your/jdk
export PATH=$PATH:$JAVA_HOME/bin
Open a terminal or command prompt and type the following commands:
java -version
javac -version
If the installation is successful, you should see the version numbers of Java and the Java compiler.
Here is a simple “Hello, World!” program in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class HelloWorld
: Defines a public class named HelloWorld
. In Java, every program must have at least one class.public static void main(String[] args)
: The main
method is the entry point of a Java program. It is declared as public
(accessible from anywhere), static
(can be called without creating an instance of the class), and void
(does not return a value).System.out.println("Hello, World!");
: Prints the string “Hello, World!” to the console.To compile and run the program, save the code in a file named HelloWorld.java
and run the following commands in the terminal:
javac HelloWorld.java
java HelloWorld
Java has several primitive data types, including:
byte
: 8 - bit signed integershort
: 16 - bit signed integerint
: 32 - bit signed integerlong
: 64 - bit signed integerfloat
: 32 - bit floating - point numberdouble
: 64 - bit floating - point numberchar
: 16 - bit Unicode characterboolean
: Represents true or falseHere is an example of declaring and initializing variables:
public class VariableExample {
public static void main(String[] args) {
int age = 25;
double height = 1.75;
char grade = 'A';
boolean isStudent = true;
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Grade: " + grade);
System.out.println("Is Student: " + isStudent);
}
}
Reference data types are used to refer to objects. Examples include String
, arrays, and user - defined classes.
public class ReferenceExample {
public static void main(String[] args) {
String name = "John";
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Name: " + name);
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
public class IfElseExample {
public static void main(String[] args) {
int score = 80;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D");
}
}
}
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
public class WhileLoopExample {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}
A class is a blueprint for creating objects. Here is an example of a simple Person
class:
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("My name is " + name + " and I am " + age + " years old.");
}
}
public class ObjectExample {
public static void main(String[] args) {
Person person = new Person("Alice", 20);
person.introduce();
}
}
class Person
: Defines a class named Person
.String name; int age;
: Declares two instance variables.public Person(String name, int age)
: A constructor that initializes the instance variables.public void introduce()
: A method that prints an introduction.Inheritance allows a class to inherit the properties and methods of another class.
class Animal {
public void eat() {
System.out.println("The animal is eating.");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("The dog is barking.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
Polymorphism allows objects of different classes to be treated as objects of a common superclass.
class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
Java uses try - catch blocks to handle exceptions.
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
The try
block contains the code that might throw an exception. If an exception occurs, the control is transferred to the appropriate catch
block.
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
scanner.close();
}
}
The Scanner
class is used to read user input from the console.
import java.io.FileWriter;
import java.io.IOException;
public class OutputExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("This is a test.");
writer.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
The FileWriter
class is used to write data to a file.
In this tutorial, we have covered the fundamental concepts of Java, including setting up the environment, basic syntax, variables, control structures, object - oriented programming, exception handling, and input/output operations. We also discussed common practices and best practices in Java programming. By mastering these concepts, beginners can start writing their own Java programs and continue to explore more advanced topics.