-3
public class Hello {
    public static void main(String args[]) {
        System.out.println(" This is awesome "+args);
    }
}

In the above code, why it is mandatory to mention String args[] in main() and why do we get "[Ljava.lang.String;@174e5edb" as output when we print it?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
DGM
  • 21
  • 5
  • The other half of your question (not part of the duplicate question) was already asked here: [*Why main method in Java always needs arguments?*](http://stackoverflow.com/q/10783190/2991525) – fabian Oct 19 '15 at 12:20

2 Answers2

1

why it is mandatory to mentioned "String args[]" in main()

That's because you could pass parameters to the application at startup which then are the content of that array. As an alternative you could use varargs, i.e. String... args.

why we get "[Ljava.lang.String;@174e5edb" as output when we print it

That's the way the toString() method is implemented for arrays (in fact for Object). Use Arrays.toString(args) instead.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

The signature of the main method is simply defined the way it is. The String[] is used to pass parameters to the program. "[Ljava.lang.String;@174e5edb" is the type of the value, which is String[] ("[Ljava.lang.String"). Followed by the position in the heap ("@174..."). This is the default way of representing objects in java. If you want to print the content of the array, you can use System.out.println(Arrays.toString(args));.