用了10+年的C/C++,最近想做一些手机上的开发,发现不少平台只支持Java,
好吧,那我就开始学学Java吧,没坏处:)
学习站点:
http://java.sun.com/docs/books/tutorial/
这个博客用于记录我学习过程中的一些笔记。
笔记:
与C++不同的一些地方:
1, 类型
Instance Variables (Non-Static Fields)
Class Variables (Static Fields)
Local Variables
Parameters
内建类型:
类型 长度 default value 备注
byte 1 0
char 2 '/u0000' 用于unicode character
short 2 0
int 4 0
long 8 0L
float 4 0.0f
double 8 0.0d
boolean x false
其他类型
String x null 不可修改 (java.lang.String)
any object null
field variables will be assigned with default value (不过不要依赖这个!)
Local variables won't.
for Stirng and char, 'x' 表示非unicode, ’/u1234' 表示unicode.
null可以赋给任何变量(除了内建类型变量)
String a; a.class表示String
2 数组
int [] arr;
arr = new int(10);
int aa[]; //不鼓励这种用法
int [] bb = {1,2,3,4,5,6,7,8};
int [][] ccc; //2维数组,
某一维的长度可以不一致:
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}};
built-in length
property of array: bb.length
3 变量命名规则
unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$
", or the underscore character "_
". (避免使用$)
4 操作符
instanceof :The Type Comparison Operator。 The instanceof
operator compares an object to a specified type。 example: obj1 instanceof MyInterface, return true/false
注意: null is not an instance of anything
>>> :“unsigned right shift operator "
没有指针的概念,因此没有->操作符。
5 for的另一种形式 (designed for iteration through Collections and arrays)
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
推荐使用这种形式(enhanced for)
6 enum
enum基本上可以看作class,一种特殊的class。The compiler automatically adds some special methods when it creates an enum。
For example, they have a static values
method that returns an array containing all of the values of the enum in the order they are declared.
Note: All enums implicitly extend java.lang.Enum
. Since Java does not support multiple inheritance, an enum cannot extend anything else.
Java requires that the constants be defined first, prior to any fields or methods.
Also, when there are fields and methods, the list of enum constants must end with a semicolon.
Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.
7 Annotations
- Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
- Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
- Runtime processing — Some annotations are available to be examined at runtime
Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.
Annotation for Documentation:
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
String[] reviewers(); // Note use of array
}
@ClassPreamble (
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
reviewers = {"Alice", "Bob", "Cindy"} // Note array notation
)
public class Generation3List extends Generation2List {
// class code goes here
}
Note: To make the information in@ClassPreamble
appear in Javadoc-generated documentation, you must annotate the@ClassPreamble
definition itself with the@Documented
annotation:import java.lang.annotation.*; // import this to use
@Documented
@Documented
@interface ClassPreamble {
// Annotation element definitions
}Annotations Used by the Compiler: (类似于#pragma)
@Deprecated: means no longer be used。 When an element is deprecated, it should also be documented using the Javadoc
@deprecated
tag.@Override: means meant to override an element declared in a superclass。
@SuppressWarnings: means suppress specific warnings .
Annotation Processing
@Retention