Data types

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.


Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.


Data types :


  1. Byte
  2. Short
  3. Int
  4. Long
  5. Float
  6. Double
  7. Boolean
  8. Char

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 {

public static void main(String[] args) {
// TODO Auto-generated method stub
byte a = 50;
byte b = (byte) -80;
 
 
 
  byte c = (byte) (a + b);
 
  System.out.println("The Byte Value is : " + c);
 
}


}

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 ShortType {

public static void main(String[] args) {
// TODO Auto-generated method stub
short a = 1000;
short b = -1500;
 
 
  short c = (short) (a+b);
 
  System.out.println("The Short Value is : " + c);
 
}


}
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 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 15000;
int b = 20000;
 
int c = a + b;
System.out.println("The int Value is : " + c);
 

 
}


}

Sample Output

The int Value is : 35000




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 static void main(String[] args) {
// TODO Auto-generated method stub
int lightspeed
long days
long seconds
long distance
// approximate speed of light in miles per second 
lightspeed = 186000; 
days = 1000; // specify number of days here 
seconds = days * 24 * 60 * 60; // convert to seconds 
distance = lightspeed * seconds; // compute distance 
System.out.print("In " + days); 
System.out.print(" days light will travel about "); 
System.out.println(distance + " miles."); 
 
}


}

Sample Output


In 1000 days light will travel about 16070400000000 miles.



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 = 10.56f;
float b = 23.57f;
 
float c = a + b;
System.out.println("The Float Vaue is : " + c);

}
}


Sample Output


The Float Vaue is : 34.13



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 DoubleType {

public static void main(String[] args) {
// TODO Auto-generated method stub

double a = 123.456;
double b = 45.894;
 
double c = a + b;
System.out.println("The Double Value is : " + c);
 
double d = a - b;
System.out.println("The Double Value is : " + d);
 
}


}

Sample Output

The Double Value is : 169.35

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;
if(a == true) {
a = false;
System.out.println("The Boolean Value is : " + a);
}
}
}

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 Chartype {

public static void main(String[] args) {
// TODO Auto-generated method stub
char a = 'J';
char b = 'A';
char c = 'V';
char d = 'A';

  System.out.println("The Characters Value is : " + a+b+c+d);
}


}


Sample Output

The Characters Value is : JAVA

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.