Chapter 3 Operators

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,871评论 0 10
  • 1 意外养了猫,左眼不停地跳 在此之前我遇到的缘份可能只是缘到了,而份量不够。试问: 你们谁有过左眼跳3天的经历?...
    媛缘圆阅读 483评论 0 3
  • 这场冷空气还真是突然,今天出门的时候看到对门邻居都穿上了羽绒服。昨天还穿短袖啊…… 上午的时候贾先生带妈妈去医...
    薇薇安_b57f阅读 285评论 0 0
  • 批判性思维的定义是什么? 批判性思维就是在充分理解的基础上,积极主动地以高严的标准来自觉清晰地审查和监控我思维的诸...
    毛小五阅读 289评论 0 1
  • 6月2号张秀宝日精进:因为家里的原因没有赶上月会,今天去医院检查,太差了,挂完号没有引导去哪检查,询问之后给支的团...
    京心达张秀宝阅读 239评论 0 0

友情链接更多精彩内容