Java programs are compiled into bytecode, which can run on any device with a Java Virtual Machine (JVM). This means that a Java program written on a Windows machine can run on a Linux or macOS system without any modifications.
Java is an object - oriented programming (OOP) language. OOP is a programming paradigm based on the concept of “objects”, which can contain data (attributes) and code (methods). Key OOP concepts in Java include classes, objects, inheritance, polymorphism, and encapsulation.
Java has an automatic garbage collector that manages memory. It automatically frees up memory occupied by objects that are no longer in use, reducing the risk of memory leaks.
Visit the Oracle website or an open - source alternative like OpenJDK to download the appropriate JDK for your operating system.
Run the installer and follow the on - screen instructions. Make sure to set the JAVA_HOME
environment variable. On Windows, you can do this through the System Properties -> Advanced -> Environment Variables. On Linux and macOS, you can add the following lines to your .bashrc
or .bash_profile
file:
export JAVA_HOME=/path/to/your/jdk
export PATH=$JAVA_HOME/bin:$PATH
Open a terminal or command prompt and run the following commands:
java -version
javac -version
If the installation is successful, these commands will display the version information of Java and the Java compiler respectively.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
To compile and run this program:
HelloWorld.java
.javac HelloWorld.java
. This will generate a HelloWorld.class
file.java HelloWorld
.public class VariablesExample {
public static void main(String[] args) {
// Integer variable
int num = 10;
// Double variable
double decimal = 3.14;
// String variable
String message = "Hello, Java!";
System.out.println(num);
System.out.println(decimal);
System.out.println(message);
}
}
public class ControlStructures {
public static void main(String[] args) {
int num = 5;
if (num > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is non - positive.");
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
class Rectangle {
int length;
int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int area() {
return length * width;
}
}
public class ClassObjectExample {
public static void main(String[] args) {
Rectangle rect = new Rectangle(5, 3);
System.out.println("Area of the rectangle: " + rect.area());
}
}
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();
}
}
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();
}
}
Use try - catch
blocks to handle exceptions.
public class ErrorHandling {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileReadingExample {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
null
when they are no longer needed to help the garbage collector.Java is a powerful and versatile programming language with a rich set of features. In this guide, we have covered the fundamental concepts, installation and setup, basic syntax, object - oriented programming, common practices, and best practices. By mastering these concepts, you will be well on your way to becoming a proficient Java developer. Remember to practice regularly and explore more advanced topics to deepen your understanding of Java.