Java data types example : Using data types in java


Java has eight basic data types to store numeric, floating point numbers, characters and Boolean values. These data types are often called as primitive data types. These data types are built in parts of java programming language. This makes them more efficient and useful.

No matter what operating system or platform you are using, these data types have same characteristics and size. This is one of the reasons that makes java platform independent.

Integer data types

Integer data types are of four types, short, byte, long and int. They are whole-valued signed numbers.

Byte: It is used to store small amount of data. This is a 8bit type and ranges from -128 to 127. It is useful when working with stream of data from a network or file. Byte is also useful when working with raw binary data that may not be compatible with other data types in java.

Short: Short is least used data type. It is a signed 16 bit type and ranges from -32,768 to 32,767. Short data type is more compatible with 16-bit computers.

Int: Int is most commonly used data type. It is a signed 32 bit and ranges from -2,147,483,648 to 2,147,483,647. The int data type is more efficient and versatile.

Long: long is a signed 64-bit type and ranges from -9,223,372,036,854 to 9,223,372,036,854,775,807. It is used when an int type is not large enough to hold the given value.

Example :
int num1;
byte num2;
short num3;
long num4;

Floating point data types

Floating point data types are used to present numbers with a fractional part. There are 2 types of floating point data types.

  1. Float
  2. Double

32 bit floating point numbers are called float numbers. 64 bit floating point numbers are called double number.

Example :
float weight;
double atomic_weight;

Boolean data type

A Boolean data type is used to store values, which is either “true” of “false”.

Example :
boolean isalive;

Character data type

Character data type is used to store a single Unicode character. Unicode code character sets are 16 bit values. Therefore the space required to store a single character is 16 bit.

Example :
char ch;

No comments:

Post a Comment