Value types and reference types:

Value types

->All the primitive data types in java belongs to value types Valuetype
->When you pass value types to a function, it will pass value of that variable to the function and not the address

Reference types:

->When you pass reference type to a function then it will pass address of that object and not the value. ReferenceType
Note: When we pass value types to a function and if the called function modifies the value it will not reflects the calling function.
         class Modify
         { 
           public void swap(int i,int j)
           {
             int temp=i;
             i=j;
             j=temp;
           }
          }
          public class MainClass
          {
           public static void main(String[ ] args)
           {
             int i=10;
             int j=20;
             Modify m= new Modify();
             m.swap(i,j);
             System.out.println(i);
             System.out.println(j);
           }
          }
ReferenceType1
Note: When we pass value types to a function and if the called function modifies the value it will not reflects the calling function.
 
          class Modify
          {
             public void swap(int i, int j);               
             { 
              int temp=i;
              i=j;
              j=temp;                                  
             }                                         
           }                                             
             public class MainClass
             {
                public static void main(String[] args)
               {
                 int i=10; 
                 int j=20;
                 Modify m = new Modify();
                 m.swap(i,j);
                 System.out.println(i);
                 System.out.println(j);
               }
              }
The above program will not work. Instead of considering value type make use of reference type. ReferenceType2
Write a program:
      class Modify
       {
        public void swap(Val v)
        {
           int temp=v.i;
           v.i=v.j;
           v.j=temp;
        }
     }
ReferenceType3
Write a program:
         class Modify
          {
             public void incr (Val v)
             {
             v=new Val();
             v.i=200;
              }
            }
           class Val
            {
              int i;
            }
           public class MainClass
           {
             public static void main(String[ ] args)
              {
                Val v=new Val();
                v.i=10;
                Modify m=new Modify();
                m.incr(v);
                System.out.println(v.i);
               }
            }
->Value types will be stored in stack and reference types will be stored in heap most of all times. ->Strings will be stored in string constant pool. Write a program: ReferenceType4
Write a program: ReferenceType5
Write a program: ReferenceType6