Here is a simple example of a Java program that outputs the message "Hello, world!" to the console:
refer tttual:ouri.compublic class Main { public static void main(String[] args) { System.out.println("Hello, world!"); } }
To run this program, you will need to save it to a file with the name Main.java
and compile it using the javac
command. Then, you can run the program using the java
command.
For example, on the command line, you can use the following commands to compile and run the program:
javac Main.java java Main
This will output the message "Hello, world!" to the console.
Here is another example of a Java program that reads an integer from the user, calculates its square, and outputs the result:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int n = scanner.nextInt(); int squared = n * n; System.out.println("The square of " + n + " is " + squared); scanner.close(); } }
In this example, the Scanner
class is used to read an integer from the user, and the nextInt
method is used to parse the input. The square of the integer is then calculated and output to the console using the println
method.