Skip to main content

Java Primitive Data types & Wrapper Class Tutorial

 JAVA PRIMITIVE TYPES AND WRAPPER CLASSES :

Object class, which is stored in the java. lang package is ultimate super class of all classes. and below are the methods of Object class it means behaviour of Object class as mentioned in my previous post. Just know these methods will be more useful as we will go more deeper.

  • Object getClass(). : returns Class class object
  • int  hashCode() : returns the hashcode number for object being used.
  • boolean equals(Object obj) : compares the argument object to calling object.
  • Object clone() :  Creates copy of invoking object
  • String  toString() : returns the string representation of invoking object
  • void  notify() : wakes up a thread, waiting on invoking object's monitor.
  • void  notifyAll() : wakes up all the threads, waiting on invoking object's monitor.
  • void  wait() : causes the current thread to wait, until another thread notifies.
  • void  wait(long,int) : causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies.
  • void finalize() : It is invoked by the garbage collector before an object is being garbage collected.

You guys should now be able to understand the method signature along with return type as explained in my previous post JAVA BASIC OVERVIEW .

Now, let's understand how java understand variable type.

In Java Syntax , a variable can be defined as below : 

       AccessSpecifier TypeOfVariable ReferenceName;

  Example :   public int input1; 

    Here public is the Access specifier and int is the type of variable and input1 is nothing but a name to this variable which can be anything , only thing we need to remember that while updating or accessing we need to use the same name. 

  I already explained about access specifier in my previous post JAVA BASIC OVERVIEW, so in this post we will be discussion only  about variable types.

   In java for variables mainly we use primitive datatypes, which are not object and they doesn't have any methods, because of this only java is not fully object oriented programming language .

 There are 8 primitive types in java : boolean, char, byte, short, int, long, float and double. Below is Wrapper Class mapping for each primitive types. We need wrap our primitive type to their respective wrapper class to use the Object class methods.


 Primitive types :  boolean, byte, short, char, int, long, float, double

Wrapper classes : Boolean, Byte, Short, Character, Integer, Long, Float, Double

boolean : Can be either true or false

byte : Can store one byte (8 bit) like capital character ASCII value starts from 65 so if we store A in byte 

example : byte input1='A';     it will print 65

                byte input1='C';     it will print 67

char : char is 16-bit Unicode character , Can store only one character at a time like char input1='C';  

short : short data type is a 16-bit integer. It has a minimum value of -32,768 and a maximum value of 32,767. So we can't store value more than 32,767.

int : int is a numeric data type and is of 32 bit and can hold minimum value of -2^31 and a maximum value of 2^31-1

long : long is also a numeric data type of 64 bit.

float : float is numeric data type for floating point like 12.65 , 23.98

double : double is also numeric data type for floating point it has double has 2x more precision then float.

  • Non-primitive variables can be String , Arrays etc.

Variables scope can be of 3 types : Local, instance and static.

1) Local Variable : Variable declares inside method or enclosed brackets has scope till inside the methods or brackets it can't be use or recognise outside of methods or brackets.

2) Instance Variable : A class can have multiple variables declared inside , all these variables belongs to the instance of the same class. Once the object will be created of class instance variables will also come into the picture.

3) Static Variable : Already explained in my previous post JAVA BASIC OVERVIEW, static means it belongs the Class not with the object of class. So it can be use by without object creation and can be accessible to all static blocks.



  1. BASIC OVERVIEW OF JAVA (SESSION 1)
  2. PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
  3. JAVA OPERATORS (SESSION 3)
  4. LOOPS IN JAVA (SESSION 4)
  5. NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)

Comments

Popular posts from this blog

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT