Java Data Types
As we know that a variable in Java must be a specified data type:
Data types are divided into two groups:
Primitive data types - byte, short, int, long, float, double, boolean and char
Non-primitive data types - String, Arrays, Interfaces and Classes
Different data types
Java contains the following different types of data types.
byte (1 byte): Contain numbers from -128 to 127
short (2 bytes): Contain numbers from -32,768 to 32,767
int (4 bytes): Contain numbers from -2,147,483,648 to 2,147,483,647
long (8 bytes): Contain numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float (4 bytes): Contain fractional numbers. Sufficient for storing 6 to 7 decimal digits
double (8 bytes): Stores fractional numbers. Sufficient for storing 15 decimal digits
Boolean (1 bit): Stores true or false values
char (2 bytes): Stores a single character/letter or ASCII values
Numbers (integer & float)
Primitive number types are divided into two groups:
i. Integer types
ii. Floating point types (and double)
Integer types
Contain positive or negative number, without decimals.
Valid types are byte, short, int and long that is use depend on the numeric value.
Example:
byte myNum1 = 100;
short myNum2 = 5000;
int myNum3 = 100000;
long myNum4 = 15000000000L;
Floating point types:
It represents numbers with a fractional part, containing one or more decimals.
There are two types: float and double.
Example :
float myNum5 = 5.75f;
double myNum6 = 19.99d;
System.out.println(myNum6);
Float and double
The precision of a floating point value indicates how many digits the value can have after the decimal point.
The precision of float is only six or seven decimal digits.
Double variables have a precision of about 15 digits.
Therefore it is safer to use double for most calculations.
Boolean Types
Sometimes we need a data type that can only have one of two values, like:
YES / NO
ON / OFF
TRUE / FALSE
For this, Java has a Boolean data type, which can only take the values true or false:
Example:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun);
Note: Boolean values are mostly used for conditional testing.
Java Characters
Characters (char) data type is used to store a single character.
The character must be surrounded by single quotes, like 'A' or 'c':
Example:
char myGrade = 'B';
System.out.println(myGrade);
Alternatively, if you are familiar with ASCII values, you can use those to display certain characters:
Example:
char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1);
Strings
The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:
Example:
String greeting = "Hello World";
System.out.println(greeting);
The String type is so much used and integrated in Java, that some call it "the special ninth type".
Actually a String in Java is a non-primitive data type, because it refers to an object.
The String object has methods that are used to perform certain operations on strings.
Don't worry if you don't understand the term "object" just yet.
We will learn more about strings and objects in a later chapter.
Non-Primitive Data Types
Non-primitive data types are refer to objects that's why they called reference types.
primitive and non-primitive data types
Primitive types are predefined already defined in Java.
Non-primitive types are created by the programmer and is not defined by Java (except for String).
Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
A primitive type has always a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.