Lesson Objective
- Learn how to display information on the screen and understand what a variable is.
- Understand some basic Java syntax.
KS3, GCSE, A-Level Computing Resources
Every line of code that runs in Java must be inside a class. In the example above, we named the class Main. A class should always start with an uppercase first letter.
public class Main { public static void main(String args[]){ System.out.println("Hello World, welcome to your first Java lesson."); } }
The main() method is used in every Java program. Any code inside the main() method will be executed. Don't worry about the keywords before and after main. You will get to know them bit by bit while reading this tutorial. For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.
To display information to the user, you must use the "System.out.println" method.
public class Hello_World_1 { public static void main(String args[]){ //method header System.out.println("Hello World, welcome to your first Java lesson."); //method body } }
The "System.out.println" method is used to create new lines in the display.
public class Hello_World_1 { public static void main(String args[]){ //method header System.out.println("Hello World, welcome to your first Java lesson."); //method body System.out.println("I'm on the next line."); } }