Data Types in Java


Data Types

When we create a variable in the program then we reserve some space in the memory. So, Data types are used to allocate memory to a variable. By assigning different data types to variables, we can store integers, decimals or characters in the variables.The following should be understood for every Data type:

1. Memory size allocated.
2. Default value
3. Range of values it can represent.

1. Memory size for data types:

Every data type has some memory size defined. This enables that when ever a variable is declared, the memory size defined will be blocked irrespective of the value that is going to get assigned.

2.Default value:

Every primitive data type has default values defined. When the programmer does not declare to assign any values to the variables, these default values will be assigned by the Virtual machine during the object instantiation.

3.Range of values the data types can represent:

It is extremely important to understand what are the min and max range of values a data type can be able to hold. Without this, when ever a value that cannot fit the data type is assigned, the applications crash with fatal errors or exceptions.
Please note that all the data types that can represent numbers can hold both positive and negative numbers.

In programming zero is considered to be a positive number.

There are two data types available in Java:

• Primitive Data Types
• Non-primitive Data Types

datatypes

Data Type Default Value Default size Range
boolean false 1 bit NA
char ‘\u0000’ 2 byte 0 to 216-1 or
\u0000 to \uFFFF
byte 0 1 byte -27 to 27-1 or
-128 to 127
short 0 2 byte -215 to 215-1 or
-32768 to 32767
int 0 4 byte -231 to 231-1 or
-2147483648 to 2147483647
long 0L 8 byte -263 to 263-1 or
-9223372036854775808 to
9223372036854775807
float 0.0f 4 byte �1.4E-45 to
�3.4028235E+38
double 0.0d 8 byte �439E-324 to
�1.7976931348623157E+308

 

1. Primitive Data Types

Primitive data types are predefined by the language and named by a keyword. There are two primitive data types supported by Java.

1.Boolean

2.Numeric

1.boolean:

This is used as a result of conditional statements.

  • boolean data type represents one bit of information.
  • This data type is used for simple flags that track true/false conditions.
  • There are only two possible values: true and false or 0 and 1.
  • Default value of this data type is false.

To declare a boolean variable:

public class abc {

boolean a;

public static void main(String[] args)

{

System.out.println(new abc().b); // prints false

// System.out.println(a);//You can’t reach a, which is not static variable.

  }}

2.Numeric:

There are two types of Numeric Data types supported by java.

  1. Character
  2. Integral

1.Character:

char:

The char type represents Unicode characters. Its size is 16 bits long. To declare a char variable, simple place it between single quotes(apostrophes).

  • char data type is a single 16-bit Unicode character.
  • Minimum value is ‘\u0000’ (or 0).
  • Maximum value is ‘\uffff’ (or 65,535 inclusive).
  • Char data type is used to store any character.
  • Example: char letter A =’A’

Example:

public class abc {

public static void main(String[] args) {

char c = ‘A’;

System.out.println(c); //prints A

} }

2.Integral

There are two types of Integral data types.

  1. Integer
  2. Floating-point

1. Integer:

Integer types (Does not allow decimal places)

  • byte
  • short
  • int
  • long

byte:

  • Byte data type is an 8-bit signed two’s complement integer.
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
  • Example: byte a = 100 , byte b = -50

short:

  • Short data type is a 16-bit signed two’s complement integer.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (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 is 0.
  • Example: short s = 10000, short r = -20000

int:

  • Int data type is a 32-bit signed two’s complement integer.
  • Minimum value is – 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647(inclusive).(2^31 -1)
  • Int is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0.
  • Example: int a = 100000, int b = -200000

long:

  • Long data type is a 64-bit signed two’s complement integer.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  • This type is used when a wider range than int is needed.
  • Default value is 0L.
  • Example: long a = 100000L, int b = -200000L

Example:

public class abc{

public static void main(String[] args) {

byte a = 127; // Min:-128 Max: 127

int b = 28;

long c= 12345L; //a long value

System.out.println(a);

System.out.println(b);

System.out.println(c);

} }

1.Floating-point:

This is also known as Rational Numbers(Numbers with decimal places).

  • float
  • double

float:

  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • Float data type is never used for precise values such as currency.
  • Example: float f1 = 234.5f

double:

  • 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 is 0.0d.
  • Example: double d1 = 123.4
  • Example: boolean one = true

Example:

public class abc{

public static void main(String[] args) {

float f = 6.2;

double dd = 123.4;

System.out.println(f); //prints 6.2

System.out.println(dd); //prints 123.4

}}

 2. Non-Primitive Data Types

It is also known as user defined data types. There are two types of Non-primitive data types supported by java.

String

The String type is a class, not a primitive type. String literals contain a string of letters including escape sequences that can appear as char literals.

Example:

public class abc{

public static void main(String[] args) {

String s = “This is a test”;

String s1 = “\nThis is the second line”;

System.out.println(s+s1);

} }

Array

Java provides a user defined data type the array, which stores a fixed-size sequential collection of elements of the same type. An array is a collection of variables of the same type.

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You can not change its length. Each item in an array is called an element and each element is accessed by its numerical index.

Example:

class abc{

public static void main(String args[]){

int a[]=new int[5]; //declaration and instantiation

a[0]=10; //initialization  

a[1]=20;

a[2]=30;

a[3]=40;

a[4]=50;

//printing array  

for(int i=0;i<a.length;i++) //length is the property of array  

System.out.println(a[i]);

}}

Difference between Primitive and Non- Primitive Data Type

Primitive Data Type:

  • A primitive data type is one that fits the base architecture of the underlying computer such as int, float, and pointer, and all of the variations, there of such as char short long unsigned float double and etc, are primitive data type.
  • Primitive data are only single values, they have not special capabilities.
  • The examples of Primitive data type s are given byte, short, int, long, float, double, char etc.
  • The integer reals, logic data character data pointer and reference are primitive data structures data structure that normally are directly operated upon by machine level instructions are known as primitive structure and data type.

Non- Primitive Data type:

  • A non-primitive data type is something else such as an array structure or class is known as the non-primitive data type.
  • The data type that are derived from primary data types are known as non-primitive data type.
  • The non-primitive data types are used to store the group of values.
  • Examples of non-primitive data types are Array, structure, union, link list, stacks, queue etc.

Comments are closed.