Demo1
/** * @projectName: JAVA编程 * @package: PACKAGE_NAME * @className: Demo01 * @author: IT_Jay * @description: TODO * @date: 2021/11/14 21:08 */ public class Demo01 { public static void main(String[] args) { //标识符大小写敏感 String Ahello = "IT_Jay"; String hello = "IT_Jay"; String $hello = "IT_Jay"; String _hello = "IT_Jay"; /* 标识符错误写法范例 String 1hello = "IT_Jay"; String #hello = "IT_Jay"; String *hello = "IT_Jay"; String class = "IT_Jay"; String _1*# = "IT_Jay"; */ String _1class = "IT_Jay"; System.out.println(_1class); } }
Demo2
/** * @projectName: JAVA编程 * @package: PACKAGE_NAME * @className: Demo02 * @author: IT_Jay * @description: TODO * @date: 2021/11/14 21:23 */ public class Demo02 { public static void main(String[] args) { //八大基本数据类型 //整数 int num1 = 10; //常用 byte num2 = 20; short num3 = 30; long num4 = 40L; //Long型要在数字后加L //小数,浮点数 float num5 = 50.1F; //float型要在数字后加F double num6 = 3.14159265; //字符 char name = '我'; //两个字节,一个汉字 //字符串 String namea = "周杰伦"; //注意String颜色,String不是关键字,而是类 //布尔型 返回True或false boolean flag = true; System.out.println(num1); System.out.println(num2); System.out.println(num3); System.out.println(num4); System.out.println(num5); System.out.println(num6); System.out.println(name); System.out.println(namea); System.out.println(flag); } }
运行结果
注意:
-
Long型要在数字后加L
-
float型要在数字后加F
-
String颜色,String不是关键字,而是类
Demo3
/** * @projectName: JAVA编程 * @package: PACKAGE_NAME * @className: Demo03 * @author: IT_Jay * @description: TODO * @date: 2021/11/14 21:48 */ public class Demo03 { public static void main(String[] args) { //整数拓展 int i1 = 10; int i2 = 010; //八进制 0 开头 int i3 = 0x10; //十六进制 0x 开头 System.out.println(i1); System.out.println(i2); System.out.println(i3); System.out.println("====================================================="); //======================================================================================== //浮点数拓展 银行业务中对钱的计算 //BigDecimal 数学工具类,严谨 //======================================================================================== //float 有限 离散 舍入误差 大约的 接近但不等于 //double /* 最好完全使用浮点数进行比较 */ float f = 0.1f; //0.1 double d = 1.0/10; //0.1 注意1.0的写法 System.out.println(f==d); //false // System.out.println(f); // System.out.println(d); float f1 = 1231231849348923f; float f2 = f1 +1; System.out.println(f1==f2); //true //======================================================================================== //字符拓展 //======================================================================================== System.out.println("====================================================="); char c1 = 'a'; char c2 = '帅'; System.out.println(c1); System.out.println((int)c1); //强制转换类型 System.out.println(c2); System.out.println((int)c2); //强制转换类型 int c3 = 65536; System.out.println((char)c3); //将int型强转为char型,unicode标中65536位置的字符 //所有字符本质是数字,转型后显示其在Unicode中位置 //U0000 UFFFF char c4 = '\u0061'; System.out.println(c4); //a System.out.println("====================================================="); //转义字符 // \t 制表符 System.out.println("Hello\tWorld"); // \n 换行 System.out.println("Hello\nWorld"); System.out.println("====================================================="); //对象 从内存分析 String s1 = new String("hello world"); String s2 = new String("hello world"); System.out.println(s1==s2); String s3 = "hello world"; String s4 = "hello world"; System.out.println(s3==s4); System.out.println("====================================================="); //布尔值扩展 boolean flag = true; if (flag){ //等同于 if(flag == true){} //less is more!原则 即代码要精简易读 System.out.println("ok"); } } }
运行结果
小结
-
八进制 0 开头,十六进制 0x 开头
-
float 有限 离散 舍入误差 大约的 接近但不等于,BigDecimal 数学工具类,严谨
-
最好完全使用浮点数进行比较
-
所有字符本质是数字,转型后显示其在Unicode中位置
-
\t 制表符,\n 换行
-
//less is more!原则 即代码要精简易读