In This Article, You Will Learn about Java Print Variables.
Java Print Variables – Before moving ahead, let’s know a bit about Java Variables.
Table of Contents
Display Variables
In Java, println() method is always used for displaying outputs or variables.
To print the variable and text together, use the + operator.
Example: Use + operator to combine both variable and text.
public class Main{
public static void main(String[] args){
System.out.print("Hello Java" + "World");
}
}
Java also allows us to use + operator as to add one variable to another variable.
Example: Use + operator to add one variable to another variable.
public class Main{
public static void main(String[] args){
String firstName = "Hello Java";
String secondName = "World";
System.out.print(firstName + secondName);
}
}
For numerical values, + operator works as same as mathematical operator.
Example: Use + operator to add two numerical values.
public class Main{
public static void main(String[] args){
int firstvalue = 10;
int secondvalue = 20;
System.out.print(firstvalue + secondvalue);
}
}
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: