Java Data Types

Java programming language is a “strongly” typed language. Every variable declared must have a data type that determines the size of the variable in memory.

Advertisements

Expressions in Java have types. Although data types are strictly defined, there is compatibility between certain data types. This allows type conversion and casting of variables.

The data types are

  • Integers
  • Floating-point numbers
  • Characters
  • Boolean
Main TypeData TypeSize (in bits)Range
IntegerLong

 

Int

Short

Byte

64

 

32

16

8

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

 

-2,147,483,648 to 2,147,483,647

-32,768 to 32767

-128 to 127

Floating-point numberDouble

 

Float

64

 

32

4.9e-324 to 1.8e+308

 

1.4e-045 to 3.4e+038

CharacterChar160 to 65536
BooleanBoolean8 

Long

As per the table above, the long type is 64-bit signed type with a large range. The int type is the first choice for an integer type, but the long type is chosen when the number cannot be stored with int type.

long distance;

Int

The int type is the most common type used as an integer type. The int type is signed 32-bit type. Here is an example of int type.

int area;

short

The short type is the least used type in Java. It is signed 16-bit type with range -32,768 to 32767/ Here is an example of short type.

short total;

byte

The byte type is smallest of the integer type. It is signed 8-bit type suitable to store raw binary data. Other than that byte type can handle a stream of information from network, or file.

byte queue;

double

Advertisements

The double precision is also known as.double It has a storage of 64-bit. Also, it is faster in some optimized processors when compared to single precision. To maintain the accuracy of mathematical calculations, built-in functions like sin(), cos(), etc returns.double Here are some examples of.double

double radius;
double PI;

float

The float is a single precision that use32-bit of storage. Here are some examples of the.float

float length;
float height;

char

The char type is unsigned 16-bit to store Unicode characters. It has a range of 0 to 65536. The character is expressed in many ways.

  • Using single quotes.
  • Escape sequence
  • Unicode escape sequence

Here is the example for each type.

Example #1

char answer = 'Y';
char day = 'f';

The escape sequence does work for all characters, but only a few. Here is the list.

Escape SequenceDescription
‘\n’newline
‘\r’a carriage return
‘\f’form feed
‘\b’backspace
‘\t’a tab
‘\\’a backslash
‘\”‘a double quote
‘\”a single quote

Example #2

char ret = '\r';
char nwline = '\n';

Example #3

char a1 = '\u0041';    //The code '\u0041' is same as 'A'

Boolean

The boolean type has two values called boolean literals. Here are some examples of boolean type.

boolean finished = true;
boolean off;
off = false;
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version