Command line arguments

  • The group of strings what we are passing as an argument to the main method from the console or command line are known as command line arguments.
  • Every command line argument would be accepted by the JVM in the form of a String object.
  • JVM will always executes main method by passing object of an array of strings.
  • JVM would never execute main method by passing null
  • JVM always executes main as a thread. 

 

Program on command line arguments:

package com.instanceofjavaforus;

public class CommandLineArguents {

    public static void main(String args[]){
      
        System.out.println("Your first argument is: "+args[0]);
    }
  
}

  • >javac CommandLineArguent.java

    >java CommandLineArguents

    OutPut:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at com.instanceofjavaforus.CommandLineArguents.main(CommandLineArguents.java:8)
  • >javac CommandLineArguent.java

    >java CommandLineArguents  instance

    OutPut:
    instance

Enhanced For loop

  • Using enhanced for loops we are just simplify the regular for loop and operation of for loop.
  • Enhanced for loops are applicable to any object that maintains group of elements and supports indexes.
  • Enhanced for loops are used where ever it is required to access all the elements from the beginning till ending automatically from array or from any object representing group of elements along with index.

Enhanced for loop to iterate Array:

 package com.instanceofjavaforus;

public class EnhancedForloop {

    public static void main(String args[]){
      
        int x[]={1,2,3,4,5,6};
      
        for(int i:x){
            System.out.println(i);
        }
      
    char ch[]={'a','b','c','d','e'};
  
    for(char i:ch){
        System.out.println(i);
    }
  
    }
}

output:

1
2
3
4
5
6
a
b
c
d
e

Enhanced for loop to iterate ArrayList:


package com.instanceofjavaforus;

import java.util.ArrayList;
import java.util.Collection;

public class EnhancedForLoopList {

    public static void main(String args[]){
       
    Collection<String> nameList = new ArrayList<String>();
    nameList.add("Ramu");
    nameList.add("Geetha");
    nameList.add("Aarya");
   nameList.add("Ajay");
    for (String name : nameList) {
      System.out.println(name);
    }
    }
}

Output:

Ramu
Geetha
Aarya
Ajay

Difference between arraylist and vector

     
  • Vector was introduced in  first version of java . that's the reason only vector is legacy class.
  • ArrayList was introduced in java version1.2, as part of java collections framework.
  • Synchronization and Thread-Safe:
  • Vector is  synchronized. Synchronization and thread safe means at a time only one thread can access the code .In Vector class all the methods are synchronized .That's why the Vector object is already synchronized when it is created .
  • ArrayList is not synchronized.

How To Make ArrayList synchronized:

  • We have direct method in collections class to make Arrraylist as Synchronized.
  • List li=new ArrayList();
  • Collections.synchrinizedlist(li);

Automatic Increase in Capacity:
  • A Vector defaults to doubling size of its array.
  • ArrayList increases it's size by 50%.
Performance:
  • Vector is slower than ArrayaList.
  • ArrayList is faster than Vector.




Boxing and unboxing in java

  • The concept of representing the primitive data values in the form of its equivalent and corresponding wrapper class objects is known as "boxing"

                Integer x= new Integer(3);
               Double d= new Double(1.2);
  • package com.instanceofjavaforus;

    public class wraperDemo3 {
    public static void main(String args[]){
              String s1="12";
            Integer i=new Integer(s1);
            System.out.println(i);
     
        }
    }
     
  • The concept of getting back actual primitive data values present inside wrapper class objects into its corresponding primitive data value known as "unboxing"
          Integer x= new Integer(3);
          int y= x.intValue();
  • package com.instanceofjavaforus;

    public class wraperDemo3 {

    public static void main(String args[]){
           
                String s1="12";
                Integer i=new Integer(s1);
                int x=i.intValue();
                System.out.println(x);
              }
              }
  • The concept of JVM automatically representing the primitive data values in the form of its equivalent and corresponding wrapper class objects is known as "Autoboxing".

                Integer x=3;
               Double d= 1.3;
  • package com.instanceofjavaforus;
    public class wraperDemo3 {
    public static void main(String args[]){
                  
                  Integer x=3;
                 Double d= 1.3;
                System.out.println(x);
                System.out.println(d);

                  }
            }
     
  • The concept of getting back actual primitive data values present inside wrapper class objects into its corresponding primitive data value automatically by JVM  known as "Autounboxing"
          Integer x= new Integer(3);
          int y= x;
  • package com.instanceofjavaforus;
    public class wraperDemo3 {
    public static void main(String args[]){
                  
                  Integer x= new Integer(3);
                     int y= x;
                System.out.println(x);
                System.out.println(y);
                  }
            }

Wrapper Classes


  • Using wrapper classes we can wrap the concept of object on the top of the primitive data.
  • And represent the primitive data values in the form of its equivalent objects.
  • In order to represent the 8 primitive data values in the form of its equivalent objects we have 8 wrapper classes they are
    1. Byte
    2. Short
    3. Integer
    4. Long
    5. Double
    6. Float
    7. Character
    8. Boolean
     

Uses of wrapper classes: 

  • Using wrapper classes we can represent the primitive data  values in form of its equivalent objects.
  • many time it would be required to represent the primitive data values in the form of objects
    ex: Collections, Session
  • Wrapper classes supports the functions using which we can get back the sequence of primitive data characters present inside the string class object back into its corresponding data value.
  • It has two constructors
    1. Accepts the values of primitive data
    2. Accepts the object of String class
     
  • Syntax:
        Integer num1=new Integer(12);
        Integer num2=new Integer("12");
  • To get the value form it
     int n=num2.intValue();

Integer wrapper class Example program:

package com.instanceofjavaforus;

public class wrapperDemo {

public static void main(String args[]){
    int x=12;
   
    Integer num1=new Integer(x);
    System.out.println(num1);
    num1=num1+1;
    System.out.println(num1);
    String str="12";
    Integer num2= new Integer(str);
    System.out.println(num2);
   
    int num3=num2.intValue();
   
    System.out.println(num3);
}
}
Output:
12
13
12
12

Example program 2 on Integer wrapper class:

package com.instanceofjavaforus;
public class Wraperdemo2 {
    public static void main(String args[]){
  
        String s1="12";
        Integer i=new Integer(s1);
       
        int x=i.intValue();
       
        System.out.println(x);
        String s2="abc";
        Integer i2=new Integer(s2);// no compilation error but run time error will come
       
        String s3=" 123";
       
        Integer i3= new Integer(s3);// run time error
         i3= new Integer(s3.trim());// recommended to trim
    }
}

  • String s2="abc"
    Integer i2=new Integer(s2);// no compilation error but run time error will come.
  • It accepts String class object but the string should contain only integer otherwise the constructor dont know how to represent characters gives error.
  •    String a=" 123";
  • here some times there may be a chance of having spaces in a string so it is recommended to trim the object
  •    i3= new Integer(s3.trim());// recommended to trim

Program 3 :
package com.instanceofjavaforus;

public class wraperDemo3{
public static void main(String args[]){

        String s1="12";
        Integer i=new Integer(s1);
       
        int x=i.intValue();
        System.out.println(x);

        String s2="123";
        int y= Integer.parseInt(s2.trim());
    }
}
Output:
12

Print numbers in pyramid shape


  1. package com.instanceofjava;
  2.  
  3. public class Pyramidshape {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.   int num=10;
  8.  
  9.   for (int i = 1; i < num; i++) {
  10.  
  11.    for (int j = 1; j<=num-i;j++) {
  12.  
  13.     System.out.print(" ");
  14.    } 

  15.   for (int k = 1; k <= i; k++) {
  16.    System.out.print(""+k+" ");
  17.   } 

  18.   for (int l =i-1; l >0; l--) {
  19.    System.out.print(""+l+" ");
  20.   } 

  21.    System.out.println();
  22.   }
  23.  
  24. }



 Output:

  1.  1
  2.  1 2 1 
  3.  1 2 3 2 1
  4.  1 2 3 4 3 2 1

Select Menu