目录
一、标识符和保留字
1.标识符
标识符是程序员定义的单词,它命名程序正文中的一些实体。Java的标识符的命名规则和c基本一样,不同的是Java多了美元符“$”作为可以用于合法的标识符里的符号。
2.保留字
(也叫关键字,为和c区别下称保留字)
和c的关键字一样,Java中具有专门意义和用途的保留字,不能当作一般的标识符使用。
java中的所有保留字(均用小写字母表示):
abstract,break,byte,boolean,catch,case,class,char,continue,default, double,do,else,extends,false,final,float,for,finally,if,import, implements,int,interface,instanceof,long,native,new,null,package,private,protected,public,return,switch,synchronized,short,static,super,try,true,this,throw,throws,transient,volatile,void,while 。
二、Java的数据类型
Java包含两大数据类型:基本类型和引用类型。
1.基本类型
K位整数数据范围,共
个数(k位0或1的所有可能组合)。如果在运算或者转化过程中超过范围就会发生整数溢出,需要换用更大的类型。
验证代码:
public class dataInterval {
public static void main(String[] args) {
System.out.println("ByteMax:"+Byte.MAX_VALUE);
System.out.println("ByteMin"+Byte.MIN_VALUE);
System.out.println("ShortMax:"+Short.MAX_VALUE);
System.out.println("ShortMin"+Short.MIN_VALUE);
System.out.println("IntegerMax:"+Integer.MAX_VALUE);
System.out.println("IntegerMin:"+Integer.MIN_VALUE);
System.out.println("LongMax:"+Long.MAX_VALUE);
System.out.println("LongMin:"+Long.MIN_VALUE);
}
}
输出:
2.引用类型
Java有 5种引用类型(对象类型):类、接口、数组、枚举、标注。
2.1类和接口
详见本人另一篇
2.2 数组
Java数组创建格式为:类型标识符 [](或放数组名后)数组名=new 类型标识符[数组长度];其中等号后创建了一个数组对象包括数组,等号前创建了一个对象的引用,然后通过等号将前者的内存空间传了后者。
如果创建一个基本类型数组对象,里面将存储着基本类型,数组名为基本类型数组的引用。
而创建一个引用类型数组对象,里面将存储着四位的引用,数组名为引用数组的引用。
3.包装类常见知识点整理
3.1自动装箱与自动拆箱
自动装箱就是自动将基本数据类型转换为包装器类型;自动拆箱就是自动将包装器类型转换为基本数据类型。
例如执行代码:
public class test {
public static void main(String[] args) {
int a = 1000;
Integer b = a;
System.out.println(b);
b--;
int c=b;
System.out.println(c);
}
}
其实就调用了Integer.valueOf(int i)方法,new了一个值为a的Integer类型变量,这就是自动装箱。同理c=b就是自动拆箱。
最终输出:
1000
999
//Integer.valueOf(int i)方法源码
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
3.2常量池
Java的8种基本类型(Byte, Short, Integer, Long, Character, Boolean, Float, Double), 除Float和Double以外, 其它六种都实现了常量池, 但是它们只在大于等于-128并且小于等于127时才使用常量池。
如Integer,内部定义了IntegerCache类,存放了-128~127。
//源码
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
h = Math.max(parseInt(integerCacheHighPropValue), 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
// Load IntegerCache.archivedCache from archive, if possible
CDS.initializeFromArchive(IntegerCache.class);
int size = (high - low) + 1;
// Use the archived cache if it exists and is large enough
if (archivedCache == null || size > archivedCache.length) {
Integer[] c = new Integer[size];
int j = low;
for(int i = 0; i < c.length; i++) {
c[i] = new Integer(j++);
}
archivedCache = c;
}
cache = archivedCache;
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
细心的同学会发现前面Integer.valueOf(int i)方法里面就有用到IntegerCache,当i在常量池范围内时不会new一个新的Integer,而是返回常量池里的Integer。
这样就会导致的结果就是执行下面这个语句,前者输出true,后者会返回false。因为前者执行Integer.valueOf(int i)方法,i在常量池范围内,返回的是常量池里同一个的引用,而后者是执行了构造方法new了一个新的Integer。
public class Test {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
System.out.println(b==a);
Integer c = new Integer(100);
Integer d = new Integer(100);
System.out.println(c==d);
}
}
三、运算符
1.细节辨识
1.1 & 和 &&
& :无论左边结果是什么,右边都参与运算。
&&:短路与,如果左边为false,那么右边不参数与运算。
1.2 | 和||
|:两边都运算。
||:短路或,如果左边为true,那么右边不参与运算。
四、逻辑语句
通过带标号的break跳出多重循环,与c的go to 不同在于:标号在要跳出的最大的循环的前面。
五、结语
本篇默认读者学过c语言,着重写与c不同的点,与c相关的基础知识和有些暂时没能力写的点以后找时间补上。