Primitive and reference data types in java

In the previous post we learnt about different types of operators in java. Now, we will learn about different data types in java. When you will start writing the java program, you should know about the available variables in java. The use of the variable is to reserve memory space to store values into it. That means when you declare the variable that occupies some space in memory to store values.  Reservation of the memory is depending on the variable type. That are primitive and object/reference data types.

Primitive Data Types:
        The Java Programming Language is statically-types, which means that all data types must declare first first they can be used. There are eight primitive data types are supported by java.

Byte:
  • byte data type is 8-bit signed two's complement integer.
  • Minimum value is -128, maximum value is 127(inclusive).
  • default value is 0.
  • byte is mainly used to store data in large array's. Byte is four times smaller that a int.
Ex: byte b=110; byte b=-14;

Short:
  • short data type is 16-bit signed two's complement integer.
  • Minimum value is -32,768 , maximum value is 32,767(inclusive)
  • default value is 0.
  • A short data type can also be used to store byte data type. A short is two timessmaller that int.
Ex: short s=10001; short s= -10001;

int:
  • A int data type is 32-bit signed two's complement.
  • Minimum value is -2,147,483,648, maximum value is 2,147,483,648.
  • default value is 0.
  • This data type is better to use normal values, but you want to use larger values use long.
Ex: int i=100001; int i=-200001

long:
  • long data type is 64-bit signed two's complement.
  • Minimum value is -9,223,372,036,854,775,807, maximum value is 9,223,372,036,854,775,807(inclusive).
  • Default value is 0L.
  • This is used when a wider range else int is used.
Ex: long l = 100000L; long l = -100000L;
  • above values 'L' represents the long type values.
Note:The above all are the numeric type values only accepted.

Float:
  • Float data type is a single precision 32-bit IEEE 754 floating point.
  • This data type is used to save memory in large array's of floating point numbers.
  • This data type should never used for precise values, such as currency.
  • Default value is 0.0f.
Ex: float f = 10.5f;

Double:
  • Double data type is double-precession 64-bit IEEE 754 floating point.
  • This data type is generally as the default type for decimal values.
  • Double data type should never be used for precise values , such as currency.
  • Default value is 0.0d.
Ex: double d= 105.4d;
Note:The above two types are allows only floating point values.

Boolean:
  • boolean data type allows only one-byte of space for values.
  • There are only two possible values : true, false.
  • This data is used for only true/false conditions.
  • Default value is false.
Ex: boolean one = true;

Char:
  • Char data type is a single 16-bit Unicode character.
  • Minimum value is '\u0000'(0), maximum value is '\uffff'(65,535 inclusive).
  • Char data type is used to store any character.
  • Minimum value is the default value of character.
Ex: char letterA = 'A';

This table represents the default values of the data types:

Data Types
Values
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0d
char
'\u0000'
boolean
false
String
null

Reference/Object Data Types:
  • These variables are created using defined constructors of the class. That are used to access the objects. These variables are declared to be specific type that can not be changed.
Ex: Student, Employee,,,
  • Class objects and various types of array values are comes under the reference variables.
  • Default value of any reference variable is 'null'.
  • Reference variables are used to refer to any object of the declared type or any compatible type.
Ex: Animal animal = new Animal(“man”);

Literals in java:
  • A literal is a source code representation of fixed values. Literals are represented directly in your source code without requiring competition.
  • Literals can be assigned to any primitive data types.
Ex: byte b = 68;
char a = 'A';
byte, int, short and long can be created from int literals. Value of type long that exceeds the range of int can created from long literals.
  • Decimal: Base 10, whose number consists of the numbers 0 through 9.
  • Hexadecimal: Base 16, whose digits consists of the 0 through 9 and the letters A through F.
  • Binary: Base 2, whose digits consists of 0 and 1.
Ex:
//The number 26 in decimal
int decVal = 26;
//the number 26 in Hexadecimal
int hexVal = 0x1a;
//the number 26 in binary
int binVal = 0b11010;
  • Floating point literals of type float if it ends with the F or f.
  • Otherwise it's type is double.
Ex:
double dVal = 234.2;
float f Val = 122.4f;
  • String and char type of literals can contains any Unicode characters.
Ex: char a = '\u0001';
String a = \u0001
Note:
Java language supports few special escape sequences for String and char literals.
Ex: \n, \r, \t, \b, \s,',\\,etc...

below table represents the literals and what the actions that performs:

Notation
Character represented
\n
New Line
\r
Carriage return
\f
Formfeed
\b
Backspace
\s
Space
\t
Tab
\”
Double Quotes
\'
Single Quotes
\\
Back Slash
\ddd
Octal Charecter
\uxxxx
Hexadecimal Unicode Character

    Comments

    Post a Comment

    Popular posts from this blog

    Hibernate auto increment with example

    how to count the page views by using JSP

    Multithreading in java with example

    How to retrieve data from table by using JDBC with example

    Prime, Fibonacci and Factorial number with example in java

    How to insert images into database using JDBC?

    How to sort list of objects in java with examples

    String interview questions and answers

    Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

    Store file into table by using JDBC with example