So far, to declare a variable, we could use the name a known primitive type, initialize the variable, and use it as we saw fit. Here is an example: public class Exercise { public static void main(String[] args) { int number = 48.62; System.out.println("Number: " + number); } } As mentioned in the previous section, all classes, and subsequently all types, are based on the Object class. Because of (or thanks to) this, you can use the Object class to declare a variable of any type. Here is an example: public class Exercise { public static void main(String[] args) { Object number = 48.62; System.out.println("Number: " + number); } } This would produce: Number: 48.62` Both programs would produce the same result because both declarations have the same effect. In the same way, in previous sections, to use a class, we declared a variable for it, initialized it and used it as necessary. Here is an example: class Square { public double side; public Square(double part) { side = part; } public double calculatePerimeter() { return side * 4; } public double calculateArea() { return side * side; } } public class Exercise { public static void main(String[] args) { Square sqr = new Square(48.62); System.out.println("Square Characteristics"); System.out.printf("Side: %.3f\n", sqr.side); System.out.printf("Perimeter: %.3f\n", sqr.calculatePerimeter()); System.out.printf("Area: %.3f\n", sqr.calculateArea()); } } This would produce: Square Characteristics Side: 48.620 Perimeter: 194.480 Area: 2363.904 When an object has been created, it uses memory (and resources). When it is not used anymore, its memory must be freed so that other applications can access that memory. In other words, the object must be destroyed. Normally, this is done automatically when the compiler (the garbage collector) judges it necessary. To support this functionality, the Object class is equipped with a method named finalize. Its syntax is: protected void finalize(); As you can see, this method is marked as protected. If you want, when creating a class, you can override this method. Here is an example: class Square { public double side; public Square(double part) { side = part; } public double calculatePerimeter() { return side * 4; } public double calculateArea() { return side * side; } public void finalize() { } } public class Exercise { public static void main(String[] args) { Square sqr = new Square(68.592); System.out.println("Square Characteristics"); System.out.printf("Side: %.3f\n", sqr.side); System.out.printf("Perimeter: %.3f\n", sqr.calculatePerimeter()); System.out.printf("Area: %.3f\n", sqr.calculateArea()); sqr.finalize(); } } In reality, to effectively override this method, you should know something about exception handling (next lesson).
When you declare and initialize two variables, one of the operations you may want to subsequently perform is to compare their values. To support this operation, the Object class is equipped with a method named equals. The equals() method uses the following syntax: public boolean equals(Object obj); This method is called from a declared variable and you must pass the other object as argument. To use this method, you should override it in your class.
To allow you to convert an object to a string, the Object class provides a method named toString. It syntax is: public String toString(); To use this method, you should override it in your class. Then you can call it where a variable of the class has been declared. Once the toString() method has been overridden, you can just use the object wherever it should be treated as a string. This means that you do not have to call its toString implementation.
To provide an enhanced support for primitive types, a class was created for each of them.
To support characters, a class named Character was derived from the Object class. The Character class is defined in the java.lang package.
To support Boolean values, a class named Boolean was derived from the Object class.
Java has an extensive support for numbers. To make it possible, a class named Number was derived from Object. Based on this Number class, the following classes were derived:
Each of these classes is based on the Number class that is derived from the Object class. All these classes are part of the java.lang package. Each Number-based class provides a method that can be used to convert its value to the primitive equivalent. These methods are:
Each Number-based classes is equipped with two constants named MIN_VALUE and MAX_VALUE. The MAX_VALUE constant represents the highest value that a variable declared from that class can bear. The MIN_VALUE constant represents the lowest value that a variable declared from that class can bear. Each Number-based class overrides the toString() and the equals() methods of their ancestor the Object class. Te overridden toString() method makes it possible to use a variable declared from their class as a string. The equals() method makes it possible to compare two variables declared and initialized from the same class.
So far in previous lessons, to retrieve a value from a primitive type, we were using the Scanner class. Each Number-based class is equipped with a method that can be used to retrieve a value from the user and convert it to the value based on the class. These methods are:
One of the most common operations performed on variables consists of comparing their values: to find out whether they are equal or to know whether one is higher than the other. These operations can be performed using the Boolean operators we reviewed in Lesson 6. The Boolean operations are part of the Java language. To formally implement them, each Number-based class is equipped with a method named compareTo. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|