Data types in java

What are the data types in Java? What is the use of data type and why should we learn it? Let’s understand in detail what is the data type in Java and types of data type in Java.

In any programming language, variable is like an empty container, they contain the value (which we assigned to them) of some specified type. A variable must be of the specified data type and the type of specified variable is called data type in Java. so here java offers basically two different data types.

  • Primitive data type comes as a part of the language, java provides eight primitive, data types, which are: byte, short, int, long, float, double, char, Boolean.
  • Reference data types are constructed from primitive data types. these are classes, arrays, and interfaces. But their storage mechanism is different from primitive data types. Reference data types are used to store the memory address of an object.

Primitive Data types in Java

Java supports the following four categories of primitive data types:

  • Numeric Integral Primitive types
  • Fractional data type
  • Character primitive types
  • Boolean primitive types

Have a look in detail.

Numeric integral data types in Java

The data type that is used to store numeric values fall under this subcategory I.e., numeric primitive data types. There are four numeric integral types in java.

  1. Byte:
  • Size: 8 bits (1 byte)
  • Description: Byte-length integer
  • Range: -128 to +127
  1. Short:
  • Size: 16 bits (2 bytes)
  • Description: Short integer
  • Range: -32,768 to +32,767
  1. Int:
  • Size: 32 bits (4 bytes)
  • Description: Integer
  • Range: -2 billion to + billion
  1. Long:
  • Size: 64 bits (8 bytes)
  • Description: long integer
  • Range: -10E18 to +10E18

Floating numeric data types in Java

Floating numeric data type can store fractional as well as a decimal value.

  1. Float:
  • Size: 32 bits (4 bytes)
  • Description: single-precision floating-point

2. Double:

  • Size:64 bits (8 bytes)
  • Description: double -precision floating point

Character data types in Java

Char data type is used to sore characters in java

  • Size:16 bits (2 bytes)
  • Description: a single character

Boolean data type in Java:

It is used to store a single true/false value.

  • Size: java reserve 8 bits but only uses 1 bit.
  • Description: Logical or boolean value.

Reference data types in Java

In java, you can define one or more user-defined data types via the class construct. For example, to create a program that behaves like a dog, you can define a class that represents a dog:

class Dog

{

void bark()

{

System.out.println(“”);

}

}

Look, this user-defined data type begins with the keyword class.l, followed by the name for the data types, in the case Dog. Followed by the specification of what it is to be a dog between opening and closing curly brackets. this simple example provides no data fields, only the single behavior of barking, as represented by the method bark.

What is the basic difference between primitive user-defined data types?

Primitive data types.

 

User-defined data type
These are built-in data types.java to provide these data types. These are created by the user.
 The size of these data types is fixed. The sizes of these data types are variable as their sizes depend upon their constituent.
These data types are available in all parts of java program The availability of these data types depends upon their scope.