Type Checking Java is a strongly types language, which means that the Java compiler checks the code to ensure that every assignment and every call is type correct. If a type error is discovered, compilation fails with an error message. Type checking depends on the fact that every variable declaration gives the type checking of the variable, and the header of every method and constructor defines its signature: the types of its arguments and results (and also the types of any exceptions it throws). This information allows the compiler to deduce an apparent type for any expression and this deduction then allows it to determine the legality of an assignment For example consider int y= 7; int z= 3; int x= num.gcd (z,y); // discuss that
When the compiler processes the call to class Num.gcd, it knows that the Num.gcd requires two integer arguments and it also knows that expressions z and y are both of type int. therefore; it knows the call of gcd is legal. Furthermore it knows that gcd returns an int and therefore it knows that the assignment to x is legal. Note Java has an important property: legal Java programs (that is, those accepted by the compiler) are guaranteed to be type safe. This mean that there cannot by any type errors when the program runs. Object o1= a; // a is an array defined previously Object o2= “abc”; Here a is an array and s is a string An implication of the assignment rule is that actual type of the object obtained by the evaluating an expression is a subtype of the apparent type of the expression deduced by the compiler using declaration. For example, the apparent type of o2 is Object, but it actual type is String. Type checking is always done using the apparent type. This means, for example, that any method calls made using the object will be determined to be legal based on the apparent type. Therefore only Object method like equal can be called on Object O2, string method like length (which returns a count of the number of characters in the string) cannot be called: Example If ( o2.equals(“abc”)// legal If ( o2.lenght ( ) ) // illegal Furthermore, the following is illegal: String s=o2; // illegal Because the apparent type of o2 is not a subtype of String. Compilation will fail when the program contains illegal code as in these examples. Sometimes a program needs to determine the actual type of an object at runtime, for example, so that a method not provided by the apparent type can be called. This can be done by casting. s= (String) o2; // legal The use of a cast causes to occur at runtime; if the check succeeds, the indicated computation is allowed and otherwise, a ClassCastException will be raised. In the example, the casts check whether o2’s actual type is the same as the indicated type string, these checks succeed, and therefore, the assignment of the statement is allowed. Example: Object o=”abc”;
We have a following code, we need to indicate whether or not a compile time error will occur, and for those statements that are legal at compile time, indicate whether they will return normally or by throwing an exception boolean b= o.equals("a,b,c"); char c= o.charAt(1); Object o2=b; String s=o; String t=(String)o; t.length ()// 3 c=t.charAt(1);// c=t.charAt(3);
The Object Class The object class is the root of all classes in the Java technology programming language. If a class is declared with no extends clause, then the compiler adds implicitly the code extend Object to the declaration for example Public class Employee { // code here } Is equivalent to
Public class Employee extends Object { // code goes here}
Note: extends is used for import other classes. Implements is used import interface classes. The extends and implements clauses will discuss in GUI class. Two important methods are: • equals • toString
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|