Table of contents | |
Introduction | |
Installing Java on Windows | |
Installing Java on macOS | |
Installing Java on Linux | |
Verifying the Java Installation |
Java is a popular programming language used for developing a wide range of applications. Setting up Java on your computer is the first step towards writing and running Java programs. This article will guide you through the process of setting up Java on different operating systems and provide examples and explanations along the way.
To install Java on a Windows machine, follow these steps:
To install Java on a macOS system, follow these steps:
To install Java on a Linux system, follow these steps:
This command will display the installed Java version.
1. Create a Java file: Open a text editor and create a file named "HelloWorld.java" with the following contents:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Compile and Run: Open a terminal or command prompt, navigate to the directory where you saved the "HelloWorld.java" file, and execute the following commands:
javac HelloWorld.java
java HelloWorld
If everything is set up correctly, you should see the output: "Hello, World!".
1. Problem: Write a Java program to calculate the sum of two numbers and display the result.
public class SumCalculator {
public static void main(String[] args) {
int number1 = 10;
int number2 = 20;
int sum = number1 + number2;
System.out.println("The sum is: " + sum);
}
}
Output
The sum is: 30
2. Problem: Write a Java program to calculate the area of a rectangle.
public class RectangleAreaCalculator {
public static void main(String[] args) {
int length = 5;
int width = 10;
int area = length * width;
System.out.println("The area of the rectangle is: " + area);
}
}
Output
The area of the rectangle is: 50
60 videos|37 docs|12 tests
|
|
Explore Courses for EmSAT Achieve exam
|