Thanks Mihai. It helps a lot. Cheers.
-----Original Message----- From: Mihai DINCA <[email protected]> To: Java Programming Online Training Course By Sang Shin <[email protected]> Sent: Sun, Mar 28, 2010 5:28 pm Subject: Re: [java programming] Re: Lab-1038 commandarguments Hi I think Michèle answered all the questions. I would like just to add acomment. There is a convention largely used: -- class names begin with a Capital (such as String) -- while internal properties (such as myArray.length) -- and methods (such as myString.length()) -- as well as variable or argument names (such as String myStringor String[] args) begin with a lower case letter. You may use this convention or not (you'd better use it so your programcould be understood and maintained by other persons). On the other hand, like the good old C language, most of the Javafeatures are not intrinsic to the language, but contained by thebundled 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 soon. 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 beginwith a capital letter.) Contrary to the classes, that are structured types, byte, char,int, long, float, double and so on areelementary types, exactly like in C++. Java, written later than C++ andwithout its constraints, could drop the elementary types and useclasses everywhere. On the other hand, some elementary data processingmight become very clumsy if those elementary types were missing. Forexample, 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 isan object of the class "String[]". It can use the specialconstruction with squared parenthesis (such as "args[3]"), unique toarrays, 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 andmight need to compute it (this gives the possibility to change theimplementation of the String internal mechanisms for higherperformances 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? Also, what does the open/closed parens indicate: 'args[counter].length()' and ' maxName.length()' in this section? Why isn't it used in the surrounding for/else statement? 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]; System.out.println("maxName: " + maxName); } } Cheers. Rob -- 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. -- 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.
