(1)final
public class HelloWorld {
public void method1(final int j) {
j = 5; //这个能否执行?
}
}
不可以,因为在调用方法的时候,就一定会第一次赋值了,后面不能再进行多次赋值
(2)异或 ^
public class HelloWorld {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println(a^b); //不同返回真
System.out.println(a^!b); //相同返回假
}
}
(3)三元操作符
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
int j = 6;
int k = i < j ? 99 : 88;
// 相当于
if (i < j) {
k = 99;
} else {
k = 88;
}
System.out.println(k);
}
}
//////////////////////////////////////////////////////////////
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("今天周几?");
int day = s.nextInt();
String status = day>=6?"周末":"工作日";
System.out.println("今天是"+status);
}
}
(4)读取了整数后,接着读取字符串
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int i = s.nextInt();
System.out.println("读取的整数是"+ i);
String rn = s.nextLine();
String a = s.nextLine();
System.out.println("读取的字符串是:"+a);
}
}
(5)条件为true时 重复执行,至少会执行一次
public class HelloWorld {
public static void main(String[] args) {
//打印0到4
//与while的区别是,无论是否成立,先执行一次,再进行判断
int i = 0;
do{
System.out.println(i);
i++;
} while(i<5);
}
}
(6)n的阶乘
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int f = 1;
while(n>=1){
f = f*n;
n--;
}
System.out.println(f);
}
}
(7)总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
package digit;
import java.util.Locale;
public class TestNumber {
public static void main(String[] args) {
int year = 2020;
//总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
//直接打印数字
System.out.format("%d%n",year);
//总长度是8,默认右对齐
System.out.format("%8d%n",year);
//总长度是8,左对齐
System.out.format("%-8d%n",year);
//总长度是8,不够补0
System.out.format("%08d%n",year);
//千位分隔符
System.out.format("%,8d%n",year*10000);
//小数点位数
System.out.format("%.2f%n",Math.PI);
//不同国家的千位分隔符
System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);
}
}