Data types
Primitives are like the cups at the Coffee-House, they come in different sizes and each has name like small, big, medium.
Variable (Cup) can be of two type
- Primitive Type
- Reference Type
Primitive Type
- Primitives are like the cups at the Coffee-House They come in different sizes and each has name like small , big, medium.
| Type | Bit/Bytes | Range |
|---|---|---|
| boolean | 1 bit | True or False |
| char | 16 bit/ 2 bytes | 0 to 65535 |
| byte | 8 bit/ 1 byte | -128 to 127 |
| short | 16 bit/ 2 bytes | -32768 to 32767 |
| int | 32 bits/ 4 bytes | -2147483648 to 2147483647 |
| long | 64 bits/ 8 bytes | Huge To huge |
| float | 32 bits/ 4 bytes | varies |
| double | 64 bits/ 8 bytes | varies |
byte
- The byte data type is an 8-bit signed two's complement integer.
- byte has a minimum value of -128(-2^7) and a maximum value of 127(2^7-1).
- Default value for byte is 0
- The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
- Example : byte s= 100 , byte r = -50
short
- The short data type is a 16-bit signed two's complement integer.
- It has a minimum value of -32,768(-2^15) and a maximum value of 32,767(2^15-1).
- short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
- Default value for short is 0.
- Example : short s= 20000 , short r = -20000
int
- The int data type is a 32-bit signed two's complement integer.
- It has a minimum value of -2,147,483,648(-2^31) and a maximum value of 2,147,483,647(2^31-1).
- int is generally used as the default data type for integral values unless there is a concern about memory.
- Default value for int is 0.
- Example : int a = 200000, int b = -200000
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(-2^63) and a maximum value of 9,223,372,036,854,775,807(2^63-1).
- long is used when a wider range than int is needed.
- Default value for long is 0L.
- Example : long a = 200000L, long b = -200000L
float
- The float data type is a single-precision 32-bit IEEE 754 floating point.
- Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification.
- float is mainly used to save memory in large arrays of floating point numbers.
- Default value for float is 0.0f.
- Example : float f = 123.4f
double
- The double data type is a double-precision 64-bit IEEE 754 floating point.
- This data type is generally used as the default data type for decimal values. Generally the default choice
- double data type should never be used for precise values such as currency.
- Default value for double is 0.0d.
- Example : double d = 123.4
boolean
- The boolean data type has only two possible values: true and false.
- boolean is used for simple flags that track true/false conditions.
- Default value for boolean is false.
- Example : boolean one = true
char
- The char data type is a single 16-bit Unicode character.
- It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
- char data type is used to store any character.
- Example . char letter ='AAA'
Reference type
There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a key word. Let us now look into detail about the eight primitive data types.
- A Reference variable holds bits that represents a way to access an object.
- It
doesn't hold the objectitself, but it holds something like a pointer, or an address.
Reference Type Example:
Objects (Classes & Interfaces) and Arrays
- Objects (Classes & Interfaces) and Arrays are the reference or non-primitive data types in Java.
- They are so called because they are handled by "reference" i.e. variables of their type store the address of the object or array in a variable.
- The reference type can hold three kinds of values-
class type :
Objects (Classes & Interfaces) and Arrays are the reference or non-primitive data types in Java. They are so called because they are handled by "reference" i.e. variables of their type store the address of the object or array in a variable.
The reference type can hold three kinds of values-
- Points to an object/class instance
- Whenever a variable is created, a reference to an object is also created using the name of a class for its type i.e. that variable can contain either null or a reference to an object of that class. It is not allowed to contain any other kinds of values. Such type is called reference types in Java.
- Example :
class Example { ... ... } main() { Example example1; Example example2; }
interface type :
- Points to an object, which is implementing corresponding interface.
- When a class declaration implements an interface, that class inherits all of the variables and methods declared in that interface. So the implementations for all of the methods declared in the interface must be provided by that class. For example, Java provides an interface called ActionListener whose method named actionPerformed() is used to handle the different kind of event . Java also provides a class called Thread that implements Runnable interface.
- Example :
Runnable r; r = new Thread();
array type :
- Points to an array instance.
- An array is a special kind of object that contains values called elements. The java array enables the user to store the values of the same type in contiguous memory allocations. The elements in an array are identified by an integer index which initially starts from 0 and ends with one less than number of elements available in the array. All elements of an array must contain the same type of value i.e. if an array is a type of integer then all the elements must be of integer type. It is a reference data type because the class named as Array implicitly extends java.lang.Object.
- Example :
DataType [] example1, example2, .......examplen; DataType [] example = new DataType [ArraySize]; DataType [] example = {item 1, item 2,...item n};

