Java Reference
In-Depth Information
public class SomeThing {
private int t1;
private static
public class SomeThing {
private int t1;
private static
int t2;
int t2;
public SomeThing( int t1, int t2) {
...
public SomeThing( int t1) {
...
}
}
}
public static void setT2( int t2) {
SomeThing.t2 = t2;
}
}
(a)
(b)
Instance and static are integral parts of object-oriented programming. A data field or
method is either instance or static. Do not mistakenly overlook static data fields or methods.
It is a common design error to define an instance method that should have been static. For
example, the factorial(int n) method for computing the factorial of n should be
defined static, because it is independent of any specific instance.
A constructor is always instance, because it is used to create a specific instance. A static
variable or method can be invoked from an instance method, but an instance variable or
method cannot be invoked from a static method.
common design error
10.13 Describe class design guidelines.
Check
Point
10.12 Processing Primitive Data Type Values as Objects
A primitive type value is not an object, but it can be wrapped in an object using a
wrapper class in the Java API.
Key
Point
Owing to performance considerations, primitive data type values are not objects in Java.
Because of the overhead of processing objects, the language's performance would be
adversely affected if primitive data type values were treated as objects. However, many Java
methods require the use of objects as arguments. Java offers a convenient way to incorporate,
or wrap, a primitive data type into an object (e.g., wrapping int into the Integer class, and
wrapping double into the Double class). Recall that a char value can be wrapped into a
Character object in Section 9.5. By using a wrapper class, you can process primitive data
type values as objects. Java provides Boolean , Character , Double , Float , Byte , Short ,
Integer , and Long wrapper classes in the java.lang package for primitive data types. The
Boolean class wraps a Boolean value true or false . This section uses Integer and Dou-
ble as examples to introduce the numeric wrapper classes.
why wrapper class?
Note
Most wrapper class names for a primitive type are the same as the primitive data type
name with the first letter capitalized. The exceptions are Integer and Character .
naming convention
Numeric wrapper classes are very similar to each other. Each contains the methods
doubleValue() , floatValue() , intValue() , longValue() , shortValue() , and
byteValue() . These methods “convert” objects into primitive type values. The key features
of Integer and Double are shown in Figure 10.14.
You can construct a wrapper object either from a primitive data type value or from a string
representing the numeric value—for example, new Double(5.0) , new Double("5.0") ,
new Integer(5) , and new Integer("5") .
constructors
 
 
Search WWH ::




Custom Search