Display Variables
The println()
method is recurrently used to display variables.
To combine both text and a variable, use the +
character:
Example
public class Main {
public static void main(String[] args) {
String name = "Sowmii";
System.out.println("Hello " + name);
}
}
Output=Hello Sowmii
You can also use the +
character to add a variable to another variable:
Example
public class Main {
public static void main(String[] args) {
String name1 = "Welcome";
String name2 = "to techy sowmii";
String name = name1 + name2;
System.out.println(name);
}
}
Output= Welcome to techy sowmii
For numbers, the
+
character works as a mathematical operatorExample
public class Main {
public static void main(String[] args) {
int g = 5;
int q = 6;
System.out.println(g + q); // Print the value of g + q
}
}
Output=11
From the above example, you can sense that
- g stores the value 5
- q stores the value 6
- Then we use the println method to display the value of g+ q, which is 11
No comments:
Post a Comment
Did you find this article helpful?
😁 😀