What does "String[] args" contain in java? -
when run following program:
public class test { public static void main(string[] args) { system.out.println(args); } {
it prints: [ljava.lang.string;@153c375
and when run again, prints: [ljava.lang.string;@1d1e730
it gives me different output each time
so, "[ljava.lang.string;@153c375
" mean?
update: realized never answered question "what “string[] args” contain in java?" :-) it's array of command-line arguments provided program, each argument being string
in array.
and resume our regularly-scheduled answer...
args
array. see individual command-line arguments, index array — args[0]
, args[1]
, etc.:
you can loop through args this:
public class test { public static void main(string[] args) { int index; (index = 0; index < args.length; ++index) { system.out.println("args[" + index + "]: " + args[index]); } } }
for java test 1 2 three
, output:
args[0]: 1 args[1]: 2 args[2]: three
or loop if don't need index:
public class test { public static void main(string[] args) { (string s : args) { system.out.println(s); } } }
so,
"[ljava.lang.string;@153c375"
mean?
that's java's default tostring
return value string[]
(an array of string
). see object#tostring
. [
means "array", l
means "class or interface", , java.lang.string
self-explanatory. part comes class#getname()
. ;@153c375
;@
followed hashcode
of array hex string. (i think default implementation of hashcode
object
indicates in memory array located, why it's different different invocations of program, that's unspecified behavior , wouldn't use anyway.)
Comments
Post a Comment