public class test { public static void main(String[] args){ 代码块 } }
固定的格式如上,main是定义一个程序的入口,public class test,这里test是文件名,文件名必须是test不然会报错,是对应的,大小写也必须一致
0x02 print和println的区别
下面是测试代码。。。不太懂英文,写不出很骚气的英文名称
public class ceshi { public static void main(String[] args){ System.out.print("hello");//输出无换行 //System.out.println("hello");//输出有换行 } }
其实感觉是没啥差别的,就是print没有换行,println有换行。
0x03 数据类型
java中共有8种数据类型
整数型
int 占用4字节 值域是-21474836482147483647 short 占用2个字节 值域是-3276832767 long 占用8个字节 值域是-92233720368547748089223372036854774807 byte 占用1个字节 值域是-128127 java中整数默认是int类型,如果是长整型,需要在值后面加上小写的l或者大写L,推荐使用大写的L,容易区分
public class BigNumber { public static void main(String[] args){ long bigvar = 99999999999L; //超长整数型 long占用8个byte,值域是-9223372036854774808~9223372036854774807
public class MathCalc { public static void main(String[] args){ System.out.println(1+2); System.out.println(1-4); System.out.println(3*2); System.out.println(5/2.5); System.out.println(1+2);
} }
数学函数与常量
计算一个数值的平方根,使用sqrt方法,这里注意是java.lang.Math.sqrt()
public class Math { public static void main(String[] args){ double x = 4; double y = java.lang.Math.sqrt(x);//计算平方根 System.out.println(y); } }
幂次方计算,java中没有幂计算,需要借助Math中的pow方法
public class Math { public static void main(String[] args){ double x = 4; double a = 5; double c = java.lang.Math.pow(x,a); System.out.println(c); } }
improt static java.lang.Math.*; public class Math { public static void main(String[] args){ double x = 4; double a = 5; double y = sqrt(x);//计算平方根 System.out.println(y);
double c = pow(x,a); System.out.println(c); } }
数值类型之间的转换
整数转换为浮点型,会丢失部分精度
public class zhuanhuan { public static void main(String[] agrs){ int n = 123456789; float f = n;//将整数型n转换为float类型 System.out.println(f);//精度丢失 } } print:1.23456792E8
下图中,实箭头代表无信息丢失,虚箭头代表存在精度丢失的转换
整数型在转换时无精度丢失,整数转换浮点型存在精度丢失
强制类型转换
在需要进行强制转换数值前面加上(数据类型)
import static java.lang.Math.*; public class qzzhuanhuan { public static void main(String[] args) { double x = 12.432432; int nx = (int) x; System.out.println(nx); } } print: 12
结合赋值和运算符
public class ceshi { public static void main(String[] args){ int x = 10; x += 10; System.out.println(x); } } print: 20
自增与自减运算
public class zhuanhuan { public static void main(String[] agrs){ int a = 10; int b = 2; b = 2 * ++b;//b=2,先*2=4,自增=6,自增是增加原来的数,也就是+2 a = 2 * a++;//a=10,运算结束后在自增,运算结束后=20,已经得出了20,之后在自增,因为程序就运行一次,所以自增结果未输出,所以得20 System.out.println(a); System.out.println(b); } } print: a=20,b=6
关系和boolean运算
计算两个数是否相等或不相等
public class boolean1 { public static void main(String[] args){ int a = 3; int b = 10; //是否相等 System.out.println(a==b);//false //是否不相等 System.out.println(a!=b);//true //a大于b System.out.println(a>b);//false //a小于b System.out.println(a<b);//true }
+=是右结合运算 a += b += c === a += (b += c) === a = a + (b = b + c)
运算符优先级别
枚举类型
枚举相当于一个集合,自己定义数值放到一个集合中。
0x06 字符串
java中没有内置字符串类型,定义一个字符串可以使用String定义字符串变量,用””包含输出内容
public class meiju { public static void main(String[] args){ String e = "";//输出空字符串 String test = "Hello mode";//输出Hello mode System.out.println(e); System.out.println(test); } }
子串(截取)
substring 从一个大的字符串中截取一个小字符串
public class meiju { public static void main(String[] args){ String test = "nihao,shijie"; String s = test.substring(0 , 5);//从0开始截取到第5位,但不会包含第五位的字符 System.out.println(s);//输出:nihao } }
拼接
拼接使用+号
public class meiju { public static void main(String[] args){ String test = "nihao"; String nice = ",shijie"; String s = test + nice; System.out.println(s); } } print:nihao,shijie
当字符串和任何一个非字符串进行拼接时,后者都会转换为字符串
public class meiju { public static void main(String[] args){ String test = "nihao"; int a = 12; String test1 = test + a; System.out.println(test1); print: nihao12 //多个字符串放在一起,可以使用定界符 String all = String.join("/" , "T" , "i" , "m" , "e"); System.out.println(all); } print:T/i/m/e
不可变字符串
java里面没有内置的字符串函数,如果要修改字符串,需要先截取子串,然后拼接
public class meiju { public static void main(String[] args){ String test = "nihao"; String test1 = test.substring(0,3) + "Time";//截取nih三位字符,在拼接Time System.out.println(test1);//nihTime } }
public class meiju { public static void main(String[] args){ String s = "Hello"; System.out.println("hello".equals(s));
} } print:false 因为大小写不一致,所以不相等
空串和null
public class meiju { public static void main(String[] args){ String s = ""; System.out.println(s.equals(""));//检查为否为空字符串 //print:true String t = null; System.out.println(t == null);//检查字符串是否为null //print:true
public class meiju { public static void main(String[] args){ String test = "Time"; System.out.println(test.length());//统计字符串长度,print:4
int a = test.codePointCount(0 , test.length()); System.out.println(a);//统计字符串长度,print:4
//返回代码单元的第n个位置 char s = test.charAt(0); char t = test.charAt(3); System.out.println(s);//T System.out.println(t);//e
int index = test.offsetByCodePoints(0 , 1);//返回索引 //int cp = test.codePointCount(index); System.out.println(index); } }
String Api
java.lang.String;
构建字符串
import java.lang.String; public class meiju { public static void main(String[] args){ //构建字符串 StringBuilder b = new StringBuilder(); //添加新字符串 b.append("ch"); b.append("str"); System.out.println(b);//print chstr //添加变量c字符串 String c = "yes"; b.append(c); System.out.println(b);//print:chstryes String s = b.toString();
public class print { public static void main(String[] args){ //构造Scanner对象,并与标准输入流System.in关联 Scanner in = new Scanner(System.in);
System.out.print("What is your name?"); String name = in.nextLine();//输入一行 //如输入Time,会输出Time System.out.println(name);//用户输入字符换行后输出 String n = in.next(); System.out.println(n);//基本同上,具体细节未了解
//读取整数 System.out.println("How old are you? "); int age = in.nextInt(); System.out.println(age);
//读取浮点数 System.out.println("How float are you? "); double a = in.nextDouble(); System.out.println(a); } }
public class print { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("What is your name?");//time String name = in.nextLine();
System.out.println("How old are you? ");//23 int age = in.nextInt();
System.out.println("Hello," + name + ". Next tear,you'll be " + (age+1)); } } print: Hello,time. Next tear,you'll be 24
读取密码Console
import java.io.Console;
public class console { public void testConsole() { //获取console对象 Console con = System.console(); if (con != null){ System.out.println();//输出空 String name = con.readLine("name: ");//控制台提示信息 char[] pwd = con.readPassword("password:");//控制台提示信息 System.out.println("Hello! \nYour name is: " + name);// System.out.println("Your password is: " + new String(pwd)); } else { System.out.println("Couldn't get Console instance, maybe you're running this from within an IDE?"); System.exit(0); } } public static void main(String[] args){ new console().testConsole(); } }
public class Format { public static void main(String[] args){ double x = 10000.0 / 3.0;////x对应的数据类型所允许的最大非0数字位数。 System.out.print(x);//输出3333.3333333333335
//printf可以用8个字符的宽度和小数点后两个字符的精度打印x, System.out.printf("%8.2f" , x);//输出3333.33 //printf,可以使用多个参数 String name = "Time"; int age = 23; System.out.printf(" Hello , %s .Next Year, you'll be %d" ,name ,age);//Hello , Time .Next Year, you'll be 23 } }
日期转换符
import java.util.Date; public class Format { public static void main(String[] args){ //格式化输出日期 System.out.printf("%tc" , new Date());//周日 7月 21 00:39:02 CST 2019 //24小时制 System.out.printf("\n%tT" , new Date());//00:39:40
public class block { public static void main(String[] args){ int x = 10; int y = 10; if ( x == y){ //这是错误的用法 int x = 11; } } }
条件语句(if)
条件判断,判断某的条件是否成立,可以嵌套多个if判断语句,可以增加多个条件进行判断
public class control { public static void main(String[] args){ //判断x==y int x = 10; int y = 11; if (x == y) { System.out.println("相等"); }else { System.out.println("不相等"); } } }
public class control { public static void main(String[] args){ //do while 先满足一个条件,在循环 Scanner in = new Scanner(System.in); System.out.println("How much money do you need to retire? "); double goal = in.nextDouble();//获取输入的浮点类型值 System.out.println("How much money will you contribute every year? "); double payment = in.nextDouble();//获取输入的浮点类型值 System.out.println("Interest rate in %: "); double InterestRate = in.nextDouble();//获取输入的浮点类型值 double balance = 0; //创建浮点型变量 int year = 0;//创建整型变量
String input = null;//创建一个字符串函数
//循环 do { balance += payment; //balance = balance + payment; 先加等于 //创建一个浮点型变量,将balance×InterestRate/100得出的值赋给新建的变量interest double interest = balance * InterestRate /100; balance += interest;//balance = balance + interest;在将balance和interest相加,值赋给balance year++;//years自增 //输出循环次数和balance最后得出的值 System.out.printf("After year %d , your balance is %,.2f%n" , year , balance); System.out.print("Ready to retire? (Y/N)"); input = in.next();//输入Y/N,赋值给input } while (input.equals("N"));//获取input的值,如果等于N,重复执行循环 } }
确定循环(for)
import java.util.*; import java.io.InputStream; import java.util.Scanner; public class for1 { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("How much money do you need to retire? "); int k = in.nextInt();//获取输入整数类型值, System.out.println("How much money will you contribute every year? "); int n = in.nextInt();//获取输入整数类型值 //for循环 int t = 1; for (int i = 1; i<=k; i++){ t = t * (n - i + 1) / i; System.out.println("Your odds are 1 in " + t + ". Good luck!"); } } /* 复盘: 假定:输入的k=2 n=10 i=1,1<=2,条件成立,执行循环体,t=1 t=1*(10-1+1)/1,t=10,输出,i=1,i自增1。 i=2,2<=2,条件成立,执行循环体,t=10 t=10*(10-2+1)/2, t=45,输出,i=2,i自增1。 i=3,3<=2,条件不成立,循环结束 */ }
多重循环(switch)
import java.util.*; import java.util.Scanner;
public class multiple { public static void main(String[] args){ //switch多重循环 Scanner in = new Scanner(System.in); System.out.println("Select an option (1,2,3,4):"); int t = in.nextInt();//获取输入的整数值 //开始循环 switch (t){ case 1: System.out.println("这是第一层");//当值为1,输出代码 break;//退出循环 case 2: System.out.println("这是第二层"); break; case 3: System.out.println("这是第三层"); break; case 4: System.out.println("这是第四场"); break; } } }
import java.math.*; import java.util.*; public class BigInteger1 { public static void main(String[] args){ BigInteger a = BigInteger.valueOf(100);//将普通数值转换为大数值 System.out.println(a);//100。。没啥区别 BigInteger b = BigInteger.valueOf(13); BigInteger c = a.add(b);//c = a + b BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));//d = c * (b + 2) System.out.println(b);//13 System.out.println(c);//113 System.out.println(d);//1695 } }
public class BigInteger1 { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("How many nuber do you need to draw? "); int k = in.nextInt(); System.out.println("How many nuber do you need to draw? "); int n = in.nextInt();
BigInteger t = BigInteger.valueOf(1);
for (int i = 1; i <= k; i++) { t = t.multiply(BigInteger.valueOf((n - i + 1)).divide(BigInteger.valueOf(i))); System.out.println("Your odds are 1 in " + t + ". Good luck!"); } /* print: How many nuber do you need to draw? 10 How many nuber do you need to draw? 20325 Your odds are 1 in 20325. Good luck! Your odds are 1 in 206542650. Good luck! Your odds are 1 in 1399119911100. Good luck! Your odds are 1 in 7107529148388000. Good luck! Your odds are 1 in 28884998459048832000. Good luck! Your odds are 1 in 97804604782339345152000. Good luck! Your odds are 1 in 283828963078348779631104000. Good luck! Your odds are 1 in 720641737255927551483373056000. Good luck! Your odds are 1 in 1626488400986628483697972987392000. Good luck! Your odds are 1 in 3303397942403842450390583137393152000. Good luck! */ }
public class array { public static void main(){ //声明整数型数组 //int[] a; //初始化数组,创建一个可以存储100个整数的数组 int[] a = new int[100]; for (int i = 0; i <= a.length; i++){ a[i] = i; } //创建存储10个字符串的数组 String[] names = new String[10]; for (int i=0; i<10; i++) { names[i] = ""; } } }
for each
public class shuzu { public static void main(String[] args) { //声明整数型数组 //int[] a; //初始化数组,创建一个可以存储100个整数的数组 int[] a = new int[100]; int t = 0; for (int e : a){ e = t++; //将t自增的值赋给e System.out.println(e);//输出e,会从输出从下标0开始一直到99结束的100个整数值 } } //同上 for (int i=0; i<=a.length; i++) { System.out.println(a[i]); } }
数组初始化和匿名数组
public class niming { public static void main(String[] args){ //创建数组同时初始化值 int[] small = {2,3,4,5}; System.out.println(small[0]); for (int e:small ) { System.out.println(small[e]); } //重新初始化数组 small = new int[]{21,232,435};
//简写 int[] an = {12,232,434,543}; small = an; } }
数组拷贝
import java.lang.reflect.Array; import java.util.*; public class copy { public static void main(String[] args){ int[] small = {2,3,4,5,6,7}; int[] luck = small; small[5] = 32; //System.out.println(luck[5]);
int[] ck; ck = Arrays.copyOf(small, small.length);//复制small数组值到新数组ck中 for (int e : small ) { System.out.println(ck[e]);//遍历循环 } //luck = Array.copyOf(small, small.length-1);
import java.lang.reflect.Array; import java.util.*; public class copy { public static void main(String[] args){ int[] small = {2,3,4,5,6,7}; int[] luck = small; small[5] = 32; //System.out.println(luck[5]);
int[] ck; ck = Arrays.copyOf(small, small.length);//复制small数组值到新数组ck中 for (int e : small ) { System.out.println(ck[e]);//遍历循环 } //luck = Array.copyOf(small, small.length-1);
} } print: hello,!
在命令行中运行
import java.lang.reflect.Array; import java.util.*; public class copy { public static void main(String[] args){ int[] small = {2,3,4,5,6,7}; int[] luck = small; small[5] = 32; //System.out.println(luck[5]);
int[] ck; ck = Arrays.copyOf(small, small.length);//复制small数组值到新数组ck中 for (int e : small ) { System.out.println(ck[e]);//遍历循环 } //luck = Array.copyOf(small, small.length-1);
} } 运行命令:java dos -h cruel world print: Hello, cruel world!
数组排序
使用Arrays.sort方法
import java.util.*; public class drawing { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("How many number do you need to draw? "); int k = in.nextInt();//获取输入的值
System.out.println("What is the highest number you can draw? "); int n = in.nextInt();//获取输入的值