basic data types in java examples and programs


Basic Data Types In Java:


            There are two different types of data types used in java that are

Primitive type
Non-primitive type


Primitive type:

byte:
The byte data type is useful for saving memory in large arrays. It saves memory the range of byte data type is -128 to +127.It is an 8- bit signed two’s complement integer. The byte default value is 0.
Example: byte a=20;

short:
The short is as similar as byte but is  16-bit signed two’s complement integer. The range of short data type is -32768 to +32767. The short data type default value is 0.
Example: short s=1000;

char:
The char data type is a single 16-bit Unicode character. The range of char data type is '\u0000' to '\uffff'. The Char datatypes are used to store any character.
Example: char c ='J';

boolean:
The boolean data type used to represent one bit of information. This data type is used to check true or false conditions. The Default value is false.
Example: boolean isPrime = true;

int:
 It is a default data type for integral values and default value is 0. The range of an int is -2,147,483,648 to 2,147,483,647.It is a 32-bit signed two’s complement integer.
Example: int a=100;

Long:
The Long data type is a 64-bit signed two's complement integer. The range of Long data type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This data type is used to wide ranger int values. The default value of Long data type is 0L.
Example: long a = 10000L;

float:
 To allocate decimal type numeric values to variables it is useful. The float data type is a single-precision 32-bit IEEE 754 floating point.  The default value of float data type is 0.0f.
Example: float f = 23.5;

double:
The double data type is double precision 64-bit IEEE 754 floating point. This data types generally used as the default data type for decimal values. The Default value of double data type is 0.0d.
Example: double d1 = 12.6;

Non-primitive data type:
            The Non-Primitive data types reference variables are creates using defined constructor of the classes. The non-primitive data types also called as reference data types and its default value is null.
          Example: String str =”Java”;

Type-casting:
            Type casting is a process of Converting one data type to anther data type is known as type casting.
Example:

int x = 20;
Byte b = (byte)x;

 





EmoticonEmoticon