Professional Documents
Culture Documents
address
a type
a name (identifier)
The address or value may be passed as a parameter
Identifiers
Identifiers are used in Java to give a name to classes, methods and variables that can be used to access them. Java identifiers begin with a letter, followed by letters or digits. A Java letter includes the letters of any alphabet in Unicode plus the underscore _ and dollar sign $. There is a Java standard naming convention for the use of identifiers, which can be found at
http://java.sun.com/docs/codeconv/
There are a lot of details to learn, and it will take a while to be proficient in Java coding.
Type Compatibility
Two Types are compatible if values of one type can appear wherever values of the other type are expected, and vice versa. Java is a strongly typed language and from there comes its safety and robustness. Every variable has a type which is strictly defined. All assignments ,whether explicit or via parameter passing in method calls ,are checked for type compatibility and any mismatches result in errors .
Narrowing
Converting a value of larger range to value of smaller range. Example: int i; byte b; b=i; //invalid
Common misunderstanding
Implicit and explicit typecasting of primitive types is frequently misunderstood. The next slide is a small test program, overloaded with multiple constructors to show when and how type conversion promotion is done with implicit casting. In this example, since the third value is an integer, but there is no constructor that takes an integer, its value and the float value automatically convert to double. (Case 5: double, double, double)
public class typetest { typetest(double a, double b, short c){ System.out.println("1 (dbl dbl short)"); } typetest(float a, byte b, long c) { System.out.println("2 (float byte long)"); } typetest(long a, long b, long c) { System.out.println("3 (long long long)"); }
typetest(float a, long b, short c) { System.out.println("4 (float long short)"); } typetest(double a, double b, double c) { System.out.println("5 (dbl dbl dbl)"); } public static void main(String[] args) { typetest t = new typetest(); t.typetest(3.4, 3L, 3); } }
Assignment
For type conversion and casting the Assignment Operator is used between the two values. General form: var = exp Example: int i=10;
long m=10000L; double d=Math.PI;//PI=3.1415. i=(int)m;//cast m=i;//widening m=(long)d;//cast d=m;widening
It most cases it makes sense to consider an Object in the context of several different types. An integer may be represented as a long, a float, a double or a String. Objects may be represented as integers when using Container classes.
Type Conversion
Narrowing
Conversion of supertype to one of its subtypes is called narrowing. (downcasting) It requires explicit casting,it is allowed at compile time,it is not always safe and may result in runtime Exceptions.
Polymorphic Assignment
Polymorphic Assignment Rule:
The type of the expression at the right hand side of an assignment must be a subtype of the type of the variable at the left hand side of the assignment.
Downcasting results in exceptions being thrown,so,it should be done with proper care There are two proper ways of downcasting.
before downcasting The expression returns true if exp is an instanceof a class or an interface type named Type.
if(student1 instanceof Graduate){ Graduate gradStudent=(Graduate)student1; }else { //student1 is not a graduate student }
Continue..
The optimistic approach
ClassCastException is caught as follows,
try{ // Graduate gradStudent=(Graduate)student1; // } catch(ClassCastException e){ //student1 is not a graduate student }
Conversion Functions
Primitives have Object wrapper classes that facilitate type conversions. The Integer class, for example is a wrapper for int. An Integer may be used anywhere an Object is required (it extends java.lang.Object).
Type Assignment
Type conversion by assignment is a more restrictive form of type conversion. Essentially, conversion by assignment may only occur in cases where the assigned type is wider than the assigning type. A notable exception is in the conversion from int to byte, short, or char. Assignment is permissible if the int may be losslessly represented by the narrower class.
Type Compatibility
After an iteration of development, it is possible that a class becomes incompatible with previous versions. It is the responsibility of the new class to maintain compatibility with older versions. Indicate incompatible versions of a class using the serialVersionUID.
References
Integer (Java 2 Platform SE v1.4.2). Sun Microsystems. 2003. 4/29/06 <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Int eger.html>. Number (Java 2 Platform SE v1.4.2). Sun Microsystems. 2003. 4/29/06 <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Nu mber.html>.
References, continued
Versioning of Serializable Objects. Sun Microsystems. 1997. 4/29/06 <http://java.sun.com/products/jdk/rmi/doc/serialspec/version.doc.html>. Java Practices: Implementing Serializable. John O'Hanley. 2006. 4/29/06 <http://www.javapractices.com/Topic45.cjp>.
References, continued
VM Spec Java Programming Language Concepts. Sun Microsystems. 1999. 4/29/06 <http://java.sun.com/docs/books/vmspec/2ndedition/html/Concepts.doc.html>. Xiaoping Jia, Object Oriented Software Development Using Java. Addison Wesley, 2003 Patrick Naughton and Herbert Schildt, Java 2: The Complete Reference, Third edition