What types of data are there and what are they?
Data Types are particular sets of data that can only do certain operations, or can only take certain types of data. Some data types include int,boolean,float, long, double, and char. In This lesson we will learn more about each type of data
How to name data types
When naming data types the names for the data should always start with lowercase letters. Names for data types should always be useful to the context of the code.
Integer
An integer is one type of data that is only whole numbers. To write an integer in code, just use “int” and state the variable name. An integer can be assigned as null(meaning it has no value attached to it).
public class integerExample {
public static void main(String[] args) {
int variableName = 0;
}
}
Float
Floats are usually used when there are fractions or decimals in play, such as 3.1415 or 2.718. They use 4 bytes.
public class floatExample {
public static void main(String[] args) {
float floatName = 0.00f;
}
}
Double
A double is much like a float but contains 32 bytes. This is what we usually want to use when dealing with decimals in Java.
public class doubleExample {
public static void main(String[] args) {
double variableName = 0.00;
}
}
Long
A long is just like a double or a float but it uses 64 bits. A long can be declared as null(meaning it has no value attached to it)
public class longExample {
public static void main(String[] args) {
long variableName = 0.00L;
}
}
Boolean
A boolean is a data type that can be turned on and off to represent true or false. We declare this variable by stating it “boolean”, for example...
public class booleanExample {
public static void main(String[] args) {
boolean variableName = true; //or false
}
}
Char
A char is a data type that is characterized by being a letter, or symbol. These letters can only be inputted one at a time.
public class charExample {
public static void main(String[] args) {
char character = 'A';
}
}
How to print information
We can print out data by using this command…
public static void main(String[] args){
int number = 5;
System.out.println(number);//This will print out the number '5'
}
We can also add this to our regular System.out.printn command...
public static void main(String[] args){
int number = 5;
System.out.println("The number is " + number);
//this will print out 'The number is 5'
}