Operators and Assignments (2)

Source: Internet
Author: User
Tags arithmetic arithmetic operators expression final float range variables string variable
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 is considered to perform a deep comparison of the value of an object, whereas the = = operator Perfor Ms a shallow comparison. The Equals method compares what a object points to rather than the pointer itself (if we can admit that Java has pointers ).

The Equals method is applied to a string, however that string is created, performs a character by character comparison.

Objective 3)
In a 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 oth ER modifying operations in.

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 a int value.
· Indexing array elements, for example table[' a ', where the index expression must to a 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 is converted To/from any non-boolean type.
3. Widening conversions accepted. Narrowing conversions rejected.
4. Byte, short can ' t is 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 {
Doing 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'll produce an error.
char C = 3; This'll compile, even though a numeric literal is by default a int since the range of Char would accept the V Alue
int a = 3;
char d = A; This won ' t compile, since we ' re assigning a int to Char
Char e =-1; This also won ' t compile, since the ' isn't in ' range of of Char
float F = 1.3; This won ' t compile, even though the "value is within float range. Here the is isn't important, but precision is. 1.3 is by default a double, so a specific cast or F = 1.3f would work.
float F = 1/3; This is compile, since RHS evaluates to a int.
float F = 1.0/3.0; This is 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 VARIABL E, the If the value is within the range of the variable a 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 won't do narrowing conversions to resolve methods, instead we'll get a compile error.

This 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.
Can cast any Non-boolean type to another Non-boolean type.
Cannot cast a Boolean or to a Boolean type.

Conversion of Object references
Three types of reference variables to denote Objects-class, interface or array type.
Two kinds of objects can be created–class or array.
Two types of conversion–assignment and method call.
permitted if the direction of the conversion is ' up ' the inheritance hierarchy. Means that types can is assigned/substituted to only super-types–super-classes or interfaces. Not the other way around, explicit casting are needed for.
Interfaces can is used as types when declaring variables, so they participate in the object reference conversion. But We cannot instantiate an interface, since it are abstract and doesn ' t provide any implementation. These variables can is used to hold objects of classes, that implement the interface. The reason for has interfaces as types may, I am, 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 is an array of the interface type that they Implement.
Primitive arrays can be converted to only the arrays of the same type. They cannot is converted to another type of primitive array. Only object reference arrays can be converted/cast.
Primitive arrays can be converted to a Object reference, but not to a object[] reference. This are because all arrays (primitive arrays and object[]) are extended from Object.

Casting of Object References
Allows Super-types to is assigned to subtypes. Extensive checks done both at compile and runtime. At compile time, class of the object could known, so at runtime if checks fail, and a classcastexception is thrown.
Cast operator, instanceof operator and the = = operator behave the same way in allowing references S of them. You are 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 being the sub-class of the other.
· When old and new types are arrays, both must contain reference and it types is must to cast legal between (PRI Mitive arrays cannot is 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 to be new type or extend new type.
· The If new type is a interface, the class of the expression being converted must implement the interface.

An Object reference can is 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 is 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 INTERFAC E

An Interface reference can is converted to:
· An Object reference
· A super-interface reference
· Any Interface/class reference with casting with runtime check (except if the This class is final and doesn ' t implement the INT Erface)

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 is converted to:
· An Object reference
· A cloneable interface reference
· A super-class array reference, including an Object array reference
· Any sub-class Array reference and 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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.