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.
通过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.

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
原创粉丝点击