DATATYPES

Data Types in Java

In java, there are two types of data types
  • primitive data types
  • non-primitive data types
datatype in java

Data Type
Default Value
Default size
boolean
false
1 bit
char
'\u0000'
2 byte
byte
0
1 byte
short
0
2 byte
int
0
4 byte
long
0L
8 byte
float
0.0f
4 byte
double
0.0d
8 byte

A. Byte
  1. Byte data type is a 8-bit signed two's complement integer
  2. Minimum value is : -128 (-2^7)
  3. Maximum value is : 127 (inclusive)(2^7 -1)
  4. Default value is : 0
  5. 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
Syntax :

byte Variable_Name = Value;

Example (DataType_Byte.java) :

public class DataType_Byte {
 
 byte a = 50;
 byte b = (byte) -80;
 
 void add() {
  
  byte c = (byte) (a + b);
  
  System.out.println("The Byte Value is : " + c);
 }
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Byte obj = new DataType_Byte();
  
  obj.add();
 }
}

Sample Output

The Byte Value is : -30



B. Short
  1. Short data type is a 16-bit signed two's complement integer
  2. Minimum value is : -32,768 (-2^15)
  3. Maximum value is : 32,767(inclusive) (2^15 -1)
  4. Default value is : 0
  5. Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
Syntax :

short Variable_Name = Value;

Example (DataType_Short.java) :

public class DataType_Short {
 
 short a = 1000;
 short b = -1500;
 
 void add() {
  
  short c = (short) (a + b);
  
  System.out.println("The Short Value is : " + c);
 }
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Short obj = new DataType_Short();
  
  obj.add();
 }
}

Sample Output

The Short Value is : -500



C. Int
  1. Int data type is a 32-bit signed two's complement integer
  2. Minimum value is : -2,147,483,648.(-2^31)
  3. Maximum value is : 2,147,483,647(inclusive).(2^31 -1)
  4. Default value is : 0
  5. Int is generally used as the default data type for integral values unless there is a concern about memory
Syntax :

int Variable_Name = Value;

Example (DataType_Int.java) :

public class DataType_Int {
 
 int a = 15000;
 int b = -20000;
 
 void add() {
  
  int c = a + b;
  
  System.out.println("The int Value is : " + c);
 }
}

class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Int obj = new DataType_Int();
  
  obj.add();
 }
}

Sample Output

The int Value is : -5000



D. Long
  1. Long data type is a 64-bit signed two's complement integer
  2. Minimum value is : -9,223,372,036,854,775,808.(-2^63)
  3. Maximum value is : 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  4. Default value is : 0L
  5. This type is used when a wider range than int is needed
Syntax :

long Variable_Name = Value_L;

Example (DataType_Long.java) :

public class DataType_Long {
 
 long a = 1000L;
 long b = -2000L;
 
 void add() {
  
  long c = a + b;
  
  System.out.println("The Long Value is : " + c);
 }
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Long obj = new DataType_Long();
  
  obj.add();
 }
}

Sample Output

The Long Value is : -1000



E. Float
  1. Float data type is a single-precision 32-bit IEEE 754 floating point
  2. Default value is : 0.0f
  3. Float data type is never used for precise values such as currency
  4. Float is mainly used to save memory in large arrays of floating point numbers
Syntax :

float Variable_Name = (float) Value;

Example (DataType_Float.java) :

public class DataType_Float {
 
 float a = (float) 10.56;
 float b = (float) -23.57;
 
 void add() {
  
  float c = a + b;
  
  System.out.println("The Float Vaue is : " + c);
 }
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Float obj = new DataType_Float();
  
  obj.add();
 }
}

Sample Output

The Float Vaue is : -13.009999



F. Double
  1. double data type is a double-precision 64-bit IEEE 754 floating point
  2. Default value is : 0.0d
  3. Double data type should never be used for precise values such as currency
  4. This data type is generally used as the default data type for decimal values. generally the default choice
Syntax :

double Variable_Name = Value;

Example (DataType_Double.java) :

public class DataType_Double {
 
 double a = 123.456;
 double b = -45.894;
 
 void add() {
  
  double c = a + b;
  System.out.println("The Double Value is : " + c);
 }
 
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Double obj = new DataType_Double();
  
  obj.add();
 }
}

Sample Output

The Double Value is : 77.56200000000001



G. Boolean
  1. boolean data type represents one bit of information
  2. There are only two possible values : true and false
  3. This data type is used for simple flags that track true/false conditions
  4. Default value is : false
Syntax :

boolean Variable_Name = Value (true/false);

Example (DataType_Boolean.java) :

public class DataType_Boolean {
 
 boolean a = true;
 
 void check() {
  
  if(a == true) {
   
   a = false;
   
   System.out.println("The Boolean Value is : " + a);
  }
 }
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Boolean obj = new DataType_Boolean();
  
  obj.check();
 }
}

Sample Output

The Boolean Value is : false



H. Char
  1. char data type is a single 16-bit Unicode character
  2. Minimum value is : '\u0000' (or 0)
  3. Maximum value is : '\uffff' (or 65,535 inclusive)
  4. Char data type is used to store any character
Syntax :

char Variable_Name = Value;

Example (DataType_Char.java) :

public class DataType_Char {
 
 char a = 'J';
 char b = 'A';
 char c = 'V';
 char d = 'A';
 
 void join() {
  
  System.out.println("The Characters Value is : " + a+b+c+d);
 }
}
 
class MainClass {
 
 public static void main(String args[]) {
  
  DataType_Char obj = new DataType_Char();
  
  obj.join();
 }
}

Sample Output

The Characters Value is : JAVA

No comments:

Post a Comment