Data Types in Java

Data Types in Java

by Sanjay Nair December 25, 2017

Data Types in Java

  • There are majorly two types of languages.
  • First one is Statically typed language where each variable and expression type is already known at compile time.
  • Once a variable is declared to be of a certain data type, it cannot hold values of other data types.
  • Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.

 

Java has two categories of data:

  • Primitive data (e.g., number, character)
  • Object data (programmer created types)

Primitive data

  • Primitive data are only single values; they have no special capabilities.
  • There are 8 primitive data types

 

Data TypeSize(bytes)
boolean1
int4
short2
byte1
long8
float4
double8
char2

 

 

boolean

  • boolean data type represents only one bit of information either true or false .
  • Values of type boolean are not converted implicitly or explicitly (with casts) to any other type.

byte

  • The byte data type is an 8-bit signed two’s complement integer.The byte data type is useful for saving memory in large arrays.
  • Size: 8-bit
  • Value: -128 to 127

short

  • The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
  • Size: 16 bit
  • Value: -32,768 to 32,767 (inclusive)

int

  • It is a 32-bit signed two’s complement integer.
  • Size: 32 bit
  • Value: -231 to 231-1

long:

  • The long data type is a 64-bit two’s complement integer.
  • Size: 64 bit
  • Value: -263 to 263-1.

Floating point Numbers : float and double

float

  • The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers.
  • Size: 32 bits
  • Suffix : F/f Example: 9.8f

double:

  • The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.

char

  • The char data type is a single 16-bit Unicode character. A char is a single character.

A Sample Java Program to demonstrate the primitive data types

 

Output

char: G

integer: 89

byte: 4

short: 56

float: 4.7333436

double: 4.355453532

 

 

Leave a Reply