• Post Reply Bookmark Topic Watch Topic
  • New Topic

To check how the main method accepts arguments & interprets them  RSS feed

 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a HW.java program as follows:


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:

The Output was:
What is this output doing here: I can understand that the arg is string type, but how do I interpret the output?

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

How can I now prove that the main method takes in only a single argument in the form of of an array of elements of type string?
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I print the entire list of strings in the array iteratively via 'if then' / 'do while' loops till the end of the array in Java?
Found a link: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array
Will check and get back!
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added the following lines to the code:
Error codes:
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found the code:

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.length);
   
    for (int i = 0; i < args.length; i++) {
       System.out.print(args[i] + ", ");
    }

}
}

Result:


Solved! Indeed, Array of elements of the type string!
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
? But I am yet to understand what was the output doing with the line of code: ? What is that no.?
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 wrote:"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
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 or explicit still:
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.

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 to 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.

Output:


But yet to understand what was that output doing?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But yet to understand what was that output doing?

1 [Ljava.lang.String;@1f33675

That is the String returned by the array's toString() method.  The compiler puts in a call to an object or array's toString() method when it needs a String for the variable being printed.
The parts of the String: [ says its an array, then the classname, then @ then a unique hex number(could be memory address)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16044
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:I still don't know why we require the main method to be "void"? Please, Dr. Jong?

Methods in Java may or may not return a value.

When a method returns a value, then you must specify the type of the value that it returns in the method declaration line. (The type could be any valid Java type: something like int, boolean or String).

When a method does not return a value, you must use the keyword void instead of the type of the return value.

The main method is the entry point of a Java program - it's where the program starts running. Java expects that the main method has a specific form: it must take one argument, which is a String[], it must be public and static and it also must not return a value, so it must be void. So, the reason why the main method must be void, is because Java expects it to be that way - it's just a standard that has been designed like that by the people who invented Java.

To learn more about how to return values from methods, see: Returning a Value from a Method
 
Campbell Ritchie
Marshal
Posts: 56202
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably the easiest way to inspect the args array:-You ask how the main method interprets the command‑line arguments. The answer is: however you tell it to interpret them. Any handling of the information is for you to decide. It is almost certainly best for you to move that sort of code out of the main method however.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:... it must take one argument, which is a String[]...

In the meanwhile, I had learnt more from the Eclipse forum where I had posted the results of my experimentation, from a person who truly understood what I wanted to know, here.
He was concerned that you might be offended and could ignore me. I told him that I will explain to you thus at this link.
Did you expect that my question will be, "Will this line not be: an array of elements of type string?" ;-)
But I saw that you used String[] which forms the part of an Array declaration for a String Array.
 
Campbell Ritchie
Marshal
Posts: 56202
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You received a good answer from Jesper de Jong who is one of our Sheriffs. You were also told on the Eclipse forum that the details of the main method were beyond the scope of that forum.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:This is probably the easiest way to inspect the args array:-

toString method has issues, I learnt just an hour back. But I won't presently be able to explain.

Campbell Ritchie wrote:You ask how the main method interprets the command‑line arguments.
I'm so sorry that I could not recall my having asked this question. A mild reminder please?! A case of retrograde amnesia because of the shock I got when I learnt about the main() method ;-)

Campbell Ritchie wrote:It is almost certainly best for you to move that sort of code out of the main method however.
I will get back to you on this part later!

Thanks!
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:This is probably the easiest way to inspect the args array:-

Yes, now I found from where you found this part! My earlier post here!
Rajib Ban wrote:...
import java.util.Arrays;
String[] array = args;
System.out.println(Arrays.toString(array));

Subsequently, I did not use this part.
So I will presently leave this part alone, and tackle it later.
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method doesn't have problems, you simply didn't write valid code.

Honestly, I think you should be working on your tone if you want to learn from people who are offering their free time to try and help you. Particularly your comments regarding Jesper and complaining about our help on other forums is very insulting, and even on the Eclipse forums your behavior was labeled annoying.

Please try and work on this.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:This is probably the easiest way to inspect the args array:-
Yes, I now checked this part:

Problematic, because otherwise, this code would have given trouble:
Thank you, Dr. Richie!
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: ... you simply didn't write valid code ...
Sir, I am a newbie!

Stephan van Hulst wrote: ... Particularly your comments regarding Jesper and complaining about our help on other forums is very insulting, and even on the Eclipse forums your behavior was labeled annoying ...
Sir, and it was I who posted the links myself! I hope you commented after following through my entire posts? It was not insulting, it was appreciative, when I admit someone's contributions.
And I only appreciated about the better Editor at Eclipse forum including the availability of editing for eternity.

Stephan van Hulst wrote:Please try and work on this.

Only today I found out the icon of PM, Sir. So you will find the desired results!
I thank and appreciate you for bearing with my actions, which will now gradually ebb.
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's move on. Did you figure out the relationship between the main method signature and the command line arguments?
 
Campbell Ritchie
Marshal
Posts: 56202
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:. . . toString method has issues, I learnt just an hour back. . . .
What issues? You can't expect helpful answers if you post vague things like this without explanation.
. . . I'm so sorry that I could not recall my having asked this question. A mild reminder please?. . .
Kindly read your t‍hread title.
 
Campbell Ritchie
Marshal
Posts: 56202
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajib Ban wrote:. . . this code would have given trouble:
. . .
No, it wouldn't have compiled as you had it.
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, it wouldn't have compiled as you had it.

Yes, it wouldn't have compiled! And I had to read to know why the code was written like you mentioned, couldn't blindly copy-paste codes!
Thanks!
 
Rajib Ban
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:... Kindly read your t‍hread title...

It was my goal, my motto for the day, not a question to the forum! And I thought about the issue, recording my experimental results and found out a solution. Anyway, let's not delve into semantics. I am feeling good because of the fruitful effort!

And yes, Dr. Hulst, I do agree with you to ignore minor issues!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!