Data Types in Java
Data Type in Java is defined as that specifies the size of memory allocated for a specific variable and which type of value is stored in an identifier. In Java there is mainly two types of data type:-
1. Primitive Data Type
2. Non-Primitive Data Type
for example, int a; //where int is the data type and a is variable
Primitive Data Type
Java defined eight different types of primitive data types like int, short, long, byte, float, double, char, and boolean. Integers group contain four data type int, short, long, byte. The floating-point numbers group contains float and double data type numbers. Char and boolean both are different.
int Data Type
The most commonly used integer type is int. It is a signed 32-bit type that has a range from -2,147,483,648 to 2,147,483,647.
Syntax: <datatype>name of variable
for example- int b;
short Data Type
short is a signed 16-bit type. It has a range from -32,768 to 32,767. If our value in the program not more than this range then we recommended this type of data type.
Syntax: <datatype>name of variable
for example: short c;
byte Data Type
This is the smallest integer type of data type. It's range from-128 to 127 and 8-bit requires.
Syntax: <datatype>name of variable
for example byte d;
long Data Type
long data type is used when int is not large enough to hold the value. It's range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It's require 64-bit in memory.
Syntax: <datatype>name of variable
for example long e;
Floating-Point Types
Floating -point numbers, are also known as real numbers, are used when evaluating the expression that requires fractional precision. For example, calculations such as the square root of any numbers and other mathematics expressions. It has mainly two types:
1. float
2. double
float Data type
It requires a 32-bit size and sufficient for holding 6 to 7 precision value.
for example: float a=2.5564f
where F defines the float variable
double Data Type
It requires a 64-bit size and sufficient for holding 15 precision value.
double h=2.45247d
where d defines the double variable
boolean Data Type
It stores logical values. It can have only one of two possible values, true or false.
boolean b;
char Data Type
char data type store or hold a character. It requires 16-bit.
for example char ch='M';
Program for data types in java.
public class DataType{
public static void main(String[]args){
int a=2147483647;
byte b=127;
short c=32767;
long d=12332252626L;
float e=2.1234567F;
double g=2.12345678998765434d;
boolean i=true;
char ch='M';
System.out.println("Integer number is="+a);
System.out.println("byte number is="+b);
System.out.println("short number is="+c);
System.out.println("long number is="+d);
System.out.println("float number is="+e);
System.out.println("double number is="+g);
System.out.println("boolean number is="+i);
System.out.println("char number is="+ch);
}
Non-Primitive Data Type
Non-Primitive Data Type is called reference type because they refer to the objects. For example arrays and strings.
Arrays
An array is a group of like type variables that are referred to as a common name. The syntax for declaring an array in java program: data type[] arrayName; or data type arrayName[];
for example double myName[]; or double []myName;
Strings
The String data type is used to store a sequence of characters. Strings must be surrounded by double-quotes.
for example string m="codehubpoint";
No comments:
Post a Comment