- byte
- short
- int
- long
类型 | 大小 | 范围 |
---|---|---|
byte | 1字节 | -128 ~ 127 |
short | 2字节 | -32768 ~ 32767 |
int | 4字节 | -2147483648 ~ 2147483647 |
long | 8字节 | -263 ~ 263-1 |
- float
float的精确度比double低
Float.MIN_VALUE并不是最小的浮点数 - double
数据类型转换
小的数据类型会默认转成大的数据类型
大的数据类型需强制转成小的数据类型 - boolean
&&
||
!
lazy evaluation - char
ASCII 码
Unicode
可用(int)(char c)
转换一个char
到int
,转为对应的ASCII值
有+,-运算 - reference
copy value- 练习1
package com.company;
class MyInteger{
public int num;
public MyInteger(int num){
this.num = num;
}
}
public class Main {
public static void func1(int a,int b) {
int t = a;
a = b;
b = t;
}
public static void func2(Integer a,Integer b){
Integer t = a;
a = b;
b = t;
}
public static void func3(MyInteger a,MyInteger b){
int t = a.num;
a.num = b.num;
b.num = t;
}
public static void main(String[] args) {
int a1 = 2, b1 = 3;
func1(a1,b1);
Integer a2 = 2, b2 = 3;
func2(a2,b2);
MyInteger a3 = new MyInteger(2), b3 = new MyInteger(3);
func3(a3,b3);
System.out.print(a1 + " " + b1 + " ");
System.out.print(a2 + " " + b2 + " ");
System.out.print(a3.num + " " +b3.num);
}
}
其输出结果为
2 3 2 3 3 2
- 练习2
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s2.equals(s2));
String s3 = new String("def");
String s4 = new String("def");
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));
其输出结果为
true
true
false
true
- Lintcode 相关题目
Reference
Hex Conversion
Reverse 3-digit Integer
网友评论