常量

数据类型

数据类型 位数 范围 notice notice
byte 8 -2^7 ~ 2^7-1 整数在计算机中的存储
short 16
int 32 1
long 64 1 l or 1 L
0123:表示八进制123
0x123:表示十六进制123
float 32 1.1f 浮点数在计算机中的存储
double 64 1.1 or 1.1d 近似存储,因此不能比较两个浮点数
char
bool true和false不对应任何整数值

运算

除法

没有++,—

op1+=op2等价于op1=(T)(op1+op2)
int sum=0;
sum+=4.5; //sum==4
sum+=4.5; //sum=(int)(sum+4.5)

变量

命名

  • 标识符使用unicode进行编码,因此可以识别中文

    类型转换

  • 低级到高级自动转换

  • 高级到低级需要强制转换

byte —>short,char—>int—>long—>float—>double
8 16 16 32 64 32 64

  1. char ch = 'B';
  2. cout << ch + 3 << endl; //69
  3. char ch = 66;
  4. cout << ch << endl; //C
  1. public class E_char{
  2. public static void main(String args[]){
  3. char ch1='B';
  4. char ch2=67;
  5. System.out.println(ch1+3); //69
  6. System.out.println(ch2); //C
  7. }
  8. }
  • 精度问题

image.png

流程控制

for ( elemType i in array ):此时i不是下标,而是数组的每一个元素

  1. public class ForTest{
  2. public static void main(String args[]){
  3. int array[]={10,20,30,40,50};
  4. for(int i:array){
  5. System.out.println(i);
  6. }
  7. }
  8. }