Learn Java for Android Development Second Edition 笔记(一)
来源:互联网 发布:男士 爽肤水 知乎 混合 编辑:程序博客网 时间:2024/05/05 09:07
Java与C++的相似之处
- 单行注释和多行注释相同
- 许多关键字一致,例如“for, if, switch, while, catch, class, public, try”
- 支持的原子类型数据有很多一致包括:char, double, float, int, long, short
- 很多操作符一致,例如+, -, *, /, %, ?:
- 使用{}来定义数据块
Java与C++的不同之处
- 支持额外的注释类型,/**,用于Javadoc
- 一些新的关键字:extends, strictfp, synchronized, transient
- Java不需要了解机器硬件相关的知识。提供byte integer类型,不提供signed 字符类型,不提供无符号的integer, long integer, short interger,所有Java的原子类型有固定长度的字节数。
- 新的操作符:instanceof, >>>
- 新增labeled break和labeled continue
- Java设计得比C/C++更安全,它不允许重载操作符,忽略指针。
- 如果x是整形,那么 while(x) {x++},这个语句会提示需要x是布尔型。
一个简单Java程序
public class DumpArgs{public static void main(String[] args){System.out.println("Passed arguments:");for (int i = 0; i < args.length; i++)System.out.println(args[i]);}}
Java注释
Java新增的注释类型为:以/**开始,以*/结束。里面的以@开头的特殊字符串有如下种类:
@author identifies the source code’s author.
@deprecated identifies a source code entity (such as a method) that should no longer be used.
@param identifies one of a method’s parameters.
@see provides a see-also reference.
@since identifies the software release where the entity first originated.
@return identifies the kind of value that the method returns.
@throws documents an exception thrown from a method.
@deprecated identifies a source code entity (such as a method) that should no longer be used.
@param identifies one of a method’s parameters.
@see provides a see-also reference.
@since identifies the software release where the entity first originated.
@return identifies the kind of value that the method returns.
@throws documents an exception thrown from a method.
通过javadoc工具可以产生帮助文档。
Java有效命名定义
包含大小字母,数字,连接符如_,货币符号如$。
有效的描述符必须以字母/货币符号/连接符开始。1name和first#name不是有效的描述符。
Java是大小写敏感的。
Java保留的关键字有:
abstract, assert, boolean, break, byte, case, catch, char,
class, const, continue, default, do, double, enum, else, extends, false, final, finally, float, for,
goto, if, implements, import, instanceof, int, interface, long, native, new, null, package, private,
protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw,
throws, transient, true, try, void, volatile, while.
class, const, continue, default, do, double, enum, else, extends, false, final, finally, float, for,
goto, if, implements, import, instanceof, int, interface, long, native, new, null, package, private,
protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw,
throws, transient, true, try, void, volatile, while.
Java数据类型
原子类型,自定义类型,数组类型- 原子类型
boolean类型的存储大小是依赖于具体平台实现的。
character类型可以用'A'赋值,也可以用unicode数字表示,如'\u0041',里面的数字是16进制表示。
普通int型就是直接写数字,long型是在数字后面加字母L或l。
负整数以2的补码形式存储。
- 用户自定义类型
包括class,interface,enum,annotation type。
用户自定义类型也叫做引用类型(reference type),这种类型的变量存储的是一片内存区域的引用,类似指针。原子类型的变量存储的是变量值本身。
- 数组类型
int[] ages;
char gradeLetters[];
这两种定义方法都可以。
二维数组类型的length是指列的个数。如以下方法枚举二维数组:
float[][] matrix = { { 1.0F, 2.0F, 3.0F }, { 4.0F, 5.0F, 6.0F }};for (int row = 0; row < matrix.length; row++){for (int col = 0; col < matrix[row].length; col++)System.out.print(matrix[row][col] + " ");System.out.print("\n");}
Java操作符
>>,有符号的右移
>>>,无符号的右移,连符号位也跟着右移,所以负数也可能移位为正数。
当String类型和普通类型相加时,普通类型会被转换为字符串。例如,"A"+5会等于"A5"。
条件判读式里,如果有多个条件,会从左到右判读,如果第一个满足,就不会去判读第二个了。例如age > 64 && stillWorking,当age>64时,就不会再去判断stillWorking。
当==判断符用在object操作时,它不会去比较object所指向的内容,而是比较引用本身。例如"abc" == "xyz"不会比较a和x。
循环操作
- labeled break,label必须在loop语句的前面一行,用于直接跳出多重循环。例如:
outer:for (int i = 0; i < 3; i++)for (int j = 0; j < 3; j++)if (i == 1 && j == 1)break outer;elseSystem.out.println("i=" + i + ", j=" + j);System.out.println("Both loops terminated.");以上语句的执行结果是:
i=0, j=0i=0, j=1i=0, j=2i=1, j=0Both loops terminated.
- labeled continue,也是用于跳出多重循环,继续执行剩下的循环。例如:
outer:for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (i == 1 && j == 1) continue outer; elseSystem.out.println("i=" + i + ", j=" + j);System.out.println("Both loops terminated.");以上语句的执行结果是:
i=0, j=0i=0, j=1i=0, j=2i=1, j=0i=2, j=0i=2, j=1i=2, j=2Both loops terminated.
0 0
- Learn Java for Android Development Second Edition 笔记(一)
- Learn Java for Android Development Second Edition 笔记(二)
- Learn Java for Android Development Second Edition 笔记(三)
- Learn Java for Android Development Second Edition 笔记(四)
- Learn Java for Android Development Second Edition 笔记(五)
- Learn Java for Android Development Second Edition 笔记(七)
- Learn Java for Android Development Second Edition 笔记(六)- Interface
- JAVA线程——选自《Learn Java for Android Development》
- Learn Java for Android Development (第三版)---封面
- Learn Java for Android Development (第三版)---简介
- Pro Drupal Development, Second Edition
- Linux kernel Development second Edition
- 《Effective Java》Second Edition中文版笔记(完整)
- Learning ROS for Robotics Programming Second Edition学习笔记(一) indigo v-rep
- multicast(一) (转自network programming for microsoft windows, second edition.)
- 3D Math Primer for Graphics and Game Development Second Edition
- Pro Java Programming, Second Edition
- Java Swing, Second Edition [ILLUSTRATED]
- 从零开始学HTML(四)--HTML 属性
- HDU 1025 解题报告
- 黑莓CEO回应出售手机报道:不会放弃手机业务
- MAC OS X make 命令
- 大四学生发现自己不合企业要求……
- Learn Java for Android Development Second Edition 笔记(一)
- Ubuntu 删除缓存
- URAL 1517 Freedom of Choice
- Leetcode - string - Reverse Words in a String
- NYOJ36&&南阳理工oj_36(动态规划)
- Codeigniter 报错 Unable to connect to your database server using the provided settings
- Linux系统里如何彻底的清空屏幕?
- 服务器端口大全
- 软件设计原则