Data Types in Java

Variables are only saved memory areas to store values. This implies that when you make a variable, you save some space in memory. In light of the data type of a variable, the working framework distributes memory and chooses what can be put in the held memory. Consequently, by appointing diverse data types to variables, you can store whole numbers, decimals, or characters in these variables.

There are two data types accessible in Java:

  • Reference/Object Data Types
  • Primitive Data Types
Primitive Data Types

There are eight primitive information types, which are supported by Java. Primitive data types are predefined by the dialect and named by a catchphrase. This section discusses these data types in detail.

Byte:
  • Byte information sort is an 8-bit marked two’s supplement whole number.
  • Maximum worth is 2^7 -1, which is equal to 127. This value is also included in the range of these values.
  • Minimum worth is -2^7, which is equal to -128.
  • Default value stored in a variable of this type is 0.
  • Byte information sort is utilized to spare space in vast exhibits, principally set up of numbers, since a byte is four times littler than an int.
  • Example: byte x = 200, byte y = -20
Short:
  • Short information sort is a 16-bit marked two’s supplement number.
  • The Maximum value is 2^15 -1, which is equal to 32,767. This number is also included in the range.
  • The Minimum value is -2^15, which is equal to -32,768.
  • Short information sort can likewise be utilized to spare memory as byte information sort. A short is 2 times littler than an int
  • The default value for this data type is 0.
  • Example: short x = 425164, short y = -76686
Int:
  • Int information sort is a 32-bit marked two’s supplement number.
  • The Maximum value for this data type is 2^31 -1, which is equal to 2,147,483,647. This number is also included in the range for this data type.
  • Minimum value for this data type is -2^31, which is equal to – 2,147,483,648.
  • Int is, for the most part, utilized as the default information sort for its indispensable qualities unless there is a worry about memory.
  • The default value for this data type is 0.
  • Example: int x = 826378, int y = -64782
long:
  • long information sort is a 64-bit marked two’s supplement whole number.
  • Maximum value for this data type is 2^63 -1, which is equal to 9,223,372,036,854,775,807.
  • Minimum value for this data type is -2^63, which is equal to -9,223,372,036,854,775,808.
  • This sort is utilized when a more extensive memory range than int is required.
  • The default value for those data type is 01.
  • Example: long x = 174636l, int y = -536452l
float:
  • Float is a data type, which is known for its solitary exactness, 32-bit IEEE 754 gliding point.
  • Float is, for the most part, used to spare memory in vast exhibits of coasting point numbers.
  • The default value for this data type is 0.0f.
  • Float information sort is never utilized for exact values, for example, money.
  • Example: float x = 254.3f
double:
  • Double information sort is a float with twofold exactness 64-bit IEEE 754 drifting point.
  • This information sort is, for the most part, utilized as the default information sort for decimal qualities.
  • Double information sort ought to never be utilized for exact values, for example, money.
  • The default value for this data type is 0.0d.
  • Example: double x = 321.4
boolean:
  • boolean information sort speaks to one bit of data.
  • Any boolean variable can assume one of the two values: true or false.
  • This information sort is utilized for basic banners that track natural/false conditions.
  • The default value for this data type is false.
  • Example: boolean check = true;
char:
  • char information sort is a solitary 16-bit Unicode character.
  • The Maximum value for a variable of this type is “\uffff” (or 65,535 comprehensive).
  • The minimum value for a variable of this type is “\u0000” (or 0).
  • char information sort is utilized to store any character.
  • example: char text =‘a’
Reference Data Types
  • Reference variables are made utilizing characterized constructors of the classes.
  • They are utilized to get to objects. These variables are proclaimed to be of a particular data type that can’t be changed. A few examples of such data types are Employee and Dog.
  • Class objects and different kind of variables go under reference data type.
  • Default estimation of any reference variable is invalid.
  • A reference variable can be utilized to allude to any object of the announced sort.
  • Example: myanimal = new Animals(“rabbit”);

Copyright 1999- Ducat Creative, All rights reserved.