How to Print in Java?
Print numbers or text in Java
In Java, when we write programs, we often want to display information on the screen for the user to see.
To do this, we use the System.out.println(); statement.
For example, if we want to display a number, like 5, on the screen, we can write:
Syntax-
System.out.println(5);
This will display the number 5 on the screen when we run the program. Similarly, if we want to display a message, like "Hello, World!", we can write:
Syntax-
System.out.println(“Hello, World!”);
This will print the message "Hello, World!" on the screen.
So, the System.out.println(); statement is like a way to show things on the screen when we run our Java programs.
class Main {
public static void main(String[] args) {
System.out.println(33);
System.out.println(“Java is easy”);
}
}
Output -
33
Java is easy
Print Variables
In Java, we can use the System.out.println(); statement to print not only text or numbers but also the value stored in variables.
Syntax -
System.out.println(variableName);
where variableName is the name of the variable.
Let's consider an example:
class Main {
public static void main(String[] args) {
// create a variable
int num = 19;
// print the num variable
System.out.println(num);
}
}
This code will output 19 because the variable num stores the value 19. So when we use System.out.println(num);, it prints the value stored in the variable num.
Now, if you're wondering why System.out.println(num); prints 19 instead of "num", the next example will help clarify things:
class Main {
public static void main(String[] args) {
// create a variable
int num = 19;
System.out.println(num); // print the num variable
System.out.println(“num”); // print “num”
}
}
Output -
19
num
In this case, System.out.println(num); prints the value stored in the variable num, which is 19. On the other hand, System.out.println("num"); prints the text "num" because it's wrapped in quotation marks, indicating it's a string.
Remember, for text, we always use quotation marks, but for variables, we don't need them.
Common Mistakes
-
When we write text in Java, we need to put it inside quotation marks to indicate that it's a piece of text.
For example, System.out.println(Java is easy.);
Here, the text "Java is easy." is not enclosed in quotation marks. Java then interprets it as something else, like a variable or a keyword, rather than as a piece of text. This causes an error because Java is expecting it to be inside quotation marks, like System.out.println("Java is easy.");. It's similar to how we put a sentence in quotes when we write it on paper.
-
In Java, we use a semicolon at the end of each statement to indicate that the statement has ended.
For instance, System.out.println("Hey") , the program expects a semicolon at the end, like System.out.println("Hey");
Forgetting the semicolon gives rise to an error because the program doesn't understand where the statement ends. It's similar to forgetting to use a full stop at the end of a sentence, making it difficult to know when one sentence ends and another begins.