Data Types In Java Programming

Introduction

Data type specifies the type of value and its size to be stored in variables. There are two types of data type in java,

  1. Primitive data type (byte, short, int, long, float, double, chart, boolean)
  2. Object data type (String, Classes, Array)

Primitive Data Type

In java there are 8 primitive data types available,

SL. No. Data TypeCapacityMin and Max values
1 byte 1 byte -128 to 127
2short 2 byte -32,768 to 32,767
3int4 byte -2,147,483,648 to 2,147,483,647
4long8 byte -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
5float4 byte -3.4e38 to -1.4e-45 for negative values and 1.4e-45 to 3.4e38 for positive values
6double8 byte -1.8e308 to -4.9e-328 for negative values and 4.9e-324 to 1.8e308 for positive values
7char2 byte0 to 65535
8boolean 1 byte true / false

Note: Here ‘e’ represents *10 to the power. Suppose 3.4e38 means 3.4*1038    

Byte

  • The ‘byte’ data type can store number from -128 to +127.
  • It can store 1 byte (8 bit) of data

Example:

public class DataTypes {	
	public static void main(String[] args) {		
		byte age = 12;		
		byte Number = -100;			
		//byte number = 200; // compile error > Type mismatch: cannot convert from int to byte			
		System.out.println(age+"\n"+ Number);
	}
}
/* 
 Output
 12
-100
 */

Short

  • The ‘short’ data type can store number from -32,768 to 32767.
  • It can store 2 byte (16 bit) of data

Example:

public class DataTypes {	
	public static void main(String[] args) {		
		short number = 500;		
		short ticketNumber = -500;			
		//short num = 50000; // compile error > Type mismatch: cannot convert from int to byte			
		System.out.println(number+"\n"+ticketNumber);
	}
}
/* 
 Output
 500
-500
 */

Integer

  • The ‘short’ data type can store number from -2,147,483,648 to 2,147,483,647.
  • It can store 4 byte (32 bit) of data

Example:

public class DataTypes {	
	public static void main(String[] args) {		
		int number = 50;		
		int ticketNumber = -150;		
		System.out.println(number+"\n"+ticketNumber);
	}
}

/* 
 Output
 50
-150
 */

Long

  • The ‘long’ data type can store number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • It can store 8 byte (64 bit) of data
  • If value exceeds the size of integer data type size, long data type value should be ended with an “L”.  

Example:

public class DataTypes {	
	public static void main(String[] args) {		
		long number = 5000000000;		
		long ticketNumber = 150;		
		System.out.println(number+"\n"+ticketNumber);
	}
}

/* 
 Output
 5000000000
 150 
*/

Float

  • It can store 4 byte (32 bit) of data
  • float data type value should be ended with “f”.
  • Sufficient for holding 7 decimal digits

Example:

//Float Data type in java
public class DataTypes {	
	public static void main(String[] args) {		
		float Value1 = 55.98f;	// Output: 55.98
		float Value2 = 55.98;	// Compile error: Type mismatch:
		float Value3 = 55.951487623f;// Output: 55.95149
		float Value4 = 150; // Output: 150.0
	}
}

Double

  • It can store 8 byte (64 bit) of data
  • double data type value should be ended with “d”.
  • Sufficient for holding 15 decimal digits

Example:

//double Data type in java
public class DataTypes {	
	public static void main(String[] args) {		
		double Value1 = 55.98d;		// Output: 55.98
		double Value2 = 55.98;		// Output: 55.98
		double Value3 = 55;		// Output: 55.0		
	}
}

Char

  • The char data type is used to store a single character.
  • The char data type is a single 16-bit Unicode character.

Example:

//char Data type in java
public class DataTypes {	
	public static void main(String[] args) {		
		char Value1 = 'N';					
		System.out.println(Value1);
	}
}

Boolean

  • A boolean data type is declared with the boolean keyword
  • boolean data type represents only one bit of information like true or false

Example:

//boolean Data type in java
public class DataTypes {	
	public static void main(String[] args) {		
		boolean Value1 = true;	//Output: true
		boolean Value2 = false;	//Output: false	
		boolean Value3 = name;	//Compile error: name cannot be resolved to a variable	
	}
}

Object Data Type will be discussed in next topic

2 thoughts on “Data Types In Java Programming”

  1. Spot on with this write-up, I seriously feel this amazing site needs
    a lot more attention. I’ll probably be back again to see more,
    thanks for the info!

  2. Howdy! This post couldn’t be written any better!
    Reading through this post reminds me of
    my old room mate! He always kept chatting about this. I will forward this post to
    him. Pretty sure he will have a good read. Thank you for sharing!

Leave a Comment

Your email address will not be published.