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

NEED API HELP  RSS feed

 
John Abong
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m confused about when to use arguments with Methods. Particularly, when to place parameters inside the parenthesis or use the dot.

This issue is slowing me down because I spend too much time on syntax and not enough on structure.

ex.1 --

ex.2 --

ex.3 --

I can look back at Methods such as these that I�ve used but I�m still foggy. I think I need a lesson on interpreting the API. Ya think?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These examples all follow the same structure:



The parameters can be missing -as in the 3rd example-, but the parentheses are always required.

The dot is always required when calling methods. The only case where the object (and consequently the dot) can be missing is if the method you're calling is part of the very object in which you're calling it (or inherited from its parent objects).


Does this make things clearer?
[ November 27, 2007: Message edited by: Ulf Dittmer ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ex.2 --

this example uses some slippery convenience features of Java,

better coding style would be to say the inverse, which is equivalent

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Shirley:

better coding style would be to say the inverse


that's debatable. saying "0".equals... helps prevent null pointer exceptions.
 
John Abong
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!! Ulf:

Not clear yet. I guess some of my confusion stems from my naively trying to apply Methods to primitives vs. applying them to objects. I�m going through JavaRanch, Cattle Drive right now and I�ve made it to java-6 (Grains). At this early stage of learning, the use of objects and their properties hasn�t been the focus so, calling Methods on objects is foreign territory.

My current style is to take my best guess (which is usually correct) at choosing a Class to solve a problem but then spending hours trying to figure out how to properly invoke it's methods. I want to employ the API in a more deliberate manner.

I�ve written some pretty sophisticated lines of (VB) code in the past but nothing of a complete nature. Now I�m really trying to become a (java) programmer so I, like the rest of you, must endure the frustration of overcoming the unknown. I�m anxious to know the language as the �simple�, high-level technology that it is.

further example �



vs.



I understand that args is an object. Further, it is an object of String type, and String is from the java.lang package, and length() is a method from the String Class (data structure). But why do I get an error when I include the parenthesis?

I understand the principle behind library > package > class > method. Could you briefly explain the difference between using java.lang.string.length() to display an object�s length vs. testing to see if an array object has a acquired new element?
 
John Abong
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ALL! I learn every time I post here...
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ought to know better than to butt in, but, assuming you have got args from a part of. . . then args is a String[] array, not a String, and it has a public final attribute called length. You write args.length which tells you how many members args has. Each individual member (eg args[0]) is a String which has a public method called length() which returns an int telling you how many characters the String contains.

Note that both args.length and args[0].length() might return 0.
 
John Abong
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, you nailed that one! Thanks for your input. That was driving me nuts.

John
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!