Java Data Types: Comprehensive Guide and Examples

Rumman Ansari   Software Engineer   2024-07-04 03:14:01   14117  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Data types in Java

Datatype is a spacial keyword used to allocate sufficient memory space for the data, in other words Data type is used for representing the data in main memory (RAM) of the computer.Data types represent the different values to be stored in the variable. Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type. Java provides eight primitive data types for numeric values, characters, and Boolean values. This section introduces numeric data types.

In general every programming language is containing three categories of data types. They are

  • Fundamental or primitive data types
  • Derived data types
  • User defined data types.
data types

Primitive Data Types

There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword.

Categories of Primitive Data Types:

data types
Primitive data types in Java
TypeDescriptionDefaultSizeExample Literals
booleantrue or falsefalse1 bittrue, false
bytetwos complement integer08 bits(none)
charUnicode character\u000016 bits 'a', '\u0041', '\101', '\\', '\'', '\n', 'ß'
shorttwos complement integer016 bits(none)
inttwos complement integer032 bits-2, -1, 0, 1, 2
longtwos complement integer064 bits-2L, -1L, 0L, 1L, 2L
floatIEEE 754 floating point0.032 bits 1.23e100f, -1.23e-100f, .3f, 3.14F
doubleIEEE 754 floating point0.064 bits 1.23456e300d, -1.23456e-300d, 1e1d
Range of numeric data types in Java
TypeSizeRange
byte8 bits-128 .. 127
short16 bits-32,768 .. 32,767
int32 bits-2,147,483,648 .. 2,147,483,647
long64 bits-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
float32 bits 3.40282347 x 1038, 1.40239846 x 10-45
double64 bits 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324
Default Value and Default size of Primitive data types in Java
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

Derived data types

Derived data types are those whose variables allow us to store multiple values of same type. But they never allows to store multiple values of different types. These are the data type whose variable can hold more than one value of similar type. In general derived data type can be achieve using array.

Categories of Derived Data Types:

http://www.atnyla.com/library/images-tutorials/
Example
int  a[] = {10,20,30};  // valid
int b[] = {100, 'A', "ABC"};   //  invalid

Reference Data Types

User defined data types are those which are developed by programmers by making use of appropriate features of the language.Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy, etc.

Default value of any reference variable is null.


Reference Data Types:

data types in java

A reference variable can be used to refer any object of the declared type or any compatible type.

Example
Gender male = new Gender("yes");

MCQ Available

There are 51 MCQs available for this topic.

51 MCQTake Quiz