Operators and Assignments(2)

Objective 2)
Determine the result of applying the boolean equals(Object) method to objects of any combination of the classes java.lang.String java.lang.Boolean and java.lang.Object.

The equals method can be considered to perform a deep comparison of the value of an object, whereas the == operator performs a shallow comparison. The equals method compares what an object points to rather than the pointer itself (if we can admit that Java has pointers).

The equals method applied to a String, however that String was created, performs a character by character comparison.

Objective 3)
In an expression involving the operators & | && || and variables of known values state which operands are evaluated and the value of the expression.

Objective 4)
Determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method.

Unary Numeric Promotion
Contexts:
·    Operand of the unary arithmetic operators + and –
·    Operand of the unary integer bit-wise complement operator ~
·    During array creation, for example new int[x], where the dimension expression x must evaluate to an int value.
·    Indexing array elements, for example table[‘a’], where the index expression must evaluate to an int value.
·    Individual operands of the shift operators.

Binary numeric promotion
Contexts:
·    Operands of arithmetic operators *, / , %, + and –
·    Operands of relational operators <, <= , > and >=
·    Numeric Operands of equality operators == and !=
·    Integer Operands of bit-wise operators &, ^ and |

Conversion of Primitives
1.    3 types of conversion – assignment conversion, method call conversion and arithmetic promotion
2.    boolean may not be converted to/from any non-boolean type.
3.    Widening conversions accepted. Narrowing conversions rejected.
4.    byte, short can’t be converted to char and vice versa.
5.    Arithmetic promotion
5.1    Unary operators
·    if the operand is byte, short or char  {
convert it to int;
           }
            else {
do nothing; no conversion needed;
}
5.2    Binary operators
·    if one operand is double {
all double; convert the other operand to double;
           }
           else if one operand is float {
all float; convert the other operand to float;
}
else if one operand is long {
all long; convert the other operand to long;
}
else {
all int; convert all to int;
}
6.    When assigning a literal value to a variable, the range of the variable’s data type is checked against the value of the literal and assignment is allowed or compiler will produce an error.
char c = 3; // this will compile, even though a numeric literal is by default an int since the range of         char will accept the value
int a = 3;
char d = a; // this won’t compile, since we’re assigning an int to char
char e = -1; // this also won’t compile, since the value is not in the range of char
float f = 1.3; // this won’t compile, even though the value is within float range. Here range is not  important, but precision is. 1.3 is by default a double, so a specific cast or f = 1.3f will work.
float f = 1/3; // this will compile, since RHS evaluates to an int.
float f = 1.0 / 3.0; // this won’t compile, since RHS evaluates to a double.
7.    Also when assigning a final variable to a variable, even if the final variable’s data type is wider than the variable, if the value is within the range of the variable an implicit conversion is done.
byte b;
final int a = 10;
b = a; // Legal, since value of ‘a’ is determinable and within range of b
final int x = a;
b = x; // Legal, since value of ‘x’ is determinable and within range of b
int y;
final int z = y;
b = z; // Illegal, since value of ‘z’ is not determinable

8.    Method call conversions always look for the exact data type or a wider one in the method signatures. They will not do narrowing conversions to resolve methods, instead we will get a compile error.

Here is the figure of allowable primitive conversion.

byte à short à int à long à float à double
                            ­
           char

Casting of Primitives
9.    Needed with narrowing conversions. Use with care – radical information loss. Also can be used with widening conversions, to improve the clarity of the code.
10.    Can cast any non-boolean type to another non-boolean type.
11.    Cannot cast a boolean or to a boolean type.

Conversion of Object references
12.    Three types of reference variables to denote objects - class, interface or array type.
13.    Two kinds of objects can be created – class or array.
14.    Two types of conversion – assignment and method call.
15.    Permitted if the direction of the conversion is ‘up’ the inheritance hierarchy. Means that types can be assigned/substituted to only super-types – super-classes or interfaces. Not the other way around, explicit casting is needed for that.
16.    Interfaces can be used as types when declaring variables, so they participate in the object reference conversion. But we cannot instantiate an interface, since it is abstract and doesn’t provide any implementation. These variables can be used to hold objects of classes that implement the interface. The reason for having interfaces as types may be, I think, several unrelated classes may implement the same interface and if there’s a need to deal with them collectively one way of treating them may be an array of the interface type that they implement.
17.    Primitive arrays can be converted to only the arrays of the same primitive type. They cannot be converted to another type of primitive array. Only object reference arrays can be converted / cast.
18.    Primitive arrays can be converted to an Object reference, but not to an Object[] reference. This is because all arrays (primitive arrays and Object[]) are extended from Object.

Casting of Object references
19.    Allows super-types to be assigned to subtypes. Extensive checks done both at compile and runtime. At compile time, class of the object may not be known, so at runtime if checks fail, a ClassCastException is thrown.
20.    Cast operator, instanceof operator and the == operator behave the same way in allowing references to be the operands of them. You cannot cast or apply instanceof or compare unrelated references, sibling references or any incompatible references.

Compile-time Rules
·    When old and new types are classes, one class must be the sub-class of the other.
·    When old and new types are arrays, both must contain reference types and it must be legal to cast between those types (primitive arrays cannot be cast, conversion possible only between same type of primitive arrays).
·    We can always cast between an interface and a non-final object.

Run-time rules
·    If new type is a class, the class of the expression being converted must be new type or extend new type.
·    If new type is an interface, the class of the expression being converted must implement the interface.

An Object reference can be converted to: (java.lang.Object)
·    an Object reference
·    a Cloneable interface reference, with casting, with runtime check
·    any class reference, with casting, with runtime check
·    any array referenece, with casting, with runtime check
·    any interface reference, with casting, with runtime check

A Class type reference can be converted to:
·    any super-class type reference, (including Object)
·    any sub-class type reference, with casting, with runtime check
·    an interface reference, if the class implements that interface
·    any interface reference, with casting, with runtime check (except if the class is final and doesn’t implement the interface)

An Interface reference can be converted to:
·    an Object reference
·    a super-interface reference
·    any interface/class reference with casting, with runtime check (except if the class is final and doesn’t implement the interface)

A Primitive Array reference can be converted to:
·    an Object reference
·    a Cloneable interface reference
·    a primitive array reference of the same type

An Object Array reference can be converted to:
·    an Object reference
·    a  Cloneable interface reference
·    a super-class Array reference, including an Object Array reference
·    any sub-class Array reference with casting, with runtime check

Examples
Q1 Given these class definitions:
class Superclass { }
class Subclass1 extends Superclass { }

and these objects:
Superclass a = new Superclass();
Subclass1 b = new Subclass1();
which of the following explains the result of the statement:
b = (Subclass1)a;

Select the one right answer.
a) Illegal at compile time
b) Legal at compile time but possibly illegal at runtime  // throw a ClassCastException
c) Definitely legal at runtime

时间: 2024-07-29 07:23:57

Operators and Assignments(2)的相关文章

Operators and Assignments(1)

5)Operators and AssignmentsObjective 1)Determine the result of applying any operator including assignment operators and instanceof to operands of any type class scope or accessibility or any combination of these.1.    Unary operators.1.1    Increment

operators:php operators

php operatorsthis section lists the different operators used in php.arithmetic operators + addition x=2x+2 4 - subtraction x=25-x 3 * multiplication x=4x*5 20 / division 15/55/2 32.5 % modulus (division remainder) 5%210%810%2 120 ++ increment x=5x++

win8-access control assistance operators

问题描述 access control assistance operators 在win8.1 的 计算机管理 本地用户和组中,今天 突然发现 access control assistance operators 这个组, 想删除掉它 提示 无法删除 无法使用内置用户 删除 删除掉 对于 使用系统有没有太大影响? 如何才能删掉它? 解决方案 http://zhidao.baidu.com/link?url=nN7KQRTeMc3_VnRtaT8KW9RIHLRxGRlxbVm7IaY2NfG

Effective C++ 读书笔记之Part2.Constructors, Destructors, and Assignment Operators

5.Know what functions C++ silently writes and calls. 总结:编译器可以暗自为class创建default构造函数.copy构造函数.copy assginment操作符,以及析构函数.这些函数都是public的,并且是inline的. 6.Explicitly disallow the use of compiler-generated functions you to not want. 总结:为驳回编译器自动(暗自)提供的机能,可将相应的成

Java的位运算(bitwise operators)

Java的位运算(bitwise operators)直接对整数类型的位进行操作,这些整数类型包括long.int.short.char和 byte,位运算符具体如下表: 运算符 说明 << 左移位,在低位处补0 >> 右移位,若为正数则高位补0,若为负数则高位补1 >>> 无符号右移位,无论正负都在高位补0 & 与(AND),对两个整型操作数中对应位执行布尔代数,两个位都为1时输出1,否则0. | 或(OR),对两个整型操作数中对应位执行布尔代数,两个位

Conversion Operators in OpenCascade

Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the operators when applied to objects. It also lets us define conversion operations for class types. Class-type conversions are used like the built-in con

重构——7移除对参数的赋值(Remove Assignments to Parameters)

移除对参数的赋值(Remove Assignments to Parameters) 代码对一个参数进行赋值,以一个临时变量取代该参数的位置 一.动机 1.降低了代码的清晰度 2.java只采用按值传递方式,我们的讨论也正是基于这一点 二.做法 1.建立一个临时变量,把待处理的参数值赋予它. 2.以"对参数赋值"为界,将其后的所有对此参数的引用点,全部替换为对此临时变量的引用 3.修改赋值语句,使其对新建之临时变量赋值 4.编译,测试

Es6系列之destructuring assignments

Ecmascript 6简称es6,是javascript下一代标准,还处在开发阶段,估计2014年底发布,有关更多浏览器对es6的支持情况,点击这里 今天说说es6里对赋值语句的改进,简称解构赋值. 解构赋值 所谓解构赋值其实就是按照模式匹配进行批量赋值 下面的是以往的赋值语句 var a = 1; var b = 2; var c = 3; 以往的方式对于赋值多个变量的时候代码比较多而且不方便,那么es6里对它是怎么改进的呢? 通过以对象或者数组结构组装数据进行赋值,要保证赋值左右值类型相同

Basic Operators Of Swift 2.1

原文出自:标哥的技术博客 前言 运算符是检查.改变或合并值的特殊符号或短语.例如:加号+将两个数相加.更复杂的运算例子包括逻辑与运算符&&或让i值加1的便捷自增运算符++i等. Swift支持大部分标准C语言的运算符,且改进许多特性来减少常规编码错误.例如:赋值符不返回值,以防止把想要判断相等运算符的地方写成赋值符导致的错误.算术运算符(+,-,*,/,%等)会检测并不允许值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致异常.当然允许你使用Swift的溢出运算符来实