Search Logic Blocks

Friday, January 3, 2020

Java: Primitive / Pre-defined Data Types

Java is statically-typed language means, all variables must be declared before using. We have seen already usage of int data type in our last post Arithmetic Operators. There are total 8 data types in Java. They are called primitive data types, as they are predefined in Java and named with a reserved keyword.

int -
  • 32-bit signed number.
  • An integer number whose value range is from (-2^31) -2.14 billon to (2^31-1) 2.14 billion.
  • Default value is 0.
  • Example: int a = 10000;

byte -
  • 8-bit signed number.
  • An integer number whose value range is from (-2^7) -128 to (2^7-1) 127.
  • Default value is 0.
  • Example: byte b = 120;

short -
  • 16-bit signed number.
  • An integer number whose value range is from (-2^15) 32,768 to (2^15-1) 32,767.
  • Default value is 0.
  • Example: short c = 10000;

long -
  • 64-bit signed number.
  • An integer number whose value range is from (-2^63) to (2^64-1)
  • Is used when the range wider than int is needed.
  • Data values always be suffixed by l or L.
  • Default value is 0L.
  • Example: long d = 32L;

float -
  • 32-bit floating point with a decimal point.
  • Can't be used precise value as currency.
  • Data values always be suffixed by f or F.
  • Default value is 0.0f
  • Example: float e = 0.25f;

double -
  • 64-bit floating point with a decimal point.
  • Can't be used precise value as currency.
  • Default value is 0.0
  • Example: double f= 13.25;

char -
  • Single 16-bit unicode character.
  • Char data type is used to store any character.
  • Char data values always be enclosed in single quotes (' ').
  • Default value is '\u0000' or 0.
  • Example: char g = 'a';

boolean -
  • One bit information (0 or 1).
  • There are only two possible values : true or false.
  • Default value is false.
  • Example: boolean h = true;

String -
  • String is not a data type, String is a class in package java.lang but used frequently as data type.
  • String is a group of any number of unicode characters.
  • String data values must be surrounded by double quotes (" ").
  • Example: String s = "Hello"; It creates automatically an object of String class.
  • String objects are immutable, once assigned the values can not be changed.

var -
  • var is not any data type, but it was introduced in Java 10 to represent any data type.
  • It automatically checks the data type of value assigned and internally declares.
  • Example: var i = 10; In this case I is an int.
  • Example: var str = "Hello"; But, in this case, str is string.
  • I have used var already in my earlier post How to get input from console.

No comments: