Data Types & Literals

Definition of data types

Data type is something which gives information about.

  • Size of the memory location and ranges of data that can be accommodated inside that location.
  • Possible legal operations those can be performed on that location.
  • What type of result comes out from an expression when these types are used inside that expression.

data type in java, java, literals

Different Java data types

In Java mainly we have two types of data types

  • Primitive Types - Used to store single value at a time.
  • Referenced Types - Used to collect multiple of values using primtive types.

Why do we have 8 primtive types in Java ?

Based on type and range of data, primtive type are defined into 8 types.

Why do we have 4 referenced types ?

To collect same type of values Array is given and

To collect different type of values class, interface, enum are given.

Limitation of Primitive Data Types

Using primitive data types we cannot store multiple values in continuous memory locations.

Problem 1 - For instance if we want to store multiple values, say 1 to 100, we must create 100 variables., All those 100 variable are created across JVM at different locations.

Problem 2 - Using variable we cannot pass all values to the remote computer with single network call, which increase burden on network and also increase lines of code in program.

Solution - To solve above two problem, we must group all values to send them as a single unit from one application to another application as method argument or return type.

Understanding Array -

In Java Array is a referenced data types. It is used to store fixed number of multiple values of same type in continuous memory locations.

Below diagram show array creation and its memory location structure.

data type in java, java, literals

Array Limitation -

Its size is fixed, means it will not allow us to store values more than its size. Also it will not allow different values.

Literals -

Literal is a constant.

Ex - 10,20.4, 'a',"abc". true

Types of literals and their default data types

Integral Literals - All integer type literals are called integral literals. By default they are of type int. if we want to represent them as long we must suffix literal with "l" or "L".
Floating point Literals - All floating point literals are of type double if we want to represent them as float we must suffix literal with "f" or "F". double literal can also be suffixed with "d" or "D".
Character Literals - The single character place inside single quote is considered as character. All character literal of type char.
String Literals - Character placed inside double quote is considered as string literal. All string literal are of type java.lang.String.

Below table show Types of literals and its default value.

data type in java, java, literals


Leave a Comment

%d bloggers like this: