Hi
I think Michèle answered all the questions. I would like just to add a
comment.
There is a convention largely used:
-- class names begin with a /Capital/ (such as /*S*/tring)
-- while internal properties (such as /*m*/yArray.length)
-- and methods (such as myString./*l*/ength())
-- as well as variable or argument names (such as String /*m*/yString or
String[] /*a*/rgs) begin with a /lower case/ letter.
You may use this convention or not (you'd better use it so your program
could be understood and maintained by other persons).
On the other hand, like the good old C language, most of the Java
features are not intrinsic to the language, but contained by the bundled
libraries. Those libraries /do respect the conventions/.
The most basic classes are contained in the package *java.lang*, such as
java.lang.String, java.lang.Integer, java.lang.System, and so on. You
can write, for example:
...
public static void main( java.lang.String[] args ) { ... }
...
but you are allowed to skip the "java.lang." from the class names. (So
"String", "Integer", and so on, for all the library classes, all begin
with a capital letter.)
Contrary to the classes, that are structured types, /byte/, /char/,
/int/, /long/, /float/, /double/ and so on are elementary types, exactly
like in C++. Java, written later than C++ and without its constraints,
could drop the elementary types and use classes everywhere. On the other
hand, some elementary data processing might become very clumsy if those
elementary types were missing. For example, there is a good thing "int"
exists for simple "for" loops.
Finally, arrays are some special constructs. There is no class "array",
of course, but the arrays are classes. For example, when you write
"String[] args" (or "String args[]" - it is the same thing), args /is an
object/ of the class "String[]". It can use the special construction
with squared parenthesis (such as "args[3]"), unique to arrays, but it
can use also standard features for classes, such as:
-- public method call: args.clone(), args.equals(...), args.getClass(),
agrs.toString()
-- public member use: args.length
Try, for example, "System.out.println(
args.getClass().getCanonicalName());" in the "main" static method.
Now, why "length" is a member for an array, while it is a method call
"length()" for a String? Well, it is assumed that the array must "know"
(store somewhere) its length, while a String might not storing it and
might need to compute it (this gives the possibility to change the
implementation of the String internal mechanisms for higher performances
without changing the already written programs).
Hope the long and boring philosophy helps
mihai
miga a écrit :
On Mar 28, 3:39 am, [email protected] wrote:
It helps clarify a lot; thanks. I do have a couple of questions though:
Why does the class 'string' have to have a capitalized 'S' whereas the class
'int' does not?
All classes begin with a capital letter; int is not a class, it is a
convenient wrapper to class Integer. Revise first lab, where all of
this is explained. You should also really study the resources.
Also, what does the open/closed parens indicate: 'args[counter].length()' and
'
maxName.length()' in this section?
It is the length of the String maxName. To get the length of a String,
you call the length() method. Revise class String,
Why isn't it used in the surrounding for/else statement?
Because, here you deal with an array. To get the size of an array, you
call its property (aka parameter) length. See class Array in the API.
So no parentheses a parameter of a class, with parentheses a method of
a class.
String maxName = "";
System.out.println("args" + args.length);
for(int counter=args.length - 1; counter > 3; counter= counter
-2){
System.out.println(counter);
if (args[counter].length() > maxName.length()){
maxName=args[counter -1];
System.out.println("maxName= " + maxName);
} else {
maxName=args[counter-3];
System.out.println("maxName: " + maxName);
}
}
Last, why does the program calculate the first age if the counter is always above 3?
String maxName = "";
System.out.println("args" + args.length);
for(int counter=args.length - 1; counter > 3; counter= counter
-2){
System.out.println(counter);
if (args[counter].length() > maxName.length()){
maxName=args[counter -1];
System.out.println("maxName= " + maxName);
} else {
maxName=args[counter-3];
because here we read the value of args[counter-3]; that is 0 when you
begin, that is the first number.
You should make a distinction between the value of the counter, and
the value of the index you really read; here you have index=counter-3.
Advice: it helps sometimes to take a sheet of paper, and manually
replace the parameter by the real value while changing the value of
the counter from the beginning, not just as you think it works, but
really as you have written it.
System.out.println("maxName: " + maxName);
}
}
Cheers.
Rob
Michèle
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.