Thinking in Java——笔记(3)
Operator
Using Java operators
- Some operators change the value of an operand. This is called a side effect.
- Almost all operators work only with primitives. The exceptions are ‘=‘, ‘==‘ and ‘!=‘, which work with all objects.
Precedence
- You should use parentheses to make the order of evaluation explicit.
Assignment
- An rvalue is any constant, variable, or expression that produces a value, but an lvalue must be a distinct, named variable.
- When you assign primitives, you copy the contents from one place to another.
- When you assign “from one object to another,” you’re actually copying a reference from one place to another.
- Because you’ve assigned a reference, changing the t1 object appears to change the t2 object as well! This is because both t1 and t2 contain the same reference, which is pointing to the same object.
- Manipulating the fields within objects is messy and goes against good object-oriented design principles.
- Aliasing will also occur when you pass an object into a method.
Mathematical operators
- For pre-increment and pre-decrement (i.e., ++a or --a), the operation is performed and the value is produced. For post-increment and post-decrement (i.e., a++ or a--), the value is produced, then the operation is performed.
- For the prefix form, you get the value after the operation has been performed, but with the postfix form, you get the value before the operation is performed.
Relational operators
- Equivalence and nonequivalence work with all primitives, but the other comparisons won’t work with type boolean.
- While the contents of the objects are the same, the references are not the same. The operators == and != compare object references.
- 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.
- 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 as you can in C and C++.
- A boolean value is automatically converted to an appropriate text form if it is used where a String is expected.
- The expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined.
- The latter parts of a logical expression might not be evaluated.
Literals
- A trailing character after a literal value establishes its type.
- If you try to initialize a variable with a value bigger than it can hold, the compiler will give you an error message.
Exponential notation
- When the FORTRAN programming language was invented, they decided that e would mean “ten to the power”.
- The compiler normally takes exponential numbers as doubles.
Bitwise operators
- Because bits are “small”, there is only one character in the bitwise operators.
- For booleans, the bitwise operators have the same effect as the logical operators except that they do not short circuit.
Shift operators
- If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int.
- There is a problem with the unsigned right shift combined with assignment. If you use it with byte or short, you don’t get the correct results. Instead, these are promoted to int and right shifted, but then truncated as they are assigned back into their variables, so you get -1 in those cases.
Ternary if-else operator
- You should be somewhat wary of using it on an everyday basis—it’s easy to produce unreadable code.
- It’s generally warranted when you’re setting a variable to one of two values.
String operator + and +=
- Java programmers cannot implement their own overloaded operators like C++ and C# programmers can.
- If an expression begins with a String, then all operands that follow must be Strings.
- The string conversion does not depend on what comes first.
- You will sometimes see an empty String followed by a + and a primitive as a way to perform the conversion without calling the more cumbersome explicit method.
Common pitfalls when using operators
- Leave out the parentheses when you are even the least bit uncertain about how an expression will evaluate.
- Bitwise AND and OR use one of the characters (& or |) while logical AND and OR use two (&& and ||).
Casting operators
- Java will automatically change one type of data into another when appropriate.
- It’s possible to perform a cast on a numeric value as well as on a variable.
- You are allowed to use superfluous casts to make a point or to clarify your code.
- When you perform a so-called narrowing conversion , you run the risk of losing information.
- Java allows you to cast any primitive type to any other primitive type, except for 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.
Promotion
- In general, the largest data type in an expression is the one that determines the size of the result of that expression.
Java has no “sizeof”
- The most compelling reason for sizeof( ) in C and C++ is for portability.
- The programmer must discover how big those types are when performing operations that are sensitive to size.
- Java does not need a sizeof( ) operator for this purpose, because all the data types are the same size on all machines.
Thinking in Java——笔记(3)的更多相关文章
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- servlet(6) - servlet总结 - 小易Java笔记
垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...
- Java笔记 —— 继承
Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...
- Java笔记 —— 方法重载和方法重写
Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...
- Java笔记 —— 初始化
Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Java 笔记 —— java 和 javac
Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...
随机推荐
- VS链接过程中与MSVCRT.lib冲突
vs代码生成有/MT,/MTd,/Md,/MDd四个编译选项,分别代表多线程.多线程调试.多线程DLL.多线程调试DLL. 编译时引用的lib分别为libcmt.li.libcmtd.lib.msvc ...
- Android客户端性能测试(一):使用APT测试Android应用性能
一.APT介绍: APT:Android Performance Testing Tools,适用于开发自测和定位性能瓶颈,帮助测试人员完成[性能基准测试.竞品测试]. APT提供了CPU利用率实时曲 ...
- 疯狂java学习笔记之面向对象(四) - this关键字
Java中this关键字主要有以下两个方法: 1.this引用 - 可用于任何非static修饰的方法和构造器中,当this用于方法中时,它代表调用该方法的实例/对象;当this用于构造器中时,它代表 ...
- OpenCV 线性混合(4)
带滚动条的线性混合示例: #include "stdafx.h" #include<iostream> #include<thread> #incl ...
- HDU5772 String problem(最大权闭合子图)
题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...
- 每天一个linux命令--more/less
最近小编在和第三方调试接口,只能查日志,查询除了tail,grep,cat,之外,还有 more.less,他们的优点在于可以翻页. more最基本的指令就是按空白键(space)就往下一页显示,按 ...
- django 安装
git clone https://github.com/django/django.git 或者到django的官网下载 然后 python setup.py install
- ACM 矩形的个数
矩形的个数 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3 ...
- [WP8.1UI控件编程]Windows Phone动画方案的选择
8.1 动画方案的选择 Windows Phone的动画实现方式有线性插值动画(3种类型).关键祯动画(4种类型)和基于帧动画,甚至还有定时器动画,然后动画所改变的UI元素属性可以是普通的UI元素属性 ...
- 崩溃恢复(crash recovery)与 AUTORESTART参数
关于这个参数设置的影响,在生产系统中经历过两次: 第一次是有套不太重要的系统安装在虚拟机,这套系统所有应用(DB2 WAS IHS)都配置到/etc/rc.local中,每次启动机器会自 ...