Data Types in java
Java variables are used to reserve memory locations to store values. It means when you create a variable you reserve some space in memory. Space allocation depend upon data type of a variable and operating system allocates memory and decide what to be store in reserved memory. Java introduced two data type’s.
- Primitive data types
- Reference / Object data types
Primitive Data Types
Java is a strongly type programming language , which means all variable must declare the type before they can be used. A primitive data type is predefined by the java programming language, known as reserved keyword and don’t share state with other primitive values.
int i = 10;
Writing above statement indicates your program that variable name is “i”, holds numerical data and has initial value of “10”. Data type of a variable determines the value it contain, operations may be performed on it. Java programming language supports other primitive data types are listed below :
Byte : Data type is an 8-bit signed two’s complement integer, has a minimum value of 128 and a maximum value of 127 . The byte data type can be useful for saving memory in large arrays and can be used as an alternative of int, where their limits help to clarify your code.
byte a = 50; byte b = -50;
Short : Data type is a 16-bit signed two’s complement integer, has a minimum value of 32,768 and a maximum value of 32,767 (inclusive). The same rules of byte are applicable on short data type.
short s1 = 100; short s2 = -200;
int : Data type is a 32-bit signed two’s complement integer, has a minimum value of 2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). int data type is used as default data type for integral value and is large enough for the numbers your program will use, otherwise use long if wider range is required.
int i =20;
long : The long data type is a 64-bit signed two’s complement integer. It has a minimum value of 9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
float : Data type is a single-precision 32-bit IEEE 754 floating point. Float data is not recommended to use for precise values such as currency. Default value is 0.0f. Mainly used to save memory in large arrays of floating point numbers.
float f = 100.5f;
double : By default this data type is used for decimal values. Double data type is a double-precision 64-bit IEEE 754 floating point. This data type is not recommended for precise values such as currency. Default value is 0.0d.
double d = 2.5;
boolean : Data type represents one bit information with two possible values true and false. This data type is use to flag that tracks true/false conditions. Default value is set to false.
boolean b = true;
char : is 16 bit unicode character mainly used to store any character. Minimum value ‘\u0000’ (or 0) and maximum value is ‘\uffff’ (or 65,535 inclusive).
char c = 'A';
Reference / Object data types
Reference type based on java class rather then primitive data type built in java language. A class can be a user defined class or java built in class. Both type of classes can be used as reference data types. When an object is created from a class, java allocates the memory to store the object and object refers to that memory location. To declare a reference type variable you need to list the class name as data type.
Object o = new Object();
 Addison Wesley – Concurrent Programming In Java, Design Principles And Patterns, 2Nd Edition. (n.d.).
 Ralph Morelli, R. W. (n.d.). Java, Object-Oriented Problem Solving – ISBN 0131474340. Prentice Hall.