Strings
Store text in a String and print it.
1public static void main(String[] args) {
2 String hello = "Hello World!";
3 System.out.println(hello);
4}
$ javac Main.java
$ java Main
Hello World!
Every line, explained
public static void main(String[] args) {- Every Java program starts at the main method. When you run the program, Java looks for this exact line, public static void main(String[] args), and runs everything between its { and } from top to bottom.
String hello = "Hello World!";- String declares a variable that stores text. In Java, text always goes inside double quotes: "like this". Single quotes are only for one single character (like 'A'), so writing 'Hello' would be an error. Also notice that String starts with a capital S.
System.out.println(hello);- System.out.println(...) prints whatever is inside the parentheses to the terminal, then moves to a new line. Read the name carefully: print-l-n is short for "print line".
}- This closing curly brace } ends the main method. In Java every opening brace { must have a matching closing brace }.