Java - First Program

Your First Java Program

Create Hello.java:

public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello, Java!");
  }
}

Compile and run:

javac Hello.java
java Hello

Project Structure (Maven)

demo/
  src/
    main/java/com/example/App.java
    test/java/com/example/AppTest.java
  pom.xml

Project Structure (Gradle)

demo/
  src/
    main/java/com/example/App.java
    test/java/com/example/AppTest.java
  build.gradle
  settings.gradle

Try it

  1. Print the sum of two numbers passed via args (convert with Integer.parseInt).
  2. Change the package to com.example, move the file into folders, and compile with javac com/example/Hello.java.
  3. Create a Gradle/Maven project and run the same Hello class from the build tool.