2

In java, as per the Java Language Specification 12.1.4 the main method should be declared as follows:

public static void main(String[] args){
     //code of program
}

With the use of String[] args we can get command-line arguments. Sometime those command-line arguments needs to be converted to appropriate type. suppose if you have supplied any number 123 as an argument and you want to convert it to int then we can use one of the following conversion methods:

int n1=Integer.valueOf(args[0]);
//or
int n2=Integer.parseInt(args[0]);

But, if java could have provided use of Objects[] args instead String[] args then we can easily convert from one type to another, for this example conversion would take place as follows:

public static void main(Object[] args){
     int n1=(Integer)args[0];
}

I'm not getting this why java have used String[] args and not Object[] args?

3
  • 4
    Simple. How can you pass a Object via your command line? Commented Nov 26, 2013 at 3:25
  • The command line provides strings. How would you distinguish between a string and an integer? Just being a numerical value is not enough to deem it an integer. Commented Nov 26, 2013 at 3:25
  • 2
    Ravi - It is good to think beyond the boundaries of what we do currently. FYI - Powershell/.NET/C#/Cmdlets can actually do something like this msdn.microsoft.com/en-us/library/ms714395%28v=vs.85%29.aspx crazorsharp.blogspot.com/2009/12/… Commented Nov 26, 2013 at 3:36

7 Answers 7

7

Java is designed with portability in mind, so that Java programs would be invoked by diverse operating environments. Having the environments pass Java objects to main would put too much restrictions on the caller system, because that system would have a task of "understanding" the input and converting it to Java objects.

Strings, on the other hand, are universal: all operating environments have them. Therefore, Java specification required strings, knowing that your Java program would be able to process and understand them better than an external environment.

Sign up to request clarification or add additional context in comments.

Comments

3

This is simple because the args came from outside java environment, it is from the Operating System and as such it can't create Java Objects it can only pass Strings

To call a java program from a console it would be something like:

c:\java MyProgram 123

There is no way to java know that the OS is passing an object it is just a string.

Comments

2

whenever you read from Console it is by default String that is the reason they have made it as String[] array .

if you use :

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String str = br.readLine();

String variable str by contains String because br.ReadLine() gives you string

Comments

1

Because you cannot pass an object via a command line argument. Instead, it's natural to interpret the passed arguments as Strings.

Comments

1

public static void main(String[] args) is a main method that runs when you execute the Class file $java MyClass input1 input2.

Comments

1

Because the operating system doesn't pass variable typed arguments to the JVM. If you had Object[] args the JVM couldn't really infer their types like that without the Operating System. It's also the general contract (in Java of course) of "standard" (e.g. ancestral C/C++) Main function.

Comments

1

May be because "String" is an array of char sequence which takes continuous memory, and which can be entered using standard keyboard. Otherwise how main method will handle type casting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.