Skip to main content

Data Types in Java

Information that program receives or processed outputs are stored in memory location with some pointer or name to access it, known as Variable. value of variable is prone to change any number of times in lifetime of program.

Information can be sequence of character(Strings), numbers, or true or false boolean values.

In Java, when you declare a variable you need to declare the type of data or information that it will hold, so Java compiler can understand the what type of information will store in declared variables pointing memory location an in turn JVM allocates that much size of memory,  as every data type has its own limitation or you can say size of data that it can hold.

We will see soon below what different types of data types that Java support and their data size limitations.

In Java there are two data types available:
  1. Primitive data type
  2. Reference data type.

Primitive Data Type:

In Java there are eight primitive data types available to serve different types of data natively. primitive data types also called native data types and are reserved as keywords in Java language.primitive data type store only primitive values, you might seen same data types in other languages like C/C++ or python etc.

Boolean Data Type:

  • boolean:

    • 1 bit information
    • Default value : false
    • Typically boolean data types use to represent flags in programs, mostly use in if...else if...else construct
    • e.g. boolean isDoorOpen = true;

Character Data Type:

  • char:

    • Java uses Unicode system than ASCII for char data type
    • Size of char is 16 bit (2 byte) unicode character.
    • Default value : '\u0000'
    •  \u0000 is lowest range of character in Unicode character system
    • Enclose char with single quote
    • e.g. char a = 'a' ; char A = 'A'; 

Number Data Type(Non floating point):

  • byte:

    • 8 bit (1 byte) signed twos complement number
    • Default value :  0
    • Minimum value : -128 (-2^8-1)
    • Maximum value : 127(2^8-2)
    • byte data type is smallest data type available in Java, use it wherever only when you feel confident that the data will be in range of -128 to 127, using larger data type to store small data is mere just waste of memory.
    • if you try to store number larger than max limit then overflow effect will happen, soon we will see with example how overflow effect happens.
    • e.g. byte numberOfTyres = 4 , e = -27;

  • short:

    • 16 bit (2 byte) signed twos complement number
    • Default value : 0
    • Minimum value : -32,768 (-2^16-1)
    • Maximum value : 32, 767(2^16-2)
    • if you try to store number larger than max limit then overflow effect will happen, soon we will see with example how overflow effect happens.
    • e.g. short sum = 21000;

  • int:

    • 32 bit(4 byte) signed twos complement number
    • Default value : 0
    • Minimum value : -2,147,483,648 (-2^32-1)
    • Maximum value : -2,147,483,647 (-2^32-2)
    • if you try to store number larger than max limit then overflow effect will happen, soon we will see with example how overflow effect happens.
    •  e.g. int numberOfNodes = 3700000;

  • long:

    • 64 bit(8 byte) signed twos complement number
    • Default value : 0L or 0l
    • L or l represent long against number.
    • Minimum value : -9,223,372,036,854,775,808 (-2^64-1)
    • Maximum value : -9,223,372,036,854,775,808 (-2^64-2)
    • be careful when using long data as its largest data type available in Java, double in size than int. use it when needed
    • e.g long  dailyVisitors = 33444343234444L;

Number Data Type(floating point):

  • float:

    • 32 bit(4 byte) single precision IEEE-754 floating point number
    • Default value : 0.0f
    • f or F represent float against number
    • e.g. float s = 33.44f, d = 3.14F

  • double:

    • 64 bit(8 byte) single precision IEEE-754 floating point number
    • Default value : 0.0d
    • d or D represent double against number
    • e.g double d = 13.45d, e = 44.12D
Overflow effect:
Output:
So far, we have seen about primitive data type, i will continue for Reference data type in more details in next post. 
Thanks for reading post!



Comments

Popular posts from this blog

Lambda expression in Java 8

Behaviour Parameterisation …pattern which allows you to write such a flexible code which can cope up with any requirement change. Now you may wondering why I am talking about this weird sounding pattern instead lambda expression, right? Well, let me tell you that we will soon get to know about lambda expression in detail but understanding Behaviour Parameterization  pattern will definitely help you thoroughly how lambda expression is useful to write concise and succinct code and you will start feeling power of lambda expression which is one of prime feature of java 8 release. Lets say, we have list of Books and you need to find Books with greater than 200 pages. You could write a method getBooksGreaterThanPages(): Using this method is simple enough, what if you need to find books smaller than 200 pages? Or Books above particular prices? Or Books from particular author or publications? Or even worst book with particular publication with certain pages and below particular pric

Default Methods in Java 8

Adding new methods in interface s always break the implementation in class. So Java 8 come up with new feature which propose a mechanism to extend existing interfaces by adding new methods without breaking existing implementation thus, achieved the backward compatibility. Default methods is the feature which helps you to extend existing interfaces . If added method in interface provides the default implementation then no implementing Class get affects.   Default methods enable you to add new functionality to the interfaces of your libraries and ensure  binary compatibility with code written for older version of those interfaces. An implementing class can override the default implementation defined in implemented interface  and add its own implementation. To understand it with example, lets say there is ABC company which wants to launch their smart Radios in market, so they have developed  SmartRadio interface(contract) and ABCRadio final product implements all the fu

Reference data type in java

Unlike Primitive data types in java, Reference data type store the address of object in memory which they refer to, than actual value. lets say, Car class is already defined, and its object is created in memory then variable declared with type Car becomes the reference type which will be holding address of car object to which it refers. Car car = new Car(); int noOfDoors = 4; Car myCar = car; // copy the car reference into myCar reference . Here, variable car actually contains the address and variable noOfDoors will contain actual 4 value. See below diagram for more explanation: Reference data type holding address of object in heap memory and primitive data type holds actual primitive values Available reference types in java: Class type Interface type array type e.g. String name = new String("Java"); List listOfNames = new ArrayList(); Students topRankers[] = new Student[10]; Remember though you know now that Reference data type holds