DATA TYPES IN JAVA
Data types are divided into two groups:
- Primitive data types - includes byte, short, int, long, float, double, boolean and char.
- Non-primitive data types - such as String, Arrays and Classes
PRIMITIVE DATA TYPE |
A primitive data type specifies the size and type of variable values, and it has no additional methods.
Primitive types are divided into two groups:
- BOOLEAN
- NUMERIC
BOOLEAN |
A Boolean data type is declared with the Boolean keyword and can only take the value TRUE or FALSE.
NUMERIC |
Numeric data type are divided into
- CHARACTER
- INTEGRAL
CHARACTER |
Char:
The char
data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
INTEGRAL |
INTEGRAL are divided into
- INTEGER TYPE
- DECIMAL TYPE
INTEGER TYPE |
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are BYTE, SHORT, INT, AND LONG Which type you should use, depends on the numeric value.
Byte
The Byte data type can store whole numbers from -128 to 127. This can be used instead of int
or other integer types to save memory when you are certain that the value will be within -128 and 127:
Package datatypes;
public class Byte {
public static void main (String [] args) {
System.out.println(Byte.SIZE/8 + " : Bit");
System.out.printIn(Byte.MIN_VALUE);
System.out.printIn(Byte.MAX_VALUE);
}
}
OUTPUT:
1 : Bit
-128
127
Short
The short
data type can store whole numbers from -32768 to 32767:
Package datatypes;
public class Byte {
public static void main (String [] args) {
System.out.println(Short.SIZE/8 + " : Byte");
System.out.printIn(Short.MIN_VALUE);
System.out.printIn(Short.MAX_VALUE);
}
}
OUTPUT:
2 : Byte
-32768
32767
Int
The int
data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int
data type is the preferred data type when we create variables with a numeric value.
Package datatypes;
public class Byte {
public static void main (String [] args) {
System.out.println(Integer.SIZE/8 + " : Byte");
System.out.printIn(Integer.MIN_VALUE);
System.out.printIn(Integer.MAX_VALUE);
}
}
OUTPUT:
4 : Byte
-2147483648
2147483647
Long
The Long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":
Package datatypes;
public class Byte {
public static void main (String [] args) {
System.out.println(Long.SIZE/8 + " : Byte");
System.out.printIn(Long.MIN_VALUE);
System.out.printIn(Long.MAX_VALUE);
}
}
OUTPUT:
8 : Byte
-9223372036854775808
9223372036854775807
DECIMAL TYPE |
Decimal point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.
You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
Float
The float data type can store fractional numbers from 4 : Byte, 1.4E-45 to 3.4028235E38. Note that you should end the value with an "f":
Package datatypes;
public class Byte {
public static void main (String [] args) {
System.out.println(Float.SIZE/8 + " : Byte");
System.out.printIn(Float.MIN_VALUE);
System.out.printIn(Float.MAX_VALUE);
}
}
OUTPUT:
4 : Byte
1.4E-45
3.4028235E38
Double
The double data type can store fractional numbers from 8 : Byte, 4.9E-324 to 1.7976931348623157E308 Note that you should end the value with a "d":
Package datatypes;
public class Byte {
public static void main (String [] args) {
System.out.println(Double.SIZE/8 + " : Byte");
System.out.printIn(Double.MIN_VALUE);
System.out.printIn(Double.MAX_VALUE);
}
}
OUTPUT:
8 : Byte
4.9E-324
1.7976931348623157E308
NON-PRIMITIVE DATA TYPE |
Non-primitive data types are called reference types because they refer to objects.
The main difference between primitive and non-primitive data types are:
- 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.
- String is used to storing Text.
- A String variable contains a collection on characters surrounded by double quotes.
- An array of characters works same as java string
Array:
Class:
Object:
Interface: