Java - Data Types

Java

In this tutorial we will learn about data types in Java programming language.

Constants

Constants are the fixed values that never change during execution of a program.

Following are type of constants in Java.

  • Integer constants
  • Real or Floating point constants
  • Character constants
  • String constants

Integer constants

These are the numbers without any decimal part. And they can be in decimal (bas 10), octal (base 8) or hexadecimal (base 16) number system.

Decimal integer constants uses ten digits from 0 to 9.

Example: -99, 0, 100 are decimal integer constants.

Octal integer constants starts with 0 and uses eight digits from 0 to 7.

Example: 01, 010, 011 are octal integer constants.

Hexadecimal integer constants uses ten digits from 0 to 9 and six letters from A to F. We can also use small letter from a to f.

Example: 0xA, 0xff, 0x0b are hexadecimal integer constants.

Real or Floating point constants

These are the numbers with decimal part.

Example: -10.12, 0.5, 100.234 are all real constants.

We can represent real constants in exponential a.k.a scientific notation.

mantisa e exponent

Where, mantisa is either a real number or integer.

And exponent is an integer with plus and minus symbol.

In the following example we are expressing 123000 in exponential form.

123000 = 1.23e5

Similarly, we can represent -0.012 in exponential form as follows.

-0.012 = -1.2e-2

Character constants

Character constants are single character enclosed in single quotes.

Example: 'a', 'b', 'c', '1', '2' etc are character constants.

String constants

String constants are sequence of characters enclosed in double quotes.

Example: "Hello World", "A", "B", "1" etc are string constants.

'a' is a character constant as it is using single quotes.

"a" is a string constant as it is using double quotes.

Escape Sequence

These are special backslash character constants in Java.

Some of the escape sequence is listed below.

Character ConstantDescription
'\''Single quote
'\"'Double quotes
'\a'Audible alert(bell)
'\b'Backspace
'\f'Form feed
'\r'Carriage return
'\t'Horizontal tab
'\v'Vertical tab
'\\'Backslash
'\/'Forward slash

Variables

A variable is a named memory location to hold some value.

The value stored in a variable can be changed any time we want.

Example of variables: isGameOver, namge, age etc.

Rules for variable names

To name variables we have to follow the given rules.

  • First character must be a letter or underscore
  • Can use letters (A-Z and a-z), digits (0-9) and underscore _
  • Must not be a keyword
  • Must not contain white space
  • Variable name can be of any length

Variable names are case-sensitive so, isGameOver and isgameover are treated as two separate variables.

Data types in Java

There are two categories of data types in Java.

The first one is Primitive data type. And the second one is Derivesd data type.

Following are the primitive data types available in Java.

  • INTEGER TYPE
    • byte
    • short
    • int
    • long
  • FLOATING POINT TYPE
    • float
    • double
  • CHARACTER TYPE: char
  • BOOLEAN TYPE: boolean

Derived types are Class, Interface, Array etc. and we will discuss them in the later part of this tutorial series.

Range of Integer types

KEYWORDSIZE (bytes)RANGE
byte1-128 to 127
short2-32,768 to 32,767
int4-2,147,483,648 to 2,147,483,647
long8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Range of Floating point types

KEYWORDSIZE (bytes)RANGE
float4-3.4e38 to 3.4e38
double8-1.7e308 to 1.7e308

Character type

These take 2 bytes of memory space and we use the char keyword to create a character data type variable.

Example:

char ch = 'y';

Boolean type

Boolean date type can take only two values true and false and we use the boolean keyword to create a boolean type variable.

Example:

boolean isGameOver = true;

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT