• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » DataType » Data Types in Java
Next →
← Prev

Data Types in Java

By Dinesh Thakur

A variable in a program occupies some space in computer’s memory where some value is stored. A value to be stored may be an integer, floating point number, a character or a string. However, now the question arises that how much memory is allocated to these values and what is the range of values that can be stored in it as different types of values don’t occupy same space in memory. So to store these values, we require different data types depending on the needs of the application. The data type determines how much memory is allocated to store data and what operations can be performed on it. The Data type also defines the values that a variable can takes.

Java has statically typed language (do type checking at compile-time instead of at run-time).In Java, some of the predefined (already defined) data types are part of the programming language and constants (or an identifier), and variables are defined as certain data types before you can use it.

In Java, the data types can be classified into two broad categories:

Primitive type (or built-in type or value type) and
Reference type (non-primitive or derived type).

We’ll be covering the following topics in this tutorial:

  • Difference between primitive and reference data type in java
  • Primitive Data Types
  • Reference Data Types
  • Intergral Types

Difference between primitive and reference data type in java

The main difference between the primitive type and reference type is that the memory location associated with the primitive type variable contains the actual value of the variable. On the other hand, the memory location associated with the reference type variable contains an address that indicates the memory location of the actual object.

In Java, there are eight primitive data types. Out of the eight primitive types, six are for numbers, one for character and one is for boolean values. Of the six number types, four are types of integers, and two are types of floating-point numbers.
The reference type includes array, interface and class.

primitive and reference data types

Primitive Data Types

Primitive data types definition: That are non grouped pieces of data which can have only one value at a time. They are the simplest built in forms of data in Java. Other data types are made up of combination of primitive data types. Java initializes all primitive data types to default values if an initial value is not explicitly specified by the programmer. Integer and floating-point variables are initialized to 0. The char data type is initialized to null and Boolean data types are set to false. The example of primitive data type

types of primitive data types

The 8 primitive data types byte, short, int, long, float, double, boolean and char are called primitive data types , as they are built into the Java language at low level .

Reference Data Types

Reference data types are made by the logical grouping of primitive data types. These are called reference data types because they contain the address of a value rather than the value itself. Arrays, objects, interfaces, enum etc. are examples of reference data types.

Intergral Types

The integral types consists of integer types and character types.

Integer Data Types

Integer types are used to store whole numbers without a fractional part. In Java, there are four integer types: byte, short, int and long. These four types differ only in the number of bits and therefore in the range of values. Each data type can store both positive as well as negative values. However, unlike C/C++, Java does not support unsigned integers.
The following table enlists the four integer types, their size (i.e. number of bits it occupies in memory) and their minimum and maximum values.

Size of primitive data types in java

Type

Length

Minimum Value                     

Maximum Value

byte

8 bits   

-128                              

127

short

16bits

-32768

32767

int

32 bits

-2,147,483,648

2,147,483,647

long

64bits

-9,223,372,036,854,775,808

9,223,372,036,854,775,80

float

 

 -3.4Е+38

+3.4Е+38

double

 

-1.7Е+308

+1.7Е+308

boolean

Possible values ​​are two – true or false

char

   

Object

null

  

String

null

  

The range of integer types lie in the range -2x-1to +2x-1-1 where x is the total number of bits used to store the data type. One has been subtracted from x as one bit is reserved for the sign bit and the remaining for the magnitude. Also, we have subtracted 1 from the upper limit of the range because 0 is included.

• byte: In certain applications, you may not need integers as large as the standard int type provides. In such cases, Java provides two smaller integer types byte and short. The byte data type is an 8-bit (1 byte) signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
• short: The short data type is a 16-bit (2 byte) signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

The byte and short types are mainly intended for specialized applications, such as low-level file handling, or for large arrays where saving the memory space is important. We could have used this instead of int, but you can only use the short variable if you know  100% that your values will not exceed that range. 

     A variable age of byte type and pageCount of short type can be declared as follows.
byte age;          // The byte type variable ‘age’ can store value within range -128 to +127
short pageCount;        // The int type variable ‘pageCount’ can stores values within range -32768 to +32767

int: The int data type is a 4-bytes(32 bits) signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else.

A variable length of int type can be declared as follows.
int length;
long: The long data type is a 8 bytes (64-bits) signed two’s complement 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). Use this data type when you need a range of values wider than those provided by int.

A variable distance of long integer type can be declared as follows.
long distance;
Each integer type has a corresponding wrapper class Byte, Short, Integer and Long.

In Java, the size of integer types is specified by the language and is always the same regardless of what kind of computer a program runs on. This make the Java programs to produce the same results on all the computer. This is a huge improvement over the C/C++ language which let the compilers for different platforms determine the optimum size for integer data types. As a result, C/C++ program written or tested on one type of computer might not produce the same result on another computer. This is also true for other primitive types.

char Data Types

Type char is basically a 16-bit unsigned integer that represents a Unicode value. This means that a total of 216 or 65536 different unicode characters can be represented corresponding to the integer values 0 to 65535. Each printable and nonprintable character has a Unicode value. Because Java stores all characters as Unicode, it can be used with virtually any written language in the world.

A character variable choice of char type can be declared as follows,
char choice;

Floating-Point Data Types

The integer types do not provide the facility to represent decimal number so whenever you want to represent a number with a decimal such as 3.142, use a floating-point data type. If you try to use an integer type to store a number with fractional part, the fractional part will simply be discarded. So, Java provides two primitive floating point types: float and double to store numbers with fractional parts. Out of the two, double is most commonly used because all the math functions in Java class library use double values.

floating point types

NOTE: Both float and double follow the IEEE-754 format.

• float: The float data type requires 4 bytes (32-bits) for their storage. Their digits of precision (numbers of digits after decimals point) are 7 and range lies between -3.4 E38 (-3.4×1038) to +3.4E38 (+3.4×1038). This data type is normally used to represent quantities which inheritally cannot be represented by integers. For example : height of a person, average of number of
values etc.

A variable average of float data type can be declared as follows,

float average;

• Double: Double variables are used when we want to store much larger or smaller values than the maximum and minimum allowable in a standard variable. The double data type uses 8 bytes (M-bits) for their storage. Their digits of precision are 15 and range lies between -1.7E308 to +1.7E308. A variable sunDistance of double type can be declared as follows,

double sunDistance;

NOTE: Since the double type is twice as big as the float type, so double is known as double precision and float is known as single precision.

boolean Data Types

Boolean type is used to represent true/false values. For example: whether light is on, container is empty etc. It got its name from George Boole, an English Mathematician who invented the concept of Boolean Algebra. In Java, boolean type is represented using boolean keyword. The boolean data type can only hold two values: true and false. The values true and false are literals. Booleans are typically used for design making and for controlling the order of execution of a program.
You can declare a variable flag of type boolean as follows.
boolean flag;
NOTE: A boolean is not an integral type as in C++ and integer values cannot be used in place of boolean.

double Data Types

The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class.

You’ll also like:

  1. Primitive Data Types in Java | Java Primitive Types
  2. Data Types – Explain Data Type in C++.
  3. Data Types in C
  4. Data types in Python
  5. Data Types along with Their Sizes and Ranges
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW