Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.
public keyword in method signatures when implementing an interface's methods in a class. Otherwise, the compiler reports an error.
That error occurs because you are trying to narrow a method's visibility, which Java does not permit.
This month's homework features two questions:
NOT_STARTED and STARTED constants, and the isstarted() method in the StartStop interface?
Below are last month's homework questions and their answers in red:
extends Object to a class declaration?
You should not add extends Object to a class declaration because Java does not support multiple implementation inheritance; a class can only extend a single
class. For example, if a Car class extends Object, it cannot also extend Vehicle. Besides, all classes inherit from Object, so there is no reason to explicitly extend the Object class.
String objects?
You could deeply clone an array of String objects so that corresponding elements in each array reference different String objects with the same contents. First you would perform a shallow clone to size a new array. Then you would use a for loop with appropriate code that assigns to all the new array's elements a new String object with the corresponding old array element's String contents. The following code fragment demonstrates that:
String [] bigCats =
{
"Tiger",
"Lion",
"Leopard",
"Panther",
"Cougar"
};
String [] bigCats2 = (String []) bigCats.clone ();
for (int i = 0; i < bigCats.length; i++)
bigCats2 [i] = new String (bigCats [i]);
String class allow you to clone String objects? (Hint: Think of immutability.)
The String class does not allow you to clone String objects because Java has a policy where it shares a single String object among multiple references, and cloning violates that policy. The String sharing policy helps reduce a program's memory requirements. For example, suppose you create a String object that contains a sequence of 100 characters. (Note: A String object treats a string as a sequence of characters.) Because each character occupies two bytes (remember Unicode?), 200 bytes
of storage dedicate to containing the string. Now, suppose you need an array of 1,000 copies of the object. If each array
element references a separate String object containing the same sequence of characters, the program will require 200,000 bytes to hold all the characters in all
copies of the string. However, by sharing the single String object, the program only requires 200 characters for a single string, and each element in the array references that same
String object. Because of the String sharing policy, strings are considered immutable.