Datatypes #
We will be exploring different datatypes
- Integer - int
- Double - double
- Boolean - boolean
- String - String
Integer #
- Any whole number
Double #
- Any number with a decimal
Boolean #
- True or False
String #
- Any letters, numbers, and characters
- Quotation marks are needed around the value
Integer #
Integers are usually defined as so,
int x = 12;
This is showing that the value of zero is being assigned to the integer variable called x.
ints can only store integers.
Double #
Doubles are usually defined as so,
double y = 2.1;
This is showing that the value of 2.1 is being assigned to the double called y.
doubles can store both integers, and numbers with decimals, but it will convert an integer to a double
Booleans #
Booleans are usually defined as so,
boolean z = true;
Booleans can only equal to true or false.
Strings #
Strings are basically words that can be stored as a variable. To define one, you would write,
String a = "You would need to use quotes to write a string";
Strings can contain words and numbers.
Extra #
You can print out a variable too.
System.out.println(x);
This statement would print out the value of x, which is 0
System.out.println(y);
If you replaced the x with y, it would now print out the value 2.1
System.out.println(z);
If you then replaced the y with z, it would then print out the value true
System.out.println(a);
And finally, if you were to replace the z with a, then it would print out, "You would need to use quotes to write a string"
, since that is the sentence defined in the string a