Lesson 1 of 10

Your first program

Goal

The main method, println and a static method

01

Explanation

In Java everything lives inside a class. The program here is a class called Main, and it starts running from the method public static void main(String[] args), a long line every Java programmer ends up typing more times than they can count.

System.out.println prints text and then moves to a new line, and the + sign joins strings to each other or to numbers, as in "Level " + level. Every statement in Java ends with a semicolon.

Every method also states the type of value it returns before its name, so static int total(int a, int b) takes two integers and returns an integer. Java is a compiled language: the compiler checks all of your code before it runs, and if it finds even one type error, it refuses to run the program and tells you which line the error is on.

02

Your task

Complete total(a, b) so it returns the sum of the two numbers, then print a single line from main: Total: 12. The number should come from total(5, 7) rather than being typed in by hand. Click “Run and check” to see the result.

Checks

3 checks
  1. total(5, 7) returns 12
  2. total adds negative numbers too
  3. "Total: 12" was printed
03

Hints

0/3
Track lessons0 of 10 done

Java is the language behind Android apps and large systems. Everything in it lives inside a class, and you state every variable's type explicitly.

Beginner level, 10 lessons, 2 hours in total.

Main.java
OutputJava 13 (Judge0)

Compiling and running your code on the server.

–/3

Compiling and running your code on the server.

3 checks listed under “Your task”.

Track lessons0 of 10 done

Java is the language behind Android apps and large systems. Everything in it lives inside a class, and you state every variable's type explicitly.

Beginner level, 10 lessons, 2 hours in total.