Java中的常量 整型 十进制(12),十六进制(0x12),八进制(012) 长整型 12L 单精度浮点 5.1f,4f,2e3f,0f 占用4字节 双精度浮点 5.1,4,2e-3,0d 占用8字节 布尔常量 true和false 字符常量 'a','8','/r'表示接受键盘输入 字符串常量 "1wwww" null常量 只有一个值,用null表示
变量的概念与作用 系统为程序分配的一块内存单元 class Lesson2 { public static void main(String[] args) { /* int x=0,y; y = x + 3; System.out.println("hello"+"world");*/ char ch = 97; System.out.println(ch); float f = 3.5f; } }
变量的字节大小以及有效范围 byte占用一个字节,数字大小为-2^7---2^7-1 short占用二个字节,数字大小为-2^15---2^15-1 int占用4个字节,数字大小为-2^31---2^31-1 long占用8字节,数字大小为-2^63---2^63-1 float占用4字节,数字大小为1.4E-45---3.4E+38,-1.4E-45--- -3.4E+38。用 二进制的指数形式表示一个浮点数的格式,如:101*2^2,101*2^-3 double占用八个字节,数字大小为4.9E-324---1.7E+308,-4.9E-324--- - 1.7E+308 char占用2字节,数字大小为0---2^16-1,是unicode编码。字符的本来面目, 所以可以直接将一个数字付给字符变量。 Boolean占用1字节,值只有2个,true和false
变量转换 byte b = 122; int x = b;//隐式类型转换 int b = (byte)x;//显示类型转换 自动类型转换的情况: 所有的byte,short,char转换成int型 如果一个操作数是long,计算结果是long 如果一个操作数是float,计算结果float 如果一个操作数是double,结果double
函数 for(int i=0;i < 3;i++) { for(int j=0;j < 2;j++) { System.out.print('*'); } System.out.println(); } 不能在main()里写函数 代码可以写在函数里: static void drawrectangle(int x,int y) { for(int i=0;i < x;i++) { for(int j=0;j < y;j++) { System.out.print('*'); } System.out.println(); } }