Java Hello, World!
Hello, Java!
Iniciando com a linguagem Java!
Instalação
Linux
sudo apt update && \
sudo apt upgrade && \
sudo apt install default-jre &&\
sudo apt install default-jdk
Windows
-
Denfinir alias no
.bashrc
alias java="/c/'Program Files'/java/jdk-16.0.2/bin/java.exe"
Código
import java.util.Scanner;
public class Hello {
static String userName = "";
// Execute on file call
public static void main(String[] args) {
getName();
toGreet();
}
public static void getName() {
// Create a Scanner Object
Scanner sc = new Scanner(System.in);
System.out.println("Enter username: ");
// Read user input
String userName = sc.next();
sc.close();
}
public static void toGreet() {
System.out.println(String.format("Hello, World! %s", userName));
}
}
Execução
Compilar:
javac Hello.java
Executar:
java Hello.java