Variables & Data Types
A variable has a name, a value and a data type. This chart shows all the data types.
“int” is used for store integer and most commonly used in programming.
“float” and “double” are for storing real numbers which have a fractional part (i.e decimal part). Double can represent for bits than float.
A boolean data type can hold only two values: true & false. Here is an example:
boolean a = true; boolean b = false;
A char data type is used to store a single character, such as ‘a’, ‘Z’, ‘9’, ‘&’, ‘$’, etc. The character can be letters, digits, special symbols, and even non graphic symbol(tab). If we want to store the value ‘A’ in a variable x:
char x = 'A';
I input my name “Iris” in variable name which is in String class. String is a class instead of a primitive data type. We can have text enclosed in double quotes as the object of String class.
Print Methods
This program used the same technique with the previous HelloWorld one. But this time, I used “println()” instead of “printf()”. What is the difference between “print()”, “println()”, and “printf()”?
The only difference between the print() and println() methods is that the println() statement positions the cursor onto the next line after printing the desired text while the print() method leaves the cursor on the same line. For examples:
- If we use print(),
System.out.print("Hello "); System.out.print("World ");
- The output would be
Hello World
- If we use println(),
System.out.println("Hello"); System.out.println("World");
- The output would be
Hello World
Let’s have a look of a comparison between println() and printf() first:
System.out.println ("Welcome to "+str); System.out.printf ("Welcome to %s", str);
The printf() statement is used for displaying text that needs to be formatted. So that we can add formate specifiers in printf().
If
Switch
Scanner
Java Exercises
OddEven
PrintDayInWord
KeyboardScanner
BMI
Works Cited
“Displaying Text Using Printf() Method – Java Tutorial – Java With Us”. Javawithus.com. N.p., 2016. Web. 28 Nov. 2016.
“Displaying Text Using Printf() Method – Java Tutorial – Java With Us”. Javawithus.com. N.p., 2016. Web. 28 Nov. 2016.