JAVA常用类
1. Object - 对象
介绍Object类
-Object是所有类的基类,所有类直接继承它或间接继承它,这里我详细介绍它内部的方法:
-Class getClass():获取当前对象的类对象,类对象是类加载时期创建的,一个类独一份,里面
保存了与类相关的信息
-int hashcode():根据对象的引用地址、类型等参数数值生成一个哈希值,该值在同一个对象中是
相等的,在不同对象中绝大概率下是不等的
-boolean equals(Object obj):对两个对象的引用地址进行比较,相等返回true,不等返回
false
-String toString():返回一个getClass().getName()+"@"+哈希值的字符串
-void finalize():在GC(垃圾处理器)回收垃圾对象时调用该对象中的此方法,可使用
System.gc();手动回收垃圾对象
-wait():使当前线程处于等待状态,被唤醒才能进入就绪状态
-notify():唤醒一个处于等待状态的线程
-notifyAll():唤醒所有处于等待状态的线程
2. Integer… - 包装
package com.qf.a_patbox;
public class Test {
public static void main(String[] args) {
Integer integer = Integer.valueOf(286);
int a = integer.intValue();
String str1 = String.valueOf(a);
String str2 = a + "";
int b = integer.parseInt(str1);
Integer i1 = Integer.valueOf(10);
Integer i2 = Integer.valueOf(10);
System.out.println(i1 == i2);
}
}
3. String - 字符串
package com.qf.b_string;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String a = "Hello";
String b = "Hello";
System.out.println(a==b);
System.out.println(a.concat(" world!"));
System.out.println(a.charAt(0));
System.out.println(a.contains("ell"));
System.out.println(Arrays.toString(a.toCharArray()));
System.out.println(a.indexOf("ell"));
System.out.println(a.lastIndexOf("l"));
System.out.println(a.length());
System.out.println(" hello ".trim());
System.out.println("hello".toUpperCase());
System.out.println("HEllo".toLowerCase());
System.out.println(a.startsWith("He"));
System.out.println(a.endsWith("lo"));
System.out.println(a.replace("o", "l"));
System.out.println(a.replaceAll("o", "l"));
System.out.println(Arrays.toString("a,b,c,d".split(",")));
System.out.println(a.substring(1));
System.out.println(a.substring(1,3));
System.out.println(String.valueOf(3));
}
}
4. StringBuilder - 可变长字符串
package com.qf.c_stringbuilder;
public class Test {
public static void main(String[] args) {
StringBuilder builder1 = new StringBuilder("hello");
long start = System.currentTimeMillis();
for(int i=0;i<10000;i++) {
builder1.append(i);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
StringBuilder builder2 = new StringBuilder("hello");
System.out.println(builder2.append(" world!"));
System.out.println(builder2.reverse());
}
}
5. BigDecimal - 精确运算
package com.qf.d_bigdecimal;
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
BigDecimal decimal1 = new BigDecimal("1.0");
BigDecimal decimal2 = new BigDecimal("0.6");
System.out.println(decimal1.add(decimal2));
System.out.println(decimal1.subtract(decimal2));
System.out.println(decimal1.multiply(decimal2));
System.out.println(decimal1.divide(decimal2, 2, BigDecimal.ROUND_UP));
System.out.println(decimal1.divide(decimal2, 2, BigDecimal.ROUND_FLOOR));
System.out.println(decimal1.divide(decimal2, 2, BigDecimal.ROUND_HALF_UP));
}
}
6. BigInteger - 大数
package com.qf.e_biginteger;
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
BigInteger i1 = new BigInteger(Integer.MAX_VALUE+"");
BigInteger i2 = new BigInteger("1");
System.out.println(i1.add(i2));
}
}
7. Random - 随机数
package com.qf.f_random;
import java.util.Arrays;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random random = new Random();
int[] arys = new int[10];
for(int i=0;i<10;i++) {
arys[i] = random.nextInt(26) + 75;
}
System.out.println(Arrays.toString(arys));
}
}
8. Math - 数学
package com.qf.g_math;
public class Test {
public static void main(String[] args) {
System.out.println(Math.ceil(1.2));
System.out.println(Math.ceil(1.8));
System.out.println(Math.floor(1.2));
System.out.println(Math.floor(1.8));
System.out.println(Math.round(1.2));
System.out.println(Math.round(1.8));
System.out.println(Math.pow(3,4));
System.out.println((int)(Math.random()*26) + 75);
}
}
9.Date - 日期
package com.qf.h_date;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
System.out.println(date.getTime());
}
}
10. Calendar - 日历
package com.qf.i_calendar;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("年:"+calendar.get(Calendar.YEAR));
System.out.println("月:"+(calendar.get(Calendar.MONTH) + 1));
System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("时:"+calendar.get(Calendar.HOUR));
System.out.println("分:"+calendar.get(Calendar.MINUTE));
System.out.println("秒:"+calendar.get(Calendar.SECOND));
calendar.setTime(new Date());
System.out.println(calendar.getTime());
calendar.set(Calendar.YEAR, 1999);
calendar.add(Calendar.YEAR, 2);
System.out.println("年:"+calendar.get(Calendar.YEAR));
}
}
11. SimpleDateFormat - 日期格式
package com.qf.j_simpledateformat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date());
System.out.println(time);
System.out.println(sdf.parse(time));
}
}
12. System - 系统
package com.qf.k_system;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] a = {1,2};
int[] b = new int[5];
System.arraycopy(a, 0, b, 0, a.length);
System.out.println("数组复制:"+Arrays.toString(b));
System.out.println("时间戳:"+System.currentTimeMillis());
new Person();
System.gc();
System.out.println("获取系统参数:"+System.getProperties());
new MyFrame("我是一个窗口!");
}
}
class Person{
@Override
protected void finalize() throws Throwable {
System.out.println("我在被回收...");
}
}
class MyFrame extends Frame{
private static final long serialVersionUID = -5161558921732028687L;
public MyFrame() {}
public MyFrame(String title) {
super(title);
init();
}
public void init() {
setBounds(100, 100, 800, 400);
setBackground(Color.BLACK);
setResizable(false);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}