Primitive Data Type Vs. Object Data Type in Java

Java Primitive Data Types

Primitive data types in Java are the building blocks of data manipulation. They are the most basic data types available in the Java language. Java is a statically-typed programming language, which means that all variables must be declared before they are used.

Primitive data types allow storing only one value at a particular location. They are predefined in the Java language and occupy a fixed amount of memory.

The eight primitive data types supported by the Java programming language, which can be categorized into four groups: integers, floating-point numbers, characters, and boolean.

  1. byte: 8-bit signed two's complement integer and it can hold values from -128 to 127.
  2. short: 16-bit signed two's complement integer, and it can hold values from -32,768 to 32,767.
  3. int: 32-bit signed two's complement integer, and it can hold values from approximately -2.1 billion to 2.1 billion.
  4. long: 64-bit unsigned (Java SE 8 and later) two's complement integer, and it can hold very large integer values. It's often used when you need to work with extremely large numbers
  5. float: a single-precision 32-bit IEEE 754 floating point. It can represent decimal values with a high degree of precision. It's typically used for numbers and fractional parts.
  6. double: a double-precision 64-bit IEEE 754 floating point. It's commonly used for floating-point calculations.
  7. boolean: This is used for representing true or false values. It's the simplest data type, with only two possible values: true and false.
  8. char: A single 16-bit Unicode character. It can store individual characters, such as letters, digits, or symbols.

PrimitiveType.java

Output:

smallNumber: 10
smallInteger: 1000
integer: 1000000
bigInteger: 10000000000
decimalNumber: 3.14
bigDecimal: 123.456789
character: A
isJavaFun: true
isProgrammingHard: false

Data typeSizeRangeExponent
byte8-bit signed (1-byte)-128 to 127-27 to 27 - 1
short16-bit signed(2-byte)-32,768 to 32,767-215 to 215 - 1
int32-bit signed(4-byte)-2,147,483,648 to 2,147,483,647-231 to 231 - 1
long64-bit signed (8-byte)-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807-263 to 263 - 1
char16-bit (2-byte)'\u0000' to '\uffff'
float32-bit (4-byte)32-bit IEEE 754 floating-point numbers
double64-bit (8-byte)64-bit IEEE 754 floating-point numbers
booleanTrue and false

Default Values of Primitive Data Type

It is not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

Data typeDefault values
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (for any object)null
booleanfalse

Using primitive data types is efficient in terms of memory usage and performance because they are not objects and are stored directly in memory. Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If we cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Object Data Type in Java

These are also referred to as Non-primitive or Reference Data Type. They are so-called because they refer to any particular object. Unlike the primitive data types, the non-primitive ones are created by the users in Java.

Object data types can be used to store more complex data than primitive data types. For example, a class can store data for a person, such as their name, age, and address. A string can store text, such as a sentence or a paragraph. An array can store a collection of data, such as a list of numbers or a list of strings.

When the reference variables will be stored, the variable will be stored in the stack and the original object will be stored in the heap. In Object data type although two copies will be created they both will point to the same variable in the heap, hence changes made to any variable will reflect the change in both the variables.

Example of Object Data Types:

Classes, Interfaces, Strings, Arrays, Collections.

Default Values of Object Data Type

In Java, for object data types (reference types), the default value is always null. This means that when you declare a variable of an object data type (e.g., a class or interface), it does not have a meaningful value until you assign an actual object instance to it.

Difference Between Primitive Data Types and Object Data Types

AspectPrimitive Data TypeObject Data Type
TypeIt represents simple values.Represents objects and complex data structures
DeclarationIt declared directly (for example, int, boolean, double)Declared using class names (for example, String, Integer, Double)
Default ValueIt has default values (for example, 0 for int, false for boolean)Its Default value is null.
Memory ConsumptionGenerally, it consumes less memory.It tends to consume more memory due to object overhead.
ImmutableIt is mutable (except for final variables).It is immutable (values cannot be changed).
NullabilityIt cannot hold null.It can hold null.
PerformanceTypically, it is faster than object data type due to smaller memory footprint.It may be slower due to object overhead and potential garbage collection.
Type ConversionIt converts one data type to another automatically. For example, int to double.It requires explicit casting.
Additional MethodsThere is no additional methods or operations.It may have methods and operations specific to the object's class.
ArraysIt can create arrays of primitives types (for example, int[])It can create arrays of objects. For example, String[].
Wrapper ClassesThere are no wrapper classes for primitive types.There exist wrapper classes (for example, Integer for int, Boolean for boolean).
Exampleint num = 5;String text = "Hello";





Latest Courses