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:

  • Primitive Data Types
  • Reference/Object 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 a 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. 

  • Maximum value is 2^15 -1, which is equal to 32,767. 

  • This number is also included in the range. 

  • 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. 

  • 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 0l. 

Example: long x = 174636l, int y = -536452l 

float

  • float is a data type, which is know 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 genuine/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.

  • Maximum value for a variable of this type is “\uffff” (or 65,535 comprehensive).

  • 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”);


Java Literals

A literal in Java is a source code representation of a settled worth. They are spoken to specifically in the code without any calculation. Literals can be appointed to any primitive sort variable. Case in point: 

byte x = 86; 
char x = “a” 

int, byte, short and long can be communicated in hexadecimal(base 16), decimal(base 10) or octal(base 8) number frameworks too. Prefix 0 is utilized to show octal while prefix 0x demonstrates hexadecimal when utilizing these number frameworks for literals. 

For example, 

int numd = 134; 
int numo = 0243; 
int numx = 0x95; 

String literals in Java are determined like they are in most different programming languages by encasing a grouping of characters between a couple of twofold quotes. Illustrations of string literals are: 

  • “Hi Everyone” “two\nlines” “"these characters are inside quotes"” 

String sorts of literals can contain any Unicode characters. For instance: 

  • String news = “\u0001” 

You can also use escape sequences with Java. Here is a list of escape sequences that you can use. 

  • Double quote - "

  • Carriage return (0x0d) - \r 

  • Newline (0x0a) - \n 

  • Single quote - ' Backspace (0x08) - \b

  • Formfeed (0x0c) - \f

  • Tab - \t 

  • Space (0x20) - \s 

  • Octal character (ddd) - \ddd 

  • Backslash - \ 

  • Hexadecimal UNICODE character (xxxx) - \uxxxx