Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » 1st experiment with args in main method(To check how the main method accepts arguments & interprets them)
1st experiment with args in main method [message #1768231] Sat, 15 July 2017 07:50 Go to next message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
I wrote a HW.java program as follows:
public class HW {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		System.out.println(args);
		System.out.print(args[0]);
		System.out.print(args[1]);
		System.out.print(args[2]);
		System.out.print(args[3]);
		System.out.print(" " + args[0]);
		System.out.print(" " + args[1]);
		System.out.print(" " + args[2]);
		System.out.print(" " + args[3]);
	}
}


I passed the following arguments in the identical formatting from
Run → Run Configurations → Arguments
The arguments I had written in the Arguments were exactly with line breaks:
bob harry tom john
ass
goat
Sam


The Output was:
Hello World!
[Ljava.lang.String;@7c6768
bobharrytomjohn bob harry tom john

What is this output doing here:
[Ljava.lang.String;@7c6768

I'm trying to understamd how the Main method and the System.out.print function. The argument doesn't take strings after line breaks.

How can I prove that the main method takes in only a single argument in the form of of an array of elements of type string? Or prove otherwise?

Please refer to this page: Lesson: A Closer Look at the "Hello World!" Application

Quote:
The main method accepts a single argument: an array of elements of type String.

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application.

[Updated on: Sun, 16 July 2017 04:10]

Report message to a moderator

Re: 1st experiment with args in main method [message #1768258 is a reply to message #1768231] Sun, 16 July 2017 05:29 Go to previous messageGo to next message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
Jesper de Jong came very close to giving me what I wanted, at this post, Can one not learn Java without knowing C? And C without machine assembly languages?, here:
Jesper de Jong
"args" is just a variable name. It's presumably a shortened form of "arguments". It's a single variable, of the type String[] (array of String objects). The name isn't really important. In fact, you can name it whatever you like. Even
public static void main(String[] joe)
would work.
The important part is that the method is named main and that it is public static void and that it takes one argument, which is a String[]. The name of the argument variable is not important.

What I wanted was as follows: That the method main has the functionality of having one single argument variable, "args" or "Joe" or whatever, passed on to it from the input terminal via
java "path_to_class_directory/class_name_without_the_extension_name" argument(s)
or explicit still:
java "path_to_class_directory/class_name_without_the_extension_name" argument_1 argument_2 argument_3 ... argument_n

The variable is an Array, containing one or more elements of the type |string|s assigned to it by the user via single-spaced input(s) on the console as argument(s) mentioned above.

Added later after Dr. Ed Merks's reply: Most important: Being a novice I did not understand that the //* public static void main(String[] joe) *// is a pattern created to tell the system that Joe is the array type variable of the form string using this form //*(String[] joe)*// and that, //*String[]*// and //*Joe*// are not two different unrelated entities within the main function, but related! In brief, I found an association which I did not know existed!

But he took a different path: he chose to go up to how electrons behaved within the semiconductors, etc.

This was a case of narrowly missing the appropriate role of a teacher -- to try and listen what the learner is trying to convey.

One has to have the empathy to understand the learner is trying to find his way through an untraversed maze/labyrinth, So the approach should be to allay the anxiety and the resultant apparent aggression of the learner.

The pupil may not be by nature aggressive, but aggressive when he finds that he is not being heard. Arguments by the learner doesn't have to imply that he is not listening or being impolite. He is simply trying to question and process the answers to build his own mental schema.

I chose to record my observation here so that a new learner might stumble on to this thread and understand what the new learner needs to find out.

As an aside, a computer might as well be designed by Relay-switches, and there is a book precisely on this, which is very enlightening. So Dr. Jong, we need not go upto the electrons' behaviour within a semi-conductor.

I am not making this into a game of one-upmanship. I am simply sharing my thoughts. Let us not make this into a battle of "your vs. my balls", as the male hormone Testosterone has the propensity to enhance its recipients' aggressions and territorial- and mating- drives.

I still don't know why we require the main method to be "void"? Please, Dr. Jong?

Resolved the issue from the Stackoverflow post here.
public class HW {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello World!");
		System.out.println(args);
		System.out.println(args.length);

	    // In Java 8 we have lambda expressions
	    // Arrays.stream(intArray).forEach(System.out::println); */
	    // args.length.forEach(System.out::println); 
	    
	    for (int i = 0; i < args.length; i++) {
	    	   System.out.print(args[i] + ", ");
	    	}
		
	}
}

Output:
Hello World!
[Ljava.lang.String;@1f33675
7
bob, harry, tom, john, ass, goat, Sam,  


But yet to understand what was that output doing?
[Ljava.lang.String;@1f33675

In the end, since the Eclipse forum BB editor is far better for later editing and improving of posts a copy of this post, continuously improved, will sit here to be perused by trepid learners.

[Updated on: Sun, 16 July 2017 15:34]

Report message to a moderator

Re: 1st experiment with args in main method [message #1768263 is a reply to message #1768258] Sun, 16 July 2017 08:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
In the end, this is not an appropriate forum for this discussion. This forum is for asking questions about using Eclipse and your questions barely touches upon that.

Java requires a main method because as suggested in http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html "The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class's main() method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter." So to run anything at all, there must be a class with a main method to start the processing. But you're asking for a design justification for why "it must not return any value" (and are pontificating about the role of a teacher verses the role of a learner, which I personally find to be annoying at best, and seems to be a criticism of the helpful people who could just ignore you).

So one answer is that it's simply specified this way. Another answer is that if it were to return a value, that value isn't used so it would be pointless to specify a return type other than void. If you want to control the exit code of the process, you use java.lang.System.exit(int). That could be the last line of your main method, or it could be anywhere in the application; when called it terminates the application at that point. So one could argue that this design is a much more convenient way to determine the process exit code than having it be returned by the main method.

Note also that the way a command line processor handles splitting command line arguments into individual String values passed to the main method is highly dependent on the command line processor itself, i.e., bash, sh, cmd, or whatever shell/terminal/processor is being used. For example, supposed we have this program:
public class Test
{
  public static void main(String[] args)
  {
    for (String arg : args)
    {
      System.out.println(arg);
    }
  }
}
and these "Program arguments" in the Arguments tab of Eclipse Run/Debug Configuration:
a
b
c
"d e
"
"\"h i\""
The output will be
a
b
c
d e

"h i"
So you can see that this particular command line processor uses "-pairs to join what would otherwise be separate arguments into a single argument. And it allows to specify \" so that it's still possible to specify a "-value as the value in the argument itself.

And finally, you also seem to be asking about the fact that the toString method when applied to an array-type value looks ugly:

https://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work-for-an-array

Hopefully you won't be asking "why is it that way" because that will be like asking why is the sky blue the answer might involve discussions about light waves and photon absorption. If you're still tempted to ask, try running this example to see why recursively printing nested contents (of a list in this case) is problematic:
import java.util.ArrayList;
import java.util.List;

public class Test
{
  public static void main(String[] args)
  {
    List<Object> a = new ArrayList<>();
    List<Object> b = new ArrayList<>();
    
    a.add(b);
    b.add(a);
    System.out.println(a);
  }
}


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: 1st experiment with args in main method [message #1768264 is a reply to message #1768263] Sun, 16 July 2017 09:17 Go to previous messageGo to next message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
In the beginning, I improved my earlier post following your inputs.

Ed Merks
In the end, this is not an appropriate forum for this discussion. This forum is for asking questions about using Eclipse and your questions barely touches upon that

I forgot to say -- and I am really sorry for this omission -- that only because of Eclipse I was able to solve this issue myself with a little positive help!
Without the Eclipse IDE I would have left learning Java!
I think this is the greatest advertisement for IDE builders and supportive and humane forum maintainers!
Ed Merks
... But you're asking for a design justification for why "it must not return any value" ...

Is this portion a reply to my portion?:
Ed Merks

Myself, earlier
... I still don't know why we require the main method to be "void"? Please, Dr. Jong? ...

... and are pontificating about the role of a teacher verses the role of a learner, which I personally find to be annoying at best, and seems to be a criticism of the helpful people who could just ignore you ...

BTW, you have given me the reply why it needs to be "void"! here by quoting the appropriate portion:
Quote:
... as suggested in .../technotes/tools/windows/java.html ... The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter ...

There! You, yes, you are the teacher I was looking for!
I will explain to my helper that I am a novice learner who is just learning to understand a programming language from the basics. So it is natural for me to be agitated (irritated with myself) when I see that I don't have the language to explain to others what I need and what I mean.
Which is why I have kept this record for posterity, for new trembling learners like me to read and have a partial understanding at least of the reasons why it is as it is!

Ed Merks
... for (String arg : args) ...

What does this part do?

Ed Merks
... And finally, you also seem to be asking about the fact that the toString method when applied to an array-type value looks ugly: ...

This part I could not locate in my query or in my memory of list of questions to be asked. Please direct me to the part, I implore you!

Thanks and regards,
And your advent, better late than never! Ah! You made my day!

[Updated on: Sun, 16 July 2017 15:53]

Report message to a moderator

Re: 1st experiment with args in main method [message #1768266 is a reply to message #1768264] Sun, 16 July 2017 10:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
" for (String arg : args)" is https://blogs.oracle.com/corejavatechtips/using-enhanced-for-loops-with-your-classes

You have "But yet to understand what was that output doing?" referring to "[Ljava.lang.String;@7c6768" in your output caused by "System.out.println(args);" in your code.

I'd suggest you learn to use the debugger:

http://www.vogella.com/tutorials/EclipseDebugging/article.html

You can "Step Into" to see how things work and hence why the work the way they do.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: 1st experiment with args in main method [message #1768267 is a reply to message #1768266] Sun, 16 July 2017 10:47 Go to previous messageGo to next message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
I intentionally used that code: System.out.println(args); to see what it appeared to the machine!
Now, what does that output code "[Ljava.lang.String;@7c6768" mean?
And thanks for the direction to " for (String arg : args)" !

[Updated on: Sun, 16 July 2017 11:08]

Report message to a moderator

Re: 1st experiment with args in main method [message #1768270 is a reply to message #1768267] Sun, 16 July 2017 12:06 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
I already gave you a link related to the toString representation of an array: https://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work-for-an-array

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: 1st experiment with args in main method [message #1768271 is a reply to message #1768270] Sun, 16 July 2017 13:03 Go to previous message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
I did not check because I did not use toString method. I will check as you want me to.
...
[Later]
Checked and ran this code:
import java.util.Arrays;
public class HW_import_java_util_Arrays {
	public static void main(String[] args) {

		System.out.println("Hello World!");
		System.out.println(Arrays.toString(args));
	    	}
	}

I noted the Stackoverflow comment on the toString() method with the
import java.util.Arrays
part.
Thanks!

[Updated on: Sun, 16 July 2017 15:16]

Report message to a moderator

Previous Topic:M package, R package
Next Topic:Combined Java and C++ code compilation
Goto Forum:
  


Current Time: Thu Apr 25 09:11:13 GMT 2024

Powered by FUDForum. Page generated in 0.09140 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top