When I run the following program:
public class Test
{
public static void main(String[] args)
{
System.out.println(args);
}
{
It prints: [Ljava.lang.String;@153c375
and when I run it again, it prints: [Ljava.lang.String;@1d1e730
it gives me different output each time
So, what does "[Ljava.lang.String;@153c375
" mean?
5. String[] args. It stores Java command-line arguments and is an array of type java.
String [] arguments is a java array of String objects. This means that the main function expects an array of Strings. This array of strings typically holds all command line parameter arguments passed in when the program is run from the command line.
(String… args) is an array of parameters of type String, whereas String[] is a single parameter. String[] can full fill the same purpose but just (String… args)provides more readability and easiness to use.
It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed?
Update: I just realized I never answered the question "What does “String[] args” contain in java?" :-) It's an array of the command-line arguments provided to the program, each argument being a String
in the array.
And we now resume with our regularly-scheduled answer...
args
is an array. To see individual command-line arguments, index into the array — args[0]
, args[1]
, etc.:
You can loop through the args like this:
public class Test
{
public static void main(String[] args)
{
int index;
for (index = 0; index < args.length; ++index)
{
System.out.println("args[" + index + "]: " + args[index]);
}
}
}
For java Test one two three
, that will output:
args[0]: one args[1]: two args[2]: three
Or loop like this if you don't need the index:
public class Test
{
public static void main(String[] args)
{
for (String s : args)
{
System.out.println(s);
}
}
}
So, what does
"[Ljava.lang.String;@153c375"
mean?
That's Java's default toString
return value for String[]
(an array of String
). See Object#toString
. The [
means "array", the L
means "class or interface", and java.lang.String
is self-explanatory. That part comes from Class#getName()
. The ;@153c375
is ;@
followed by the hashCode
of the array as a hex string. (I think the default implementation of hashCode
for Object
indicates where in memory the array is located, which is why it's different for different invocations of your program, but that's unspecified behavior and wouldn't be any use to you anyway.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With