目录
1.API
1.1JavaAPI的常用类
1.2Object
1.3String
1.4包装类
1.5Java.util
———————————————————————————————————————————
1.API
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
1.1 JavaAPI的常用类
1.Object 2.String 3.包装类 4.Java.util
1.2 Object
Object:所有类的基类;所有类都直接或者间接的继承Object类
Object的常用类
public class ObjectTest {
public static void main(String[] args) {
ObjectTest objectTest = new ObjectTest();
System.out.println("获取对象hash值:"+objectTest.hashCode());
System.out.println("将对象信息变为字符串返回:"+objectTest.toString());
System.out.println("获取类名" + objectTest.getClass());
}
}
1.3String
String是由多个字符组成的也可以看作是字符数组
String的常用类
String类的型转换功能
public class StringTest {
public static void main(String[] args) {
// String s = new String();
String str = "你是真牛掰";
String str1 = "NISHIZHENNIUBAI";
String str2 ="123456";
char[] chars = {'a','b','c','d'};
System.out.println("判断字符串是否为空:"+str.isEmpty());
System.out.println("获取字符串长度"+str.length());
System.out.println("判断两个字符串是否想等"+str.equals("你是真牛掰"));
System.out.println("忽略大小写进行比较"+str1.equalsIgnoreCase("NISHIZHENNIUBAI"));
System.out.println("返回改下标的值" +str.charAt(2));
System.out.println("截取字符串:" + str.substring(1));
System.out.println("截取字符串有头无尾:"+str.substring(1,4));
System.out.println("替换字符串:"+str.replace('大','超'));
System.out.println("去掉字母首尾空格:"+str1.trim());
byte[] bytes = str2.getBytes();
System.out.println("转换成Bytes类型的数组:"+ bytes.length);
System.out.println("转换成char类型的数组:"+str.toCharArray().length);
String s = String.valueOf(chars);
System.out.println("将参数数组转换成字符串 : "+ s);
System.out.println("将英文字母转换为小写"+str1.toLowerCase());
System.out.println("将英文字母装换成大写"+str1.toUpperCase());
System.out.println("讲指定字符串拼接在次字符串结尾" + str.concat("111"));
}
}
String的注意事项
String是一个用final修饰过的引用数据类型,他的值一旦创建便不能修改。
String直接用””创建对象会放到常量池中。用””号创建会先去常量池中寻找,如果有就直接返回如果没有再进行创建
String直接用构造函数来创建对象,对象不会放到常量池中而是会放在内存堆里面
StringBuffer概述
StringBuffer是用来解决String更改字符串造成的时间浪费和空间浪费的。
StringBuffer常用的方法
public class TestStringBuffer {
public static void main(String[] args) {
//在Java1.4之前都是这样式儿创建的
String str1 = new String("你是大牛逼b");
String str = "你是大牛逼";
StringBuffer stringBuffer = new StringBuffer(str);
System.out.println("追加字符串:"+stringBuffer.append("牛牛牛"));
System.out.println("指定位置追加 字符串:"+stringBuffer.insert(3,"牛牛牛"));
System.out.println("删除下标处字符串:" + stringBuffer.deleteCharAt(3));
System.out.println("删除下标区间的字符:"+stringBuffer.delete(3,6));
System.out.println("替换区间字符串:"+stringBuffer.replace(2,5,"牛牛"));
System.out.println("反转字符串:"+stringBuffer.reverse());
System.out.println("截取字符串:"+stringBuffer.substring(2,6));
}
}
String 和StringBuffer的区别
内存操作不同 :
String是每次进行更改都会产生一个新的对象
StringBuffer不会产生新的对象而是在原有的对象上进行更改
String注意事项
String 对象的字符串拼接其实是被 JVM 解释成了 StringBuffer 对象的拼接,所以这些时候 String 对象的速度并不会比 StringBuffer 对象慢。String a = “a” + “b” + “c”
StringBuffer注意事项
StringBuffer是线程安全的所以对象操作会比较慢在进行并发处理的时候,会有阻塞现象
1.4包装类
基础数据类型不具备面向对象的概念,为了弥补不足,引入了包装类方便使用面对对象的变成思想操作基本类型。
什么是包装
java中的数据类型int,double等不是对象,无法通过向上转型获取到Object提供的方法,而像String却可以,只因为String是一个对象而不是一个类型。基本数据类型由于这样的特性,导致无法参与转型,泛型,反射等过程。为了弥补这个缺陷,java提供了包装类。
包装类的实现
java为我们提供了基本数据类型的包装类,这些包装类分为两类,一种是对象型包装类,不继承任何其他类(Object的直接子类),另一种是数值型包装类,继承于Number类。
装箱与拆箱
装箱:
把基本数据类型封装成包装类
拆箱:
将包装类中包装的基本数据类型数据取出。
Integer i = new Inreger(1);//装箱
Integer i = 1;//自动装箱
i += 1;自动拆箱,通过intValue方法
ps:在java1.5之后才可以自动装箱,拆箱
public class PackageTest {
public static void main(String[] args) {
int a = 1;
//自动装箱 1.5之后
Integer c = 4;
//直接用包装类声明变量
Double d = 2.4;
//包装类--装箱
Integer integer = new Integer(a);
System.out.println(integer);
//自动拆箱
int b = integer + 1;
System.out.println(b);
//有了包装类,就可以在集合里存放了
ArrayList<Integer> arrayList = new ArrayList();
arrayList.add(1);
}
}
Integer的常用方法
Character常用的方法
1.5Java.util
Java提供了一些实用的方法和数据结构。例如,Java提供日期(Data)类、 计算函数(math)类
Math概述
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
Math常用的方法
public class TestMath {
public static void main(String[] args) {
int a = -8;
System.out.println("绝对值:"+Math.abs(a));
//随机数 0-1的随机数
double random = Math.random();
// int random1 =(int)Math.random();
System.out.println(random);
//Math.random() * (最大值-最小值)+最小值
int random2 = (int)(Math.random() * (900-1) + 1);
System.out.println(random2);
Random random1 = new Random();
//0-n的随机数
System.out.println(random1.nextInt());
}
}
Date的构造方法
public Date()
public Date (long date)
public Date(String s)
public Date(int year,int month,int date)
public Date(int year,int month,int date,int hrs,int min)
常用的方法
public long getTime()//获取时间
public void setTime(long time)//设置时间
Date时间格式化
/** 输出格式: 2016-10-26 4:48:11*/
System.out.println((new java.text.SimpleDateFormat("yyyy-M-d h:mm:ss")).format(new Date()));
/** 输出格式: 2016-10-26 16:48:11 */
System.out.println((new java.text.SimpleDateFormat("yyyy-M-d H:mm:ss")).format(new Date()));
这里需要注意的就是24时和12时的显示正则,H大写为24时计时法,h小写为12时计时法,两个h时,即hh,当为个位数时间时前面自动补0即下午4点显示04