Java Data Types
Primitive Data Type
In Java, there are two types of data types: Primitive data type and Non-primitive data type.
Primitive Data Types: These are the built-in data types in Java.
- –
byte
: 8-bit signed integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). - –
short
: 16-bit signed integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). - –
int
: 32-bit signed integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). - –
long
: 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). - –
float
: Single-precision 32-bit IEEE 754 floating point. - – `double`: Double-precision 64-bit IEEE 754 floating point.
- –
boolean
: The boolean data type has only two possible values: true and false. - – `char`: Single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
Data Type | Size(in byte) | Description |
byte | 1 | -2^7 to 2^7-1, -128 to 127 |
short | 2 | -2^15 to 2^15-1, -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long | 8 | -2^63 to 2^63-1, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 | Sufficient for storing 6 to 7 decimal digits |
double | 8 | Sufficient for storing 15 decimal digits |
boolean | 1 | Stores true or false values |
char | 2 | Stores a single character/letter or ASCII values |
Declaration of Primitive Data Types and Assign Values in Java:
byte x = 10;
short num1=10;
int myNum = 5;
float myFloatNum = 5.99f;
double myDoubleNum = 5.5;
boolean myBool = true;
char myLetter = 'D';
Non-Primitive Data Type
Non-Primitive Data Types (Reference Data Types, or reference types): These are created by the programmer and are not defined by Java (except for String). Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot. Examples of non-primitive types are:
– String
: The most commonly used non-primitive data type. It represents a sequence of characters.
– Arrays
: A group of like-typed variables that are referred to by a common name.
– Classes
: A class is a blueprint from which individual objects are created.
– `Interfaces`: Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
– Enum
: A special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
String myText = "Hello"; // String
String[] array = new String[100]; // Array
// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep();
}
// Enumerate typeenum Level {
LOW,
MEDIUM,
HIGH
}
Note: Java is statically typed. That means each variable must be declared with a data type before it can be used.
Differences Between Two Data Types
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 are 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 type 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.
Java Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
The table below shows the primitive type and the equivalent wrapper class:
Primitive Data Type | Wrapper Class |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
Pass-by-reference and Pass-by-value
- Pass-by-reference: When a method is called, the method arguments reference the same variable in memory as the caller.
- Pass-by-value: When a method is called, the caller passes a copy of the argument variables to the method resulting in two values in memory.
Java is always pass-by-value. The reason this is confusing is that Java achieves its functionality using special variable types known as object references.
Reference: https://sentry.io/answers/java-pass-by-reference-or-value/
Header image from: https://getkt.com/blog/reintroduction-to-java-data-types/