注释的作用:解释说明
//单行注释:可以进行嵌套使用
public class Demo05_常量 { public static void main(String[] args) { //整数常量 System.out.println(4); System.out.println(-4); //小数 System.out.println(-3.14); System.out.println(8.88); //布尔常量 真(true)/假(false) System.out.println(true); System.out.println(false); //字符常量''被单引号引起的内容,有且只能有一个字符存在 System.out.println(' ');// 一个空格算一个字符 //System.out.println('-1');//-1 是两个字符 System.out.println('中'); //ctrl + alt + l 格式化代码:帮我们调整代码格式 //字符串常量""被双引号引起了的任意内容 System.out.println("今天"); //空常量 null String str = null; System.out.println(str); } }变量
package com.offcn.demo; public class Demo06_变量 { public static void main(String[] args) { int a = 10; //定义一个byte 类型的数据 //随意写出的整数,默认数据类型是int类型 //byte(取值范围):-128~127(-2^7~2^7-1) byte b1 = 127; //short(取值范围): -32768~32767(-2^15~2^15-1) short s1 = 32767; //int(取值范围):-2147483648~2147483647(-2^31~2^31-1) int i = 2147483647; long l1 = 2147483647999999l; //float 虽然是4个字节,但是远远的大于long类型的整数 //当我们定义float数据时,一定要在末尾加大写的F或者小写的f float f1 = 3.14f; //double 数据类型 比 float还要大 double d1 = 2123123123123131231.23123123123123123123123123123123123; //boolean 不参与比较 没有大小 //char 字符类型 和 short是同等大小 也是2个字节 //从大到小的数据排序 //double > float > long > int > short = char > byte } }注意事项
package com.offcn.demo;
public class Demo07_注意事项 {
public static void main(String[] args) {
//1, 变量定义后可以不赋值,使用时再赋值,不赋值不能使用
//数据类型 变量名 = 数据值;
int num ;
//如果定义的时候没有赋值,那么在使用之前必须赋值
num = 100;
System.out.println(num);
//2,变量不能重复定义,并且赋值时类型得匹配
//当我们在进行变量定义的时候,使用了相同的变量名就会报错
//short num = 1000;
//byte b1 = 1.0;
//3,变量使用时有作用域的限制
//变脸的作用域 就是这个变量定义最近的一对大括号
{
int x = 99;
System.out.println(x);
num = 6666;//变量的赋值
}
//System.out.println(x);不行 因为超出了 变量的作用域
System.out.println(num);
}
}
public class Demo08_数据类型转换 { public static void main(String[] args) { byte b1 = 127; short s1 = b1; System.out.println(s1); float f1 = b1; System.out.println(f1); //分割线 System.out.println("--------------------------------------------------"); long l1 = 2;//范围超过int取值范围就需要加L(l)不超过就不用加 //强制类型转换格式:小数据类型 变量名 = (小数据类型)大数据变量名; short s2 = (short)l1;// 2 //变量的赋值 l1 = 32769; short s3 = (short)l1;//可能会出现数据溢出 System.out.println(s3); } }数据类型转换规律
package com.offcn.demo;
public class Demo09_数据转换规律 {
public static void main(String[] args) {
//我们最常用的 整数数据类型就是 int
//当我们的整数 在做数据运算的时候,如果小于int类型,会自动提升为int类型
//byte short char
byte b1 = 127;
byte b2 = 127;
int i1 = b1 + b2;
System.out.println(i1);// 254
short s1 = 3000;
int i2 = b1 + s1;
System.out.println(i2);// 3127
//当数据类型 大于int类型
//运算结果 以大数据类型为准 小数据类型 + 大数据类型 = 大数据类型
int i3 = 1000000;
long l1 = 1000000;
long l2 = i3 + l1;
System.out.println(l2);
//当数据类型都是int类型
//运算结果还是int 数据类型
int i4 = 666;
int i5 = 100;
int i7 = i4 + i5;
}
}
public class Demo10_字符集 { public static void main(String args[]) { char c1 = 48; System.out.println(c1);// 字符0 c1 = 65; System.out.println(c1);//A c1 = 97; System.out.println(c1);//a c1 = 13; System.out.println(c1);//回车 c1 = 32; System.out.println(c1);//空格 } }