Thinking in Java

Chapter 3 Operators

2018-10-06  本文已影响0人  綿綿_

Using Java operators

1.Almost all operators work only with primitives.
2.The exceptions are ‘=‘, ‘==‘ and ‘!=‘, which work with all objects (and are a point of confusion for objects).
3.In addition, the String class supports ‘+’ and ‘+’

Assignment

Assignment is performed with the operator =. It means “Take the value of the right-hand side (often called the rvalue) and copy it into the left-hand side (often called the lvalue)”

Aliasing

this kind of code may occur aliasing:

Tank t1 = new Tank(); Tank t2 = new Tank(); 
t1.level = 9; 
t2.level = 47;
t1 = t2;
t1.level = 27

t2 will be changed to 27 too, cause t1 and t2 both contain the same reference which is point to the same object.

if you don't want aliasing in your project, just say

t1.level = t2.level

This retains the two separate objects instead of discarding one and tying t1 and t2 to the same object.

Auto increment and decrement

Prefix Versions:
For pre-increment and pre-decrement (i.e., ++a or --a),the operation is performed and the value is produced.

postfix Version:
For post-increment and post-decrement (i.e., a++ or a--), the value is produced, then the operation is performed.

Relation Operators

== and !=

The operators == and != compare object references, not the values.

equal()

The default behavior of equals( ) is to compare references. So unless you override equals( ) in your new class you won’t get the desired behavior. But most of the Java library classes implement equals( ) so that it compares the contents of objects instead of their references.

Logical Operators

You can apply AND, OR, or NOT to boolean values only. You can’t use a non-boolean as if it were a boolean in a logical expression like

print("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10)) );

Exponential notation

e would mean “ten to the power” in Java

Bitewise operator

Bitwise operators can be combined with the = sign to unite the operation and assignment: &=, |= and ^= are all legitimate. (Since ~ is a unary operator, it cannot be combined with
the = sign.)

The boolean type is treated as a one-bit value, so it is somewhat different. You can perform a bitwise AND, OR, and XOR, but you can’t perform a bitwise NOT (presumably to prevent confusion with the logical NOT).

Ternary if-else operator

The expression is of the form:

boolean-exp ? value0 : value1

If boolean-exp evaluates to true, value0 is evaluated,and its result becomes the value produced by the operator. If boolean-exp is false, value1 is evaluated and its result becomes
the value produced by the operator.

String Operator + and +=

The + and += operators can be used to concatenate strings.

If an expression begins with a String, then all operands that follow must be Strings (remember that the compiler automatically turns a double-quoted sequence of characters into a String):

When you want to use + to connect the string, be sure that the first element is a string or a sequence of characters being double-quoted.

Casting operators

In narrowing conversion (that is, when you go from a data type that can hold more information to one that doesn’t hold as much), you run the risk of losing information.

In widening conversion the new type will more than hold the information from the old type so that no information is ever lost.

Any primitive type can be casted to any other primitive type except boolean which doesn’t allow any casting at all. Class types do not allow casting. To convert one to the other, there must be special methods.

Truncation and rounding

Casting from a float or double to an integral value always truncates the number.
If instead you want the result to be rounded, use the round( ) methods in
java.lang.Math:

just like:

double above = 0.7; 
float fabove = 0.7f; 
print("Math.round(above): " + Math.round(above)); 
print("Math.round(fabove): " + Math.round(fabove));

Java has no "sizeof"

Java does not need a sizeof( ) operator because all the data types are the same size on all machines. You do not need to think about portability on this level—it is designed into the language.

上一篇下一篇

猜你喜欢

热点阅读