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

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...