解析hello world程序
package com.company;/** public 表示这个类是公开的* class (类)* Main 类名(首字母大写)* {} 类的定义* */public class Main { /*public、static 用来修饰方法,她是一个公开的静态方法 * void 方法的返回类型 * {} 中间就是方法的代码 * main方法 参数类型是String[],参数名是args * */ public static void main(String[] args) { // java固定入口方法 // write your code here System.out.println("hello world"); }}
基本数据类型
变量
public class Main{ public static void main(String[] args){ int a = 100; // 定义int类型变量a,并赋予初始值100 System.out.print(a); // 打印不换行 System.out.println(a); // 打印换行 a = 200; // 重新赋值为200 System.out.println("a = " + a); }}
整数类型:byte(1个字节),short(2个字节),int(4个字节),long(8个字节)
byte:-128 ~ 127short: -32768 ~ 32767int: -2147483648 ~ 2147483647long: -9223372036854775808 ~ 9223372036854775807public class Main { public static void main(String[] args) { int i = 2147483647; int i2 = -2147483648; int i3 = 2_000_000_000; // 加下划线更容易识别 int i4 = 0xff0000; // 十六进制表示的16711680 int i5 = 0b1000000000; // 二进制表示的512 long l = 9000000000000000000L; // long型的结尾需要加L }}
浮点数类型:float(4个字节),double(8个字节)
float f1 = 3.14f;// 对于float类型,需要加上f后缀。// 浮点数可表示的范围非常大,float类型可最大表示3.4x10^38,而double类型可最大表示1.79x10^308。
字符类型:char(2个字节)与字符串类型:String
char a = 'A';char zh = '中';// 注意char类型使用单引号',且仅有一个字符,要和双引号"的字符串类型区分开。// 定义字符串类型// 字符串是不可变类型String s = "hello";public class Main { public static void main(String[] args){ String s1 = "hello"; String s2 = "world"; String s = s1 + " " + s2 + "!"; System.out.println(s); }}
布尔类型:boolean
boolean b1 = true;boolean isGreater = 5 > 3; // 计算结果为true
常量
// 定义变量的时候,如果加上final修饰,这个变量就变成了常量final double PI = 3.14; // PI是一个常量
var关键字
StringBuilder sb = new StringBuilder();// 可以简写为如下var sb = new StringBuilder();
运算
整数运算
public class Main{ public static void main(String[] args){ //int x = 123456/66; // 结果取整数 //int x = 123456%66; // 取余数 //int x = 12345678912; // 造成数据溢出,要用long int n = 3300; //n++; // 3301,相当于n=n+1; ++n; System.out.println(n); }}// 移位运算// 当n为正数时int n = 7; // 00000000 00000000 00000000 00000111 = 7int a = n << 1; // 00000000 00000000 00000000 00001110 = 14// <<右边的数字是几,就往左移动几位int n = 7; // 00000000 00000000 00000000 00000111 = 7int a = n >> 1; // 00000000 00000000 00000000 00000011 = 3// >>右边的数字是几,就往右移动几位// 当n为负数时(最高位的1不动,进行右移,结果还是负数)int n = -536870912;int a = n >> 1; // 11110000 00000000 00000000 00000000 = -268435456int b = n >> 2; // 11111000 00000000 00000000 00000000 = -134217728// >>右边的数字是几,就往右移动几位补一// 当n为负数(不管符号位,右移后高位补0)int n = -536870912;int a = n >>> 1; // 01110000 00000000 00000000 00000000 = 1879048192// 位运算(位运算是按位进行与、或、非和异或的运算。)/*与: & (两正为正)或:| (一正为正)非:~ (非0为正)异或:^ (有一个正为正)*/// 计算前N项和(利用前N项和公式(1+N)*N/2)public class Main{ public static void main(String[] args){ int N = 100; N = (1+N)*N/2; System.out.println(N); }}
浮点数运算
// 浮点数常常无法精确表示// 根据一元二次方程ax^2+bx+c=0的求根公式:// 计算出一元二次方程的两个解:public class Main { public static void main(String[] args) { // x*x + 3*x - 4 = 0 double a = 1.0; double b = 3.0; double c = -4.0; // 求平方根可用 Math.sqrt(): // double x = Math.sqrt(2)); // ==> 1.414 //double r1 = 0; //double r2 = 0; double r1,r2; // 定义的变量不赋值,默认就是0 r1=(-b+Math.sqrt(b*b-4*a*c))/2*a; r2=(-b-Math.sqrt(b*b-4*a*c))/2*a; System.out.println(r1 + ", " + r2); System.out.println(r1 == 1 && r2 == -4 ? "测试通过" : "测试失败"); }}// 三元运算符b ? x : y b成立返回x,不成立返回y
数组类型
// 整数数组// 数组大小是不可变的public class Main { public static void main(String[] args) { // 5位同学的成绩: int[] ns; int[] ns = new int[5]; // 定义数组的大小,超出数组范围将会报错 // ns = new int[] { 68, 79, 91, 85, 62 }; // 定义数组的同时初始化元素 System.out.println(ns.length); // 5 ns = new int[] { 1, 2, 3 }; System.out.println(ns.length); // 3 }}// 字符串数组public class Main { public static void main(String[] args) { String[] names = {"ABC", "XYZ", "zoo"}; String s = names[1]; names[1] = "cat"; // 把原来在该索引的字符串替换掉了 System.out.println(s); System.out.println(names[1]+" "+names[2]); }}