新建HelloWorld.java文件
// 定义一个类HelloWorld,类名必须与文件名相同 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
编译和运行
javac HelloWorld.java //编译 java HelloWorld //运行
(1)所有关键字
(2)标识符
是指在程序中,我们自己定义的内容,比如类名,方法名和变量名等等
命名规则
命名规范
基本数据类型
变量的定义
public class Variable { public static void main(String[] args){ int i = 12; System.out.println(i); float f = 0.5f; System.out.println(f); char c = ‘B‘; System.out.println(c); } }
自动转换
public class DataType { public static void main(String[] args){ int i = 5; byte b = 2; // byte类型比int小,做运算的时候会自动把byte类型转换成int类型 int j = i + b; System.out.println(j); } }
强制转换
public class DataType { public static void main(String[] args){ double i = 2.33; System.out.println(i); //2.33 //把double类型强制转换成int类型 int j = (int) 2.33; System.out.println(j); //2 } }
原文:https://www.cnblogs.com/javase-derek/p/11442761.html