Categories: Java

Data Types in Java

Data types are means to identify the type of data and associated operations of handling it. Java, like any other language, provides ways and facilities to handle different types of data by providing data types.

Java data types are of two types:

  • Primitive (or intrinsic) data types
  • Reference data types

Primitive data types come as a part of the language. Java provides eight primitive data types, which are: byte, short, int, long, float, double, char, and boolean.

Reference data types are constructed from primitive data types. These are: classes, arrays and interface. But their storage mechanism is different from primitive data types.

Primitive Datatypes

Primitive data types are the “basic” data values. The word ‘Primitive’ means a fundamental component that may be used to create other larger parts. Thus, by primitive data types, we mean fundamental data types offered by Java.

Java supports the following four categories of primitive data types:

  • Numeric Integral primitive types:

byte: It’s an integer data type with 1 byte (8 bits). The range of values is -128 to 127. Zero is the default value. For instance, byte b=10;

short: It’s a data type with 2 bytes. The range of values is -32768 to 32767. Zero is the default value. for instance, short s=11;

int: The integer data type is 4 bytes (32 bits). The range of values is -2147483648 to 2147483647. Zero is the default value. for instance, int i=10;

long: It’s an integer data type with 8 bytes (64 bits). Between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. Zero is the default value. e.g., long l=100012;

  • Fractional primitive types:

float: The float data type is 4 bytes (32 bits). 0.0f is the default value. float ff=10.3f, for example

double:The double data type is an 8-byte (64-bit) float data type. 0.0d is the default value. for instance, double db=11.123;

  • Character primitive types: char: An unsigned Unicode character with a size of 2 bytes (16 bits). From 0 to 65,535 example: char c=’a’;
  • Boolean primitive types: The boolean type is a special type for representing true/false values, and this group represents it. They are the language’s defined constants. example: boolean b=true;
Reference Data types

Broadly, a reference in Java is a data element whose value is an address. Arrays, classes, and interfaces are reference data types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable.

Difference between primitive and reference data types:
PropertiesPrimitive data typesReference Data Types
OriginPre-defined data typesUser-defined data types
Stored structureStored in a stackThe original object is stored in heap, while the reference variable is stored in stack.
When copiedTwo different variables is created along with different assignment(only values are same)Two reference variable is created but both are pointing to the same object on the heap
When changes are made in the copied variableChange does not reflect in the original ones.Changes reflected in the original ones.
Default valuePrimitive datatypes do not have null as default valueThe default value for the reference variable is null
Examplebyte, short, int, long, float, double, char, booleanarray, string class, interface etc.

Note: also read about the Escape Sequence

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago