public class Test{
public static void main(String[] args) {
String str1 = "奋奋";//在堆中的常量池
String str2 = new String("奋奋");//在堆中创建对象
System.out.println(str1==str2);//false
String a = "a";//编译时确定
String a1 = a+1;//运行时确定,堆中创建对象
String a2 = "a1";//编译时确定,在常量池中创建
System.out.println(a1==a2);//false
final String b = "b";//b为常量,编译期确定
String b1 = b+1;//编译期确定
String b2 = "b1";//编译期确定
System.out.println(b1==b2);//true
String c = getC();//运行期确定
String c1 = c+1;//运行期确定
String c2 = "c1";//编译期确定
System.out.println(c1==c2);//false
final String d = getD();//运行期确定
String d1 = d+1;//运行期确定
String d2 = "d1";//编译期确定
System.out.println(d1==d2);//false
}
private static String getC() {
return "c";
}
private static String getD() {
return "d";
}
public static void StringBufferTest() {
StringBuffer sb = new StringBuffer();
sb.append(a).append(b).append(1);
System.out.println(sb.toString());
}
}
import java.text.MessageFormat;
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
//创建一个本地语言环境对象,该对象会根据参数设置来自动选择与之相关的语言环境(参数:语言,地区)
Locale locale_CN = new Locale("zh","CN");
Locale locale_US = new Locale("en","US");
//获取当前系统默认的语言环境
Locale locale_default = Locale.getDefault();
Scanner input = new Scanner(System.in);
//用于绑定属性文件的工具类(参数:属性文件的基名,就是前缀),可以添加语言环境,不添加就是用默认的
ResourceBundle rb = ResourceBundle.getBundle("com.se.test.info",locale_CN);
System.out.println(rb.getString("system.name"));
System.out.println(rb.getString("input.username"));
String username = input.nextLine();
System.out.println(rb.getString("input.password"));
String password = input.nextLine();
if("admin".equals(username)&&"123".equals(password)){
System.out.println(rb.getString("login.success"));
String welcome = rb.getString("welcome");
welcome = MessageFormat.format(welcome, username);
System.out.println(welcome);
} else {
System.out.println(rb.getString("login.error"));
}
}
}
//属性文件1
info_en_US.properties
system.name=EMP Manager System
input.username=Input UserName:
input.password=Input Password:
login.success=Login Success!
login.error=Login Error...
welcome=Welcome,{0}!
//属性文件2
info_zh_CH.properties
system.name=\u5458\u5DE5\u7BA1\u7406\u7CFB\u7EDF
input.username=\u8F93\u5165\u7528\u6237\u540D\uFF1A
input.password=\u8F93\u5165\u5BC6\u7801\uFF1A
login.success=\u767B\u5F55\u6210\u529F\uFF01
login.error=\u767B\u5F55\u9519\u8BEF\u3002\u3002\u3002
welcome=\u6B22\u8FCE\u4F60\uFF0C{0}\uFF01
import java.util.Random;
import static java.lang.Math.floor;
public class MathTest {
public static void main(String[] args) {
System.out.println(Math.PI);//3.141592653589793
System.out.println(Math.abs(-10));//10
System.out.println(Math.round(Math.random()*1000)/1000.0);//0.203
System.out.println(Math.sqrt(2));//1.4142135623730951
System.out.println(floor(1.23456));//1.0
Random r = new Random();
System.out.println(r.nextLong());//0-long类型最大数的随机数
System.out.println(r.nextInt(10));//int类型0-10之间的随机数,不写10就是0-int最大值之间的随机数
}
}
import java.text.*;
import java.util.*;
public class DateTest {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
Calendar c1 = Calendar.getInstance();
Calendar c2 = new GregorianCalendar();
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH);
int day = c1.get(Calendar.DAY_OF_MONTH);
int hour = c1.get(Calendar.HOUR_OF_DAY);
int minute = c1.get(Calendar.MINUTE);
int second = c1.get(Calendar.SECOND);
int millisecond = c1.get(Calendar.MILLISECOND);
StringBuilder sb = new StringBuilder();
sb.append(year).append("年").append(month).append("月").append(day).append("日").append(hour).append("时").append(minute).append("分").append(second).append("秒").append(millisecond).append("毫秒");
System.out.println(sb.toString());
//使用DateFormat可以自定义格式,比上面append方式更方便
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss SSS");
String nowDate = df.format(date);
System.out.println(nowDate);
}
}
//自定义对象
//若不想改Cat类实现Comparable接口,还可以另外定义一个实现Comparator类
public class Cat { //implements Comparable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Cat() {
super();
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + "]";
}
//重写比较方法实现排序
//@Override
//public int compareTo(Cat o) {
//return this.age - o.age;
//}
}
//实现Comparator接口,不修改原类
import java.util.Comparator;
public class CatComparator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
return o1.getAge()-o2.getAge();
}
}
//测试
import java.util.Arrays;
public class CompareTest {
public static void main(String[] args) {
Cat[] cats = {new Cat("tom",2),new Cat("jim",4),new Cat("ben",1),new Cat("ker",3)};
//Arrays.sort(cats);、、第一种方法
Arrays.sort(cats,new CatComparator());//第二种方法
Arrays.sort(cats);
System.out.println(Arrays.toString(cats));
}
}
//需要克隆对象所在的类
//1.实现Cloneable接口
public class Cat implements Cloneable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Cat() {
super();
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + "]";
}
//2.重写Object的clone()方法
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
import java.io.IOException;
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
System.out.println("向控制台输出");
System.err.println("出错了");
int[] num1 = {1,2,3,4,5};
int[] num2 = new int[num1.length];
//参数:原数组,原数组起始位置,目标数组,目标数组起始位置,长度
System.arraycopy(num1, 0, num2, 0, num1.length);
System.out.println(Arrays.toString(num2));
//currentTimeMillis()方法
Date date = new Date(System.currentTimeMillis());
DateFormat fd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS");
System.out.println(fd.format(date));
//getProperty()方法
System.out.println(System.getProperty("java.version"));//java运行时环境版本
System.out.println(System.getProperty("java.home"));//java安装目录
System.out.println(System.getProperty("os.name"));//操作系统版本
//Runtime类
Runtime rt = Runtime.getRuntime();
System.out.println(rt.availableProcessors());
System.out.println(rt.totalMemory());
System.out.println(rt.freeMemory());
System.out.println(rt.maxMemory());
//在单独的进程中执行指定的字符串命令
try {
rt.exec("notepad");//打开记事本
} catch (IOException e) {
e.printStackTrace();
}
}
}
MD5:信息摘要算法(用来对密码等敏感信息进行密文存储)。
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class MD5Test {
public static void main(String[] args) {
String password = "admin12345";//明文
try {
MessageDigest md = MessageDigest.getInstance("md5");
//通过MD5计算摘要
byte[] bytes = md.digest(password.getBytes("UTF-8"));
//使用Base64:a-z A-Z 0-9 / * 对摘要进行编码存储
String str = Base64.getEncoder().encodeToString(bytes);//密文
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
public class BinaryTree {
private Node root;
//添加节点
public void add(int data){
if(root==null){
root = new Node(data);
}else{
root.addNode(data);
}
}
//输出节点
public void print(){
root.printNode();
}
//使用内部类实现节点的创建
private class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data = data;
}
public void addNode(int data){
if(this.data>data){
if(this.left==null){
this.left = new Node(data);
}else{
this.left.addNode(data);
}
}else{
if(this.right==null){
this.right = new Node(data);
}else{
this.right.addNode(data);
}
}
}
//中序遍历输出
public void printNode(){
if(this.left!=null){
this.left.printNode();
}
System.out.print(this.data+"->");
if(this.right!=null){
this.right.printNode();
}
}
}
}
public class BinaryTreeTest {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.add(8);
bt.add(3);
bt.add(10);
bt.add(1);
bt.add(6);
bt.add(14);
bt.add(4);
bt.add(7);
bt.add(13);
bt.print();
}
}
public class LambdaTest {
public static void main(String[] args) {
//原始创建只有一个方法接口对象的方法
Eat eat1 = new EatImpl();
eat1.eat();
//进阶版创建只有一个方法接口对象,使用匿名内部类
Eat eat2 = new Eat(){
@Override
public void eat() {
System.out.println("eat bnana");
}
};
eat2.eat();
//Lambda表达式(无参数使用)
Eat eat3 = ()->{System.out.println("eat orange");};
eat3.eat();
//Lambda表达式(无参数使用)
//代码块只有一句,可以省略大括号
Eat1 eat4 = (thing)->System.out.println("eat---"+thing);
eat4.eat("orange");
//带返回值的方法
Eat2 eat5 = (thing,weight)->{
System.out.println("eat---"+thing);
return weight;
};
eat5.eat("orange",10);
}
}
//只有一个抽象方法的接口
//无参情况
interface Eat{
public void eat();
}
class EatImpl implements Eat{
@Override
public void eat() {
System.out.println("eat apple");
}
}
//有参情况
interface Eat1{
public void eat(String thing);
}
//有参情况,带返回值
interface Eat2{
public int eat(String thing, int weight);
}
//比较器应用实例
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
//测试
import java.util.Arrays;
import java.util.Comparator;
public class StudentTest {
public static void main(String[] args) {
Student[] students = {new Student("Tom",18),new Student("Jim",14),new Student("Ler",20)};
//匿名内部类
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
System.out.println(Arrays.toString(students));
//Lambda表达式
Arrays.sort(students,(o1,o2)->o1.getAge()-o2.getAge());
System.out.println(Arrays.toString(students));
}
}