Java Programming

Data Types in Java ( int, float, char )

In a programming language, the data term refers to the information or the collection of information which we use in our program. In the Java language, there are various types of inbuilt as well as user-defined data types are available. In this post, I will try to explain the various data types available in Java.

Java data types

Data Types In Java

The main purpose of using the data type concept is to specify the types of data which we use in our program as well as its related operations. In simple words, Data types determine the types of values and its size which we use in any program.

Data types are broadly categorized into two types,

  • Primitive Data Types
  • Derived Data Types

Primitive Data Types

Primitive data types are also termed as an inbuilt data type which contains the set of data types which are predefined i.e., their values are already assigned at the time of designing of the programming language.

There are basically 4 types of primitive data types,

  • Integer Data Type
  • Floating Data Type
  • Character Data Type
  • Boolean Data Type

Also Read – Variables in Java ( Easy Concept )

Integer Data Type

Integer data type is the data types which are responsible for holding the whole numbers value. For example – 28, -987, 0, -23, etc. Java does not support unsigned data type so all the integer data types come with some positive or negative symbols and by default, it becomes a positive number.

Java programming language contains 4 data types belongs to an integer type,

  1. byte
  2. short
  3. int
  4. long

Size & range of Integer data types

Type Size Range
byte 1 Byte -128 to 127
short 2 Byte 32,768 to 32,767
int 4 Byte -2,147,483,648 to 2,147,483,647
long 8 Byte -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Examples-

byte number; short number; int number; long number;

Floating Point Data Type

Floating data types refer to the data types which contain the whole number value along with the fractional numbers. These data types are very useful while storing the values which contains approximate values such as temperature, height, etc. For example – 23.45, 12.2, -9.5, 0.987.

There are basically 2 types of floating data types,

  • Float datatype
  • Double datatype

Size & range of floating point data types

Type Size Range
Float 4 Bytes 3.4e-038 to

1.7e+0.38

Double 8 Bytes 3.4e-038 to

1.7e+038

Example-

float number; double number;

Character Data Type

Character data type refers to the data type which is responsible for holding character type values. For example, ‘a’, ‘b’,.. The character data type can take up to 2 bytes.

Example-

char word;

NOTE – Characters are not a string, String is a group of characters.

Learn about strings – Constants in Java ( Learn in 5 mins )

Boolean Data Type

Boolean data type is the special data type which is responsible for hold only true and false value. This data type is mostly used when we want to check any particular condition in our program i.e., the condition is true or false. A boolean data type can only take up to one bit of storage.

Derived Data Types

Derived data types refer to the type of data which are derived using primitive data types. These data types are generally designed by users according to their need i.e., made with the combination of various data types.

In Java programming language, there are basically two derived data types are available,

  • Classes
  • Arrays

Class ( User Defined DataType )

A Class is a user-defined data type which is made with the combinations of various data types which is termed as data members.

Sometimes there is a need to use various data types and writing these datatypes again & again can take up a huge amount of memory. A class is a solution for that type of situations, a class may contain various data types such as char, int, float. Also, a class may contain various member functions in it.

The class can be created using the keyword class.

Syntax-

class class_name
{

  Data Members;
  Member Functions;

};

Array ( User-Defined Data Type )

An array is a user-defined data type which contains the group of the similar data type. An array is a data type similar to primitive data type but made with the combination primitive data type according to the user requirement. It is a set of contiguous data element proceeding with 0 as an index value.

The index value of the array data type represents the memory location and it starts with 0 as the memory location.

SYNTAX

data_type variable_name[index_value]

 

For Example,

int number[10];

char name[8];

 

ALSO READ

 

 

 

Leave a Reply