Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Arguments to Methods  RSS feed

 
Dj Galla
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I mentioned before, I'm trying to get a grasp on Java (coming from a PHP background). I am reading a book about it, and alot of the terms are confusing. First off, a method is basically a function (in PHP), correct?

Below is an example from my book... that takes text and converts it to uppercase.



My first question(s) regard:



So, first question, why is the STRING setup as an array? (hence the brackets [], correct?)?  Why not just take the string (i.e. "A Lanister Always Pays His Debts"), and convert that string of text? Why make it an array?
And, since it is being "imported" as an array ... does the array automatically split the text string by spaces? Is that the default?  When I check the output, I believe the first word is text[0], text[1] is the second word, text[2] is the third word...

I know this is an example to explain things ... but cant you just do something like:



------------------------------------------------------------

Regarding the second part of the code above....

Doesn't this just take the text, run it through the method, and result in the converted text?  Why do you need the following, when you should just be able to print the argument string directly?  Or is this because of the array that was created, and it needs to run through the array in order to print each part?




 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37222
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that the book is trying to teach you arrays. Yes, this could all be done as a String, but you'd learn less.

There are times when you want words. In which case you call "myString".split(" ") to get an array of words split by a space.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dj Galla wrote:... does the array automatically split the text string by spaces? Is that the default?

No, it's not a default behavior of arrays or of String objects. Unfortunately, the code you are working with is written to assume you are running it from a command line. Java will look in the class file you specify when you execute the java command, trying to find a main method. If it does, Java then takes the rest of your command line and passes it as an array of String objects, one object for each chunk of text that is delimited by whitespace on your command line.

I say "unfortunately" because this means you are being confused by behavior that has nothing to do with String objects or arrays, and everything to do with how Java parses the command line. When you use an IDE (like NetBeans or Eclipse), you tend to stop thinking (or never start thinking) about command lines, because you tend to run your program by clicking a button. Any input to that program typically comes from GUI controls, not the console. Strings returned from GUI controls tend always to be whole single strings, regardless of whitespace (there are, of course, exceptions to that sweeping generalization, which some of my colleagues may now gleefully descend upon this thread to bludgeon me with gently point out to you).

Basically, however, your understanding of what your code is doing is correct. You are working with an array of String objects, each of which is individually being converted, in its entirety, to upper case, one by one.
 
Dj Galla
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:My guess is that the book is trying to teach you arrays. Yes, this could all be done as a String, but you'd learn less.


Any idea why my code below doesn't work?



What did I do wrong here?

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether I should write here about your previous question(Your PHP comment reminded me  ) but you misunderstood my answer that urged me to comment here so please read it again here is link of Object References explanation and make me sure you understood it properly.
 
Dj Galla
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:I say "unfortunately" because this means you are being confused by behavior that has nothing to do with String objects or arrays, and everything to do with how Java parses the command line. When you use an IDE (like NetBeans or Eclipse), you tend to stop thinking (or never start thinking) about command lines, because you tend to run your program by clicking a button.


Yup, using NetBeans. The book isn't very good "Sams Teach Yourself Java in 21 Days". It runs through "complex" examples early in the book ("complex" for a beginner), but skips over important parts that it defines later. So it is VERY confusing.

So, because I'm running the program through NetBeans, when I "run" the program, when it hits this line...

It will take the initial arguments and create an array from the string, automatically breaking it up by spaces?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dj Galla wrote:
Any idea why my code below doesn't work?



What did I do wrong here?


This doesn't work because of the way method parameters work. The method gets a copy of the reference, so you can change the state of the object, but changing the reference will have no effect on the caller.

In the case of the string array, changing the element will work... as the element is related to the string array object.

Henry
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dj Galla wrote:Yup, using NetBeans. The book isn't very good "Sams Teach Yourself Java in 21 Days". It runs through "complex" examples early in the book ("complex" for a beginner), but skips over important parts that it defines later. So it is VERY confusing.

The book by Rogers Cadenhead? Yeah, I started with that one too. He does assume you are using a command line. I used to be a great fan of the "21 days" books, but that one isn't the best in the series. To me, after I learned a lot more Java than is in that book, it ought to be called, "Fool yourself into thinking you've learned Java in 21 days."

It's becoming tragically outdated, but "Head First Java," (2d ed.) is still a good choice. The wacky "Head First" style took me some time to get used to, but I have ended up liking it. The down side is that the book only goes up to Java 5, so omits a lot of things you'll want to know and (worse, in my opinion) it teaches some obsolete library classes I don't think anyone would use anymore. Still, it's better than "21 Days."

So, because I'm running the program through NetBeans, when I "run" the program, when it hits this line...

It will take the initial arguments and create an array from the string, automatically breaking it up by spaces?

Well, yes, although I wouldn't say "from the string." It creates the array of String objects from the part of the command line following the name of your class file. However, in NetBeans, you have to set that in your project options, which (I think) is a gawdawful way to get things done. Use Jeanne's suggestion about the split method instead, to create your array of String objects in your code, and forget about the command line.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Dj Galla wrote:
Any idea why my code below doesn't work?



What did I do wrong here?


This doesn't work because of the way method parameters work. The method gets a copy of the reference, so you can change the state of the object, but changing the reference will have no effect on the caller.

In the case of the string array, changing the element will work... as the element is related to the string array object.

Henry

Henry's answer is accurate. Alas, the difference between accessing a reference to an object and a reference to an array of objects makes beginners' heads spin.

It works like this: You have a String and you want to pass a reference to that String to your method. What's a reference? Well, it's a number that Java (actually, the Java Virtual Machine) uses to identify your String object. Any String variable can contain that number and, thereby, provide a reference to the String object. So, when you pass that reference to your method, upon entry, your text parameter contains a copy of that number. When you use that parameter to invoke the String class's toUpperCase method, the method creates a whole new String object, and returns a reference to it. Your code assigns that reference to your text parameter. That's why it doesn't seem to work for you: the original String is never changed. Instead, a whole new one is created, and the only reference to it is assigned to your text parameter, which is local to your toUpperCase method. Wherever you are keeping the original reference, that reference doesn't change. It still refers to the original String (which also doesn't change).

Everything seems different when you pass a reference to an array. Again, a copy of the reference is passed to a method, but not a copy of the array. The reference passed is to the original array. So, if you change elements of that array from within your method, those changes will be made to the original array, and will be visible after your method returns.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!