Data Types in Java – Java Tutorial

 

Data Types in Java Program – Java Tutorial

 Data Types in Java - Java Tutorial

Problem Statement

List and Explain different data types in Java with programming examples. 

Solution:

Various data types used in Java are byte, short, int, long, char, float, double and boolean.

byte

This is in fact smallest integer type of data type. Its width is 8-bits with a range from -27 to 27-1. The variable can be declared as byte type as

Example: 

byte i, j;

short

This data type is also used for defining the signed numerical variables with a width of 16 – bits and having a range from -215 to 215-1. The variable can be declared as short as

Example:

short a, b

int

This is the most commonly used data type for defining numerical data. The width of this data type is 32-bit having a range form -231 to 231-1. The declaration can be

Example:

int p, q;

long

Sometimes when int data type is not sufficient for declaring some data then long is used.

The range of long is really very long and it is -263 to 263-1.

The declaration can be

Example:

long x, y;

float

To represent the real number(i.e. number that may have decimal point) float data type can be used. The width is 32-bit (4 bytes).

Example:

float x, y;

double

To represent the real numbers of a large range the double data type is used. Its width is 64-bit (8 bytes).

Example:

double a, b;

char

This data type is used to represent the character type of data. The width of this data type is 16-bit and its range is 0 to 216-1.

Example:

char ch;

boolean

Boolean is a simple data type that denotes a value to be either true or false.

Example:

boolean a, b;

Data Types in Java – Example Program

/* This program introduces use of various data type in the program */
class DatatypeDemo 
{ 
	public static void main(String[] args) 
	{
		byte a=10;
		short b=10*128;
		int c=10000* 128;
		long d=10000*1000*128;
		double e=99.9998;
		char f='a';
		boolean g=true;
		boolean h=false;
		System.out.println("The value of a=: "+a);
		System.out.println("The value of b=: "+b);
		System.out.println("The value of c=: "+c);
		System.out.println("The value of d=: "+d);
		System.out.println("The value of e=: "+e);
		System.out.println("The value of f=: "+f); 
		System.out.println("The value of g=: "+g);
		System.out.println("The value of h=: "+h);
	}
}

OUTPUT

The value of a=: 10

The value of b=: 1280

The value of c=: 1280000

The value of d=: 1280000000

The value of e=: 99.9998

The value of f=: a

The value of g=: true

The value of h=: false

Summary:

In this article, we understood the Different Data Types in Java Program – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds