Archive for April, 2007

120 Chapter (Web site management) 5 Classroom Q & A Q:

Wednesday, April 25th, 2007

120 Chapter 5 Classroom Q & A Q: What if I want the method to be able to change the contents of the argument? A: This cannot be done in Java. It s as simple as that. When using callby- value, changing the parameter in the method does not change the argument. Look closely at the setVolume() method of the Radio class discussed earlier. The last line of code in that method sets the parameter to 5. This does not change the argument to 5. We simply can t change the argument, even if we want to. Q: I suppose I can live with parameters not being able to change arguments, but what if I have a large amount of data that needs to be passed in to a method. I m talking very large. Typically, I would want to pass in a pointer so as to not waste the time and memory of copying large data. Can I avoid passing large objects in Java? A: Not only can you avoid passing large objects, you can t do it even if you want to. I need to reiterate a very important aspect of Java: A variable in Java is either one of the eight primitive data types or it is a reference. If the argument is a primitive data type, it is at most 64 bits in size (a double or a long). If the data I want to pass to a method is a very large object, it is not the object that is passed. It is a reference to the object, which is no larger than 64 bits, and in most cases is 32 bits. It is the reference that is copied, not the large amount of data. Q: So the largest amount of data that is copied with call-by-value in Java is only 64 bits? A: Correct! And copying 64 bits in today s computing world is rarely a concern in terms of performance or overhead. Q: What if I really want the method to change the argument passed in? A: Well, you just can t do it. But notice what you can do with the parameter if it is a reference to an object. The method can use this reference to do anything it wants to the object (depending on the access specifiers of the object s fields and methods). The method can change the data of the object being pointed to and invoke methods on the object. The only restriction arising from call-byvalue is that the method cannot change where the reference is pointing to.
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Methods 119 The field (Web hosting faq) band is assigned to

Wednesday, April 25th, 2007

Methods 119 The field band is assigned to F, and the setBand() method returns. We are back in the turnOn() method, which sets the field tuning equal to the parameter t. The turnOn() method is now complete, so flow of control jumps back down to main(). The next line of code in main() is a println() statement, but before println() is invoked, the getTuning() method of radio is called. Notice that no arguments are passed in because getTuning() has no parameters. Control jumps to get- Tuning(), which prints out the following: Inside getTuning The getTuning() method declares that it returns a float, so it must do so somewhere in the method. Notice that it returns the tuning field, which is a float containing the current radio station. The value of tuning is returned-byvalue, meaning that a copy of tuning is sent back to main(), similarly to how arguments are copied into parameters. The copy of tuning is sent back as a float and then concatenated to The tuning is , displaying something like the following: The tuning is 100.3 Control is back within main(), and the volume is set to 7. Recall that the setVolume() sets the volume parameter to 5 to demonstrate that the argument has not changed. The output of displaying the contents of x looks like the following: x = 7 The volume is then turned up by invoking turnUp() twice. The band is changed by invoking changeBand(), and the final output looks similar to the following: The volume is now 9, the band is A, and the tuning is 100.3 Figure 5.2 shows a sample output of the ListenToRadio program. Figure 5.2 Sample output of the ListenToRadio program when the command-line argument is 100.3.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

118 Chapter 5 Within setVolume(), the println() method (Msn web hosting)

Tuesday, April 24th, 2007

118 Chapter 5 Within setVolume(), the println() method is invoked and pushed onto the call stack. We now have the situation illustrated by the call stack discussed in the earlier section Method Call Stack. The following is displayed: Setting the volume to 5 The parameter volume is assigned to the field volume, so now the 5 is in memory in four different places. The parameter volume is assigned to 5, a statement I added to emphasize that this is call-by-value. What does changing volume to 5 do to the argument v that was passed in to volume? It does nothing to v because volume is a copy of v and not the actual variable v. Within the setVolume() method, the parameter name is volume, which also happens to be the name of one of the fields of the Radio class. This might seem like a naming conflict, but it is okay and is done regularly in Java. To distinguish between the local variable volume and the field volume, you must use the this reference whenever referring to the field. Therefore, using just volume refers to the parameter and using this.volume refers to the field. At one point in the ListenToRadio program, the initial volume of 5 was in four different variables in memory: initialVolume, v, volume, and this.volume. Keep in mind that the variables volume, v, and initialVolume are local variables (sometimes aptly referred to as temporary variables). They are allocated in memory on the call stack, and when the method is done executing, these variables go away. For example, when setVolume() is done, the parameter volume goes out of scope. When turnOn() is done, the parameter v goes out of scope. When main() is done, initialVolume goes out of scope. But the field volume in the Radio object stays in memory until the object is garbage collected, which can be long after these temporary variables have gone away. After setVolume() finishes, control returns to the turnOn() method, which invokes setBand(). The setBand() method has a char parameter, and the b in turnOn() is passed as an argument in to the b parameter of setBand(). There are now two variables in memory named b, both of value F, but their scopes are different. The b in turnOn() is not accessible from within setBand(). Nor is the b in setBand() accessible to the turnOn() method. This is why the value F was passed into setBand() because the F was needed within the scope of setBand(). The setBand() method prints out the following: Setting the band to F
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Methods 117 System.out.println( The volume is now +

Monday, April 23rd, 2007

Methods 117 System.out.println( The volume is now + radio.volume + , the band is + radio.band + , and the tuning is + radio.tuning); } } Let s follow the flow of control of the ListenToRadio program. The JVM invokes main() when you run the program, so main() is at the bottom of the call stack. The first line of code within main() is a call to the println() method, which displays the following: Creating a radio… The next statement in main() instantiates a new Radio object, followed by another call to println(): …and turning it on… I want to emphasize that before a Radio object is instantiated, none of the methods in the Radio class can be invoked. Until there is a Radio object in memory, there is no setVolume() method or turnOnRadio() method (and so on) to invoke. You cannot turn on a radio until a radio exists, and no radio exists until you instantiate one using the new keyword. The first command-line argument of the ListenToRadio program is the initial station to be tuned in. The command-line argument is parsed into a float and stored in the initialStation variable. An int is declared (initialVolume) and is set equal to 5. The turnOn() method is then invoked, causing it to be pushed onto the top of the call stack. To invoke turnOn(), you must pass in an int, float, and char, in that order. The int passed in is initialVolume, which is 5. The value 5 is copied into the corresponding parameter of turnOn(), which is v. Similarly, the contents of initialStation are copied in to the parameter t. Finally, the character F is copied in to the parameter b. The flow of control is now within turnOn(), and the first statement within turnOn() is a call to println(), displaying the following: Turning on the radio The setVolume() method is then invoked, pushing setVolume() onto the top of the call stack. You must pass an int into setVolume(), and v is passed as an argument. The contents of v are copied in to the parameter volume, which in this case is 5. There are now three variables in memory equal to 5: initialVolume in main(), v in turnOn(), and volume in setVolume().
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Abyss web server - 116 Chapter 5 Call-by-Value When an argument is

Monday, April 23rd, 2007

116 Chapter 5 Call-by-Value When an argument is passed in to a parameter, the argument s data is copied into the parameter. The process of copying data between method calls is referred to in programming as call-by-value. In Java, you do not specify that an argument is to be passed using callby- value. It happens automatically, and is, in fact, your only option. Other programming languages use call-by-reference and/or call-by-pointer, in which an argument is not copied into a parameter. You cannot do call-byreference or call-by-pointer in Java. No matter what type of argument you pass in to a method, the corresponding parameter will get a copy of that data, which is exactly how call-by-value works. For example, to invoke the setVolume() method of the Radio class, you must pass in an int argument: int x = 7; someRadio.setVolume(x); In the previous statements, the integer x is passed in to setVolume(). The contents of x are copied into the int parameter of setVolume(), which is the variable volume. There are now two 7s in memory. The value of volume is 7, and the value of x is still 7. The following ListenToRadio program demonstrates passing arguments to parameters. Try to determine the flow of control of the program and also what the output will be. public class ListenToRadio { public static void main(String [] args) { System.out.println( Creating a radio… ); Radio radio = new Radio(); System.out.println( …and turning it on… ); float initialStation = Float.parseFloat(args[0]); int initialVolume = 5; radio.turnOn(initialVolume, initialStation, F ); System.out.println( The tuning is + radio.getTuning()); int x = 7; radio.setVolume(x); System.out.println( x = + x); radio.turnUp(); radio.turnUp(); radio.changeBand();
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

Methods 115 } public (Graphic web design) void turnUp() { System.out.println( Turning

Monday, April 23rd, 2007

Methods 115 } public void turnUp() { System.out.println( Turning the volume up ); if(volume < 10) { volume += 1; //Increase volume by 1 } } public void turnDown() { System.out.println( Turning the volume down ); if(volume > 0) { volume -= 1; //Decrease volume by 1 } } public float getTuning() { System.out.println( Inside getTuning ); return tuning; } public void changeBand() { System.out.println( Switching bands ); if(band == A ) { band = F ; } else { band = A ; } } } Notice that the Radio class has three fields: volume, tuning, and band. The Radio class also has seven methods. The turnOn() method has three parameters: an int, a float, and a char. To invoke turnOn(), you must pass in an int, float, and char (in that order) as arguments. The setVolume() has one parameter: an int. To invoke setVolume(), you must pass in an int argument. The setBand() method also has one parameter: a char. Similarly, to invoke setBand() you must pass in a char argument. The other four methods have no parameters. No arguments can be passed in to these methods.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

114 Chapter 5 invoked, an (Web design conference) argument must be

Sunday, April 22nd, 2007

114 Chapter 5 invoked, an argument must be passed in to each parameter in the method s parameter list. The following Radio class has several fields and methods. Study the class and determine how many fields and methods it has. What is the parameter list for each method in the Radio class? public class Radio { public int volume; //0 -10 public float tuning; //Current station tuned in public char band; // A for AM or F for FM public void turnOn(int v, float t, char b) { System.out.println( Turning on the radio ); setVolume(v); setBand(b); tuning = t; } public void setVolume(int volume) { //Make sure the input is valid (between 0 and 10). System.out.println( Setting the volume to + volume); if(volume >= 0 && volume <= 10) { this.volume = volume; } else { this.volume = 0; } //Let s see what happens here. volume = -5; } public void setBand(char b) { System.out.println( Setting the band to + b); //Make sure the input is valid ( A or F ). if(b == A || b == F ) { band = b; } else { band = F ; }
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Methods 113 of a data (Free web hosting services) type and an

Sunday, April 22nd, 2007

Methods 113 of a data type and an identifier. For example, the following method signature declares two parameters, an int and a float: public float divide(int x, float f) Parameters and arguments are discussed in detail later in this chapter. List of thrown exceptions. Methods can throw an exception back to the caller of the method. An exception is thrown when a problem arises that the method is unable (or does not want) to handle itself. If a method throws a checked exception, the exception must be declared in the signature using the throws keyword. A method can declare multiple exceptions after the throws keyword, in which case they are separated by commas. For example, the following method signature declares that the method throws two possible exceptions: public void readFromFile() throws IOException, SecurityException If a method does not need to declare any exceptions using the throws keyword, this part of the signature is simply left off. Exceptions are discussed in detail in Chapter 11, Exception Handling. Here are some examples of method signatures: public int getDay() private void setName(String f, String g) int calibrate(double radius, int multiplier, boolean metric) public void addListener(Listener a) throws TooManyListenersException public Employee findEmployee(int number) throws SQLException From these signatures, you can determine everything you need to know about invoking each of these methods. For example, if you want to invoke get- Day(), you do not pass in any data and you get back an int. With setName(), you must pass in two Strings, and nothing is returned. To invoke the calibrate() method, you must pass in a double, an int, and a boolean, in that order, and an int will be returned. The addListener() method takes in a Listener object, returns nothing, and possibly throws a TooMany- Listeners exception. The findEmployee() method takes in an int, returns a reference to an Employee object, and possibly throws an SQLException. Now that you have seen the information that can be obtained by analyzing the signature of a method, let s look at how data is passed from one method to another. Arguments and Parameters The signature of a method contains a list of parameters, which are used to declare what type of data needs to be passed in to the method. The term argument refers to the data that is passed in to a parameter. When a method is
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

112 Chapter 5 Optional specifier. The next part

Saturday, April 21st, 2007

112 Chapter 5 Optional specifier. The next part of a method signature is a list of any optional specifiers. The possible values for these specifiers are static, final, abstract, native, and synchronized. A native method is used for writing a Java method that maps to a method written in a different programming language, a topic not discussed in this book. The other specifiers are discussed in detail later. A method might not use any of these specifiers, or a method might use more than one of them. The order of the access specifiers and optional specifiers is arbitrary. For example, you can declare main() as: static public void main(String [] args) However, you will almost always see the access specifier appear first in a method signature because it is the preferred style among Java programmers. Return value. A method signature must contain a return value type. If the method does not return a value, void is used. Otherwise, the data type of the return value is specified. Possible values for the return value are one of the eight primitive data types or a reference. Note that this allows you to return anything you want from a method because every variable in Java is either one of the eight primitive data types or a reference to an object. Method name. The name of the method must appear directly after the return value. The method name can be any valid Java identifier. The Java naming convention calls for naming a method in mixed case, so the first letter will be lowercase and other terms in the method name will be capitalized. Examples of mixed case include main, toString, getDay, and setPreferredSize. A common exception to mixed case is a term that is an acronym. For example, the name accessURL would be preferred to accessUrl. Using mixed case for naming methods is not required. It is strictly a naming convention only. However, keep in mind that Java s naming convention is widely used and accepted. You should have a specific reason for not following this convention. Parameter list. Immediately following the method name must appear a set of parentheses that contain the parameter list of the method. When a method is invoked, data can be passed in by the caller of the method. This passed-in data is copied into the parameters. A parameter consists
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Methods 111 Figure 5.1 (Web domain) Output of the DateProgram.

Saturday, April 21st, 2007

Methods 111 Figure 5.1 Output of the DateProgram. Methods in Java can appear only within a class because Java is a strictly object-oriented programming language. In many languages, methods appear at a global level and can be invoked at any time. In Java, methods (not declared as static) can only be invoked on instances of the class. For example, the getDay() method can be invoked only on instances of the Date class. If you do want to write a global-type method that can be invoked by anyone at any time without requiring an instance of a class, you write a static method. In Chapter 7, Advanced Java Language Concepts, static methods are discussed. A static method is essentially equivalent to the concept of a global method. Method Signature You can determine everything you need to know about invoking a method by looking at the method s signature. The signature includes information such as the method name, the parameter list, and the data type of any return value. For example, the signature of main() is as follows: public static void main(String [] args) Notice that the signature of a method does not include any of the statements in the body of the method. The signature is the declaration part of the method. The following list discusses each of the components in a method signature, shown in the order they appear when declaring a method. Access specifier. The possible values of the access specifier are public, private, protected, or default access, which is obtained by leaving off the access specifier. Public access allows anyone to invoke the method from anywhere. Private access means that no one else can invoke the method, thereby hiding it within the class. The protected and default access apply to inheritance and packages, respectively, and are discussed in detail in Chapter 7, Advanced Java Language Concepts.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services