All the rar files have the same 
password : http://learning4you.blogspot.com/

Monday, November 10, 2008

Calling Methods in java

Calling Methods

Calling a method is similar to referring to an object's instance variables: Method calls to objects also use dot notation. The object itself whose method you're calling is on the left side of the dot; the name of the method and its arguments are on the right side of the dot:
myObject.methodOne(arg1, arg2, arg3);
Note that all calls to methods must have parentheses after them, even if that method takes no arguments:
myObject.methodNoArgs();
If the method you've called returns an object that itself has methods, you can nest methods as you would variables. This next example calls the getName() method, which is defined in the object returned by the getClass() method, which was defined in myObject. Got it?
myObject.getClass().getName();
You can combine nested method calls and instance variable references as well (in this case you're calling the methodTwo() method, which is defined in the object stored by the var instance variable, which in turn is part of the myObject object):
myObject.var.methodTwo(arg1, arg2);
System.out.println(), the method you've been using through the book this far to print out bits of text, is a great example of nesting variables and methods. The System class (part of the java.lang package) describes system-specific behavior. System.out is a class variable that contains an instance of the class PrintStream that points to the standard output of the system. PrintStream instances have a println() method that prints a string to that output stream.

Listing 4.3 shows an example of calling some methods defined in the String class. Strings include methods for string tests and modification, similar to what you would expect in a string library in other languages.


Listing 4.3. Several uses of String methods.
 1: class TestString {
2:
3: public static void main(String args[]) {
4: String str = "Now is the winter of our discontent";
5:
6: System.out.println("The string is: " + str);
7: System.out.println("Length of this string: "
8: + str.length());
9: System.out.println("The character at position 5: "
10: + str.charAt(5));
11: System.out.println("The substring from 11 to 17: "
12: + str.substring(11, 17));
13: System.out.println("The index of the character d: "
14: + str.indexOf('d'));
15: System.out.print("The index of the beginning of the ");
16: System.out.println("substring \"winter\": "
17: + str.indexOf("winter"));
18: System.out.println("The string in upper case: "
19: + str.toUpperCase());
20: }
21: }

The string is: Now is the winter of our discontent
Length of this string: 35
The character at position 5: s
The substring from positions 11 to 17: winter
The index of the character d: 25
The index of the beginning of the substring "winter": 11
The string in upper case: NOW IS THE WINTER OF OUR DISCONTENT


Analysis
In line 4, you create a new instance of String by using a string literal (it's easier that way than using new and then putting the characters in individually). The remainder of the program simply calls different string methods to do different operations on that string:
  • Line 6 prints the value of the string we created in line 4: "Now is the winter of our discontent".
  • Line 7 calls the length() method in the new String object. This string has 35 characters.
  • Line 9 calls the charAt() method, which returns the character at the given position in the string. Note that string positions start at 0, so the character at position 5 is s.
  • Line 11 calls the substring() method, which takes two integers indicating a range and returns the substring at those starting and ending points. The substring() method can also be called with only one argument, which returns the substring from that position to the end of the string.
  • Line 13 calls the indexOf() method, which returns the position of the first instance of the given character (here, 'd').
  • Line 15 shows a different use of the indexOf() method, which takes a string argument and returns the index of the beginning of that string.
  • Finally, line 19 uses the toUpperCase() method to return a copy of the string in all uppercase.

Class Methods

Class methods, like class variables, apply to the class as a whole and not to its instances. Class methods are commonly used for general utility methods that may not operate directly on an instance of that class, but fit with that class conceptually. For example, the String class contains a class method called valueOf(), which can take one of many different types of arguments (integers, booleans, other objects, and so on). The valueOf() method then returns a new instance of String containing the string value of the argument it was given. This method doesn't operate directly on an existing instance of String, but getting a string from another object or data type is definitely a String-like operation, and it makes sense to define it in the String class.

Class methods can also be useful for gathering general methods together in one place (the class). For example, the Math class, defined in the java.lang package, contains a large set of mathematical operations as class methods-there are no instances of the class Math, but you can still use its methods with numeric or boolean arguments. For example, the class method Math.max() takes two arguments and returns the larger of the two. You don't need to create a new instance of Math; just call the method anywhere you need it, like this:

in biggerOne = Math.max(x, y);
To call a class method, you use dot notation as you do with instance methods. As with class variables, you can use either an instance of the class or the class itself on the left site of the dot. However, for the same reasons noted in the discussion on class variables, using the name of the class for class methods makes your code easier to read. The last two lines in this example produce the same result (the string "5"):
String s, s2;
s = "foo";
s2 = s.valueOf(5);
s2 = String.valueOf(5);

0 comments:

Related Posts with Thumbnails