Hot Posts

hot/hot-posts

Core Java - Fundamentals V (Var-args, main and command-line arguments)




Var-arg Methods


  • Introduced in 1.5 [eg: m1(int... x] , m1(10,20)]
  • Internally it is implemented as single dimentional array concept.
  • A method can take only 1 var-arg parameter and it should be at last incase of mixed parameters (eg : m1(int x, String... y).
  • Also it gets least priority.
  • Single dimensional array can be replaced by var-arg but reverse is not true.


Main Method
  • JVM finds the main method or gives error if  not found (NoSuchMethodError : Main)
  • Signature is fixed. Else we get (NoSuchMethodError : Main) 
         public static void main(String args[])
    • public - > to be called from anywhere
    • static -> without an existing object JVM should be able to call this method
    • void -> main method cannot return anything to JVM
    • main-> name of method configured in JVM (can be changed to anything else too)
    • Commandline arguments
  • It can be static public too, also String args[], String[] args,String []args, String... args is acceptable
  • main can have final,synchronized and strictfp modifiers too.
  • Inheritance concept is applicable, eg parent class main will be executed if child doesn't have main.
  • Overriding is also applicable but it is method hiding.Overloading is also applicable but JVM calls only String args[].


Command-line Arguments

  • Passing from command prompt.
  • Space is default separator, if we need to include space then we have to enclose in double quotes.

Post a Comment

0 Comments