AppDividend
Latest Code Tutorials

Java Data Types Tutorial | Data Types in Java Explained

0

Java Data Types Tutorial | Data Types in Java Explained is today’s topic. As we all know, Java is statically typed language, we always need to define a keyword, that which type of the variable is and that keyword is called DataType. We declare data type before a variable. For example, int x; here int is an integer data type. Data types specify the different sizes and values that can be stored in the variable. In the last tutorial, we have seen the Java Collections example.

However, there are two types of data types in Java.

  1. Primitive data types
  2. Non-Primitive Data Types

Java Data Types Tutorial

Once a variable is declared to be of the certain data type, it cannot hold the values of other data types. Example: C,C++, Java. Other, Dynamically typed languages: These programming languages can receive the different data types over time. Ruby, Python, Javascript, etc.

Now we will discuss these two data types.

#Java Primitive Data Types

A primitive data type specifies the size and type of variable values, and it has no additional methods. The primitive data types are a building blocks of data manipulation. There are eight types of Primitive Data Types, which are the following.

  1. boolean
  2. char
  3. byte
  4. short
  5. int
  6. long
  7. float
  8. double

Now we will read about them one by one.

#boolean in Java

The boolean data type is defined with a boolean keyword. This data type holds two value, either it is true or false.

These values are mostly used in conditional cases, like if the condition is satisfied return true otherwise return false.

#Syntax

boolean b = true;

#char in Java

This data type is used to hold a single character in a variable. The size of each character is 2 bytes.

#Syntax

char ch = ’a’;

#byte in Java

The byte data type stores integer value between -128 to 127. If you know that your result of an equation will be in this range, it is better to use this data type, because it will save your memory.

The default size of this data type is 1byte.

#Syntax

byte x = 20;

#short in Java

The short data type is higher than the byte. It can store numbers between -32768 to 32767. Size of this data type is 2 byte.

#Syntax

short x=5400;

#int in Java

When we have a number, and it is not possible to store it in short, we can look for int type data type. This data type can store values between -2,147,483,648 to 2,147,483,647. That means -231 to 231.

Size: 32bit means 4 bytes.

#Syntax

int x=460000

#long in Java

When we have to store a huge number, we should always go with this data type. Long can store values between  -263 to 263-1. That means -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Size of long is 64 bit = 8byte.

#Syntax

long x=321546464646;

#float in Java

When we have to store values in decimal, then we can use this data type. Float is a 32-bit precision IEEE 754 floating point. A float data type can store values from 3.4e−038 to 3.4e+038.

Size of this data type is: 32 bits means 4 bytes.

#Syntax

float x=5.4;

#double in Java

Like long data type, when we have to store substantial decimal digits, we should go with the double data type. This can store up to 15 decimal digits, fractional numbers from 1.7e−308 to 1.7e+038.

Size of this data type: 64bits means 8 bytes.

#Syntax

double x = 54.9d

So here we end our all primitive data types. Now we will store each type of data types to their respective variable. See the below program.

class DataTypes
{
    public static void main(String args[])
    {
	boolean b=true;
	System.out.println("Value of Boolean is: "+b);
	
	char ch='D';
	System.out.println("\nValue of character is: "+ch);
	
	byte bt=126;
	//This will create compiler error as byte value
	//out of range
	//byte bbt=547;
	System.out.println("\nValue of Byte is: "+bt);
	//System.out.println("\nValue of 2nd Byte is: "+bbt);

	short sh=4532;
	System.out.println("\nValue of short is: "+sh);

	int n=532143;
	System.out.println("\nValue of Int is: "+n);

	long ln=362659536;
	System.out.println("\nValue of Long is: "+ln);

	float f=54.65f;
	System.out.println("\nValue of float is: "+f);

	double d=996.6655;
	System.out.println("\nValue of Double is: "+d);
     }	
}

See the below output.

Java Data Types Tutorial | Data Types in Java Explained

 

# Non-primitive data type

Non-primitive data types are called reference type data type or object variables, because they refer to the object. There are some non-primitive data types.

#Java String

Strings are a collection of characters, and they cannot change once they create.

#Java Array and object

An array is a group of similar variables; they dynamically allocate and can contain both primitive or objects depending on the definition. Their size has to specify in the only int, and their length can be found out by member length(). Their indexing always starts with zero, and they order.

So here we end data types in Java.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.