A data type is an attribute of data that tells the compiler how the programmer intends to use the data; in other words, in which format the computer will store the data.
types of data types in Java:
- Primitive data types: The primitive data types consist of boolean, char, byte, short, int, long, float, and double.
- Non-primitive data types: The non-primitive data types consist of String, Classes, Interfaces, and Arrays.
Here’s a list of all primitive data types :
Datatype | Bytes | Bits | Range | Example |
Long | 8 | 64 | -263 to 263-1 | long n = 6500000L; |
Int | 4 | 32 | -231 to 231-1 | int n = 5000000; |
Short | 2 | 16 | -215 to 215-1 | short n = 10000; |
Byte | 1 | 8 | -27 to 27-1 | byte n = 90; |
Double | 8 | 64 | Precision = 15 digits | double n = 15.7d; |
Float | 4 | 32 | Precision = 6-7 digits | float n = 4.68f; |
Char | 2 | 16 | 0 to 216-1 | char c = ‘A’; char c = 67; |
Boolean | 1 | – | True/false | boolean isFunny = true; |
0-9 char = ASCII (0-9)
A-Z char = ASCII (65-90)
a-z char = ASCII (97-122)
Here’s a list of non-primitive data types:
String | Class | Interface | Array |
---|---|---|---|
A String stores a sequence of characters that can also contain spaces and numbers surrounded by double quotes. | A Class is like a template that is used to create and define objects, object data types, and methods. | An interface is a reference type that a class implements, thereby inheriting the abstract methods of the interface. | An Array is treated as an object of fixed size which contains elements of a similar data type. |
An object that contains multiple methods is basically a String. For instance, method to find its length, converting to upper or lower case, trimming text or replacing text. | The declaration of a class consist of access modifiers, class name, keyword and the body. It allows you to declare a variable along with its data type. | It consist of methods and variables, but the methods declared are abstract(In other words, method with no body, only signature) by default in an interface. | We can declare, instantiate, initialize and traverse an array. Array declaration has two components: the type like primitive or user defined and the name. |
String s = “How are you?”; | public void class Test { // class body } | interface Test { public void print(); } | int boxes[]; int[] boxes; |