Sunday, September 11, 2011

2. Main() and command line argument

- The compiler will check for the main method declaration during the run-time of the program and throws the Run-time Exception

Example:

class test
{

// Some code without main method

}
> javac test.java //done
> java test // Run-time Exception NoSuchMethod found Error

Syntax of Main() method

public static void main(String[] args)

- public: to call this method from anywhere by JVM.
- static: this will make the main method to be called without creating instance of the main().
- void: method won't return anything to the JVM.
- main: it is a thread which the JVM looks first and is the entry point for the program. This method only configured in JVM.
- String[]:  that is java will accept every thing as a string array.
- args: This defines a parameter to the main method and will accept command line options passed by the user while invoking the progam.

simple example:
class best
{
public static void [b]main(String[] args)
{
System.out.println("good");
}
}

- if we are performing any change to the above syntax, we will get run-time exception saying no such method Exception

Saturday, September 10, 2011

1. What are Limitations of Object Arrays and define arrays?

- An Array is a indexed collection of fixed number of homogenous data types. 

- The main limitation of array is fixed in size i.e, once the array is created then there is no chance for   increasing/decreasing of the array as per our requirement

Different types of Arrays:
1. Single Dimensional  Arrays
 Syntax:
int[] a ; //Recommended to use, because name is clearly separated from the type
int a[];
int []a;

We are no allowed to specify the array size during declaration, Compile time error
i.e., int[6] a; // compile time error


2. Two Dimensional Arrays
Syntax:
int[][] a;
int a[][];
int[] a[];
int [][]a;
int[] []a;
int []a[]; // All these are possible array declarations

3. Three Dimensional Arrays
ll'y for the three dimensional arrays declarations

Few more things:

- int[] a=new int[6]; //valid declaration
- int[6] a=new int[]; // invalid declaration compile time error
- int[] a =new int[0]; // valid declaration but cannot store elements
- int[] a = new int[null]; // invalid declaration - CE: in compatible types
- int[] a = new int[-6]; // invalid Declaration - RE: Negative Array Size Exception
- When ever single dimensional array is present , we can replace it var-arg parameter. and viceversa is not possible