Skip to main content

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

    Multithreading in java with example

    Multithreading  is one of the most important concept in core java. In this article we will learn what is multithreading? , what is the use of it? and What is the use of Synchronization and when to use it?  with detailed examples. At a time, two or more threads are accessing the same object is called as Multithreading  in Java .  First, we will create two threads for two objects. It is also possible to run two or more threads on a single class object. In this case, there is a possibility to get unreliable results. If the two threads are perform same task, then they need same object to be executed each time. For your better understanding, take an example of any reservations like, railway, movie ticket booking,etc. Let us think only one berth is available in a train and two passengers are asking for that berth. The first person has sent a request to allocate that ticket/berth to him. At the same time, the second person also sent a request to allocate that ...

    JNDI configuration for Tomcat 9 with Oracle

    In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...

    Git installation for AngularJS 2 in Windows 10

    Download Git latest version from https://git-scm.com/downloads or you click on the below link to download directly for windows https://git-scm.com/download/win . Once download completes, click on executable file to start installation process and choose Yes to allow the software installation in windows 10. Click on Next button to continue further installation. Browse the isntallation directory and click on Next button to continue. Select the list of components which you want to be installed and click on Next button to proced further installation. Type the shortcut name for Start menu and click on Next button. Select how you want to use the Git and click on Next button. For Windows no need to change anything, let it be the default one. Choose the Use the OpenSSL library and click on Next button. Select how should Git treat line ending in text files and click on Next button. Select which terminal emulator to use with Git and click on Next button. Configure extr...