Hi,
I have a few basic doubts in java .

Statment:
class MainExample1 {public static void main(String[] args) {}}
class MainExample2 {public static void main(String []args) {}}
class MainExample3 {public static void main(String args[]) {}}


1.What is the difference between the above three statments.?
2.Why main method should be declared as void?
3.Why should not we change the String type to int or float in main method


Thank you,

With Regards,
Prem

  1. The difference is in the name of the classes.
  2. The Java language defines the signature (format) of the only main method in a class as
    public static void main(String args[]) {} meanwhile
    the name of the array of String may be given differently, e.g.
    public static void main(String a[]) {},
    public static void main(String bargs[]) {},
    public static void main(String arg[]) {},
    ….
    The following program is taken as an example:

    public class MainExample1 {
    public static void main(String a[]){
    for(int i=0;i<a.length;i++)
    System.out.println("The " + (i+1) + "arguemnt: " + a[i]);
    }
    }

The String array a is used to receive String arguments when the java program is started to execute in the following command on DOS:
Java MainExample1 Dani Web Web Master

The following output on DOS is shown accordingly:
The 1arguemnt: Dani
The 2arguemnt: Web
The 3arguemnt: DaniWeb
The 4arguemnt: Master

Thanks for your answers .Can you tell me the balance answers.

2.Why main method should be declared as void?
3.Why should not we change the String type to int or float in main method

As far as I know,
2. On DOS, the command “java” calls the main method only to interpret the code in the main method. When the main method is terminated (executed completely), no return value is requested. Hence the void is always requested.
3. Any arguments received when starting an interpretation of a Java program can be stored only in terms of the instances of the class String. The value of any other types, such as int, doudle, and boolean, can be created via parsing the String instances accordingly. Therefore, only the String instance is appropriated for the type of elements of the array.

Thanks for your clear explanation i understand lot in your answers for 2nd one.

Can you give me some detail about the 3rd one .

The following program shows the String instances are converted into different values in boolean, double, and int.
import java.lang.*;
public class Input{
public static void main(String args[]){
if (Boolean.parseBoolean(args[0])) {
System.out.println("The first argument is in boolean: " + Boolean.parseBoolean(args[0]));
System.out.println("The second argument is in double: " + Double.parseDouble(args[1]));
System.out.println("The third argument is in int: " + Integer.parseInt(args[2]));
}
}
}
The executing command:
Java Input true 3.1415 255
The output:
The first argument is in boolean: true
The second argument is in double: 3.1415
The third argument is in int: 255

Also see “http://www.daniweb.com/forums/thread299093.html” where the client is asked to input an integer as the size of an array in int.
-------------------
/* The following program is written to receive a size of an int array that
* assigned by random values varying between 0 to 255 [0-255].
* Then the array will be sorted in ascending order by the bubble method.
*/
import java.lang.*;
public class MainExample1 {
static void bubble_sort(int a[]){
int i,j,t, n=a.length;
boolean change;
for(i=n-1,change=true; i>0 && change ;--i)
{
change=false;
for(j=0;j<i;++j)
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
change=true;
}
}
}

public static void main(String args[]){
int n= Integer.parseInt(args[0]);
int d[] = new int[n];
for (int i=0; i<n; i++)
d = (int)(Math.random()*256);
bubble_sort(d);
for (int i=0; i<d.length;i++)
System.out.print(d + " ");
System.out.println();
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.