常用类
String
不可变长的字符序列 "abc" Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现
其内部是由字符串组表示 private final char value[];
String : 不可变长字符串
StringBuilder:可变长字符串,线程不安全的
StringBuffer:可变长字符串,线程安全的
实例:
public class StringConstructor {
//String 不可变长的字符序列,内部其实是字符串组表示 private final char
value[];this.value=value[];
public static void main(String[] args) throws UnsupportedEncodingException {
//String 常量池中创建对象地址
String str="abc";//创建一个新对象,字符串常量池中
String str1="abc";//没有创建对象,直接在已有的常量池中寻找
//new String(String original):new一个新对象并初始化值
String str2=new String("abc");//堆中创建一个新对象, 在常量池中找到已有的对象
String str3=new String("bcd");//堆中创建一个新对象,常量池中没有在常量池中再创建一个再用
System.out.println(str);//abc string类重写了tostring方法,直接打印内容
//构造器
//空构造:this.value="".value
String str4=new String();
System.out.println(str4);
//new一个新的string对象,以字符序列表示出来char类型的字符数组
String str5=new String(new char[]{'s','h','s','x','t'});
System.out.println(str5);
//new一个新对象,以字符序列表示char类型字符数组的子数组
String str6=new String(new char[]{'s','h','s','x','t'}, 1, 3);
System.out.println(str6);
//String(byte[] bytes); 使用默认的字符集解码指定的byte数组,构造一个新的string对象
String str7="徐敏生";
System.out.println(Arrays.toString(str7.getBytes()));
String str8=new String(new byte[]{-27, -66, -112, -26, -107, -113, -25, -108, -97});
System.out.println(str8);
//从指定索引开始规定长度表示字符序列
String str9=new String(new byte[]{-27, -66, -112, -26, -107, -113, -25, -108, -97},3,6);
System.out.println(str9);
//指定编码格式编译
String str10=new String(new byte[]{-27, -66, -112, -26, -107, -113, -25, -108, -97},"GBK");
System.out.println(str10);
}
}
方法
import java.util.Arrays;
import javax.naming.ldap.StartTlsRequest;
public class StringMethod {
public static void main(String[] args) {
String str1=new String("Hannlozb");
String str2=new String("XMSzlh");
//char charAt(int index)返回指定索引处的 char值。
System.out.println(str1.charAt(2));
//int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
System.out.println(str1.indexOf("n"));
//int indexOf(String str, int fromIndex)返回指定字符从指定位置开始后第一次出现的索引
System.out.println(str1.indexOf("n",1));
//int lastIndexOf(String str)返回指定子字符串在此字符串中最右边出现处的索引。
System.out.println(str1.lastIndexOf("n"));
//int codePointAt(int index)返回指定索引处的字符(Unicode代码点)
System.out.println(str1.codePointAt(3));
//int compareTo(String anotherString)按字典顺序比较两个字符串。
System.out.println(str1.compareTo(str2));
//int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写
System.out.println(str1.compareToIgnoreCase(str2));
//String concat(String str) 将指定字符串连接到此字符串的结尾返回新串
System.out.println(str1.concat(str2));
//boolean contains(CharSequence s)判断字符串是否包含指定的char值序列,如果包含则返回true
System.out.println(str1.contains("ann"));
//static String copyValueOf(char[] data, int offset, int count)字符数组转换为字符串并按指定的位置到指定长度转换
System.out.println(str1.copyValueOf(new char[]{'s','h','s','x','t'}, 2,3));
//boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
//boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
System.out.println(str1.startsWith("s"));
System.out.println(str2.endsWith("h"));
//byte[] getBytes(Charset charset)使用指定的charset编码格式将字符串编码成byte类型数组
System.out.println(Arrays.toString(str1.getBytes()));
//void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)将字符从此字符串复制到目标字符数组。
char[] newArr=new char[]{'s','h','s','x','t'};
"haha".getChars(0, 2, newArr, 2);
System.out.println(Arrays.toString(newArr));
//String replace(char oldChar, char newChar) 指定的字符串替换原有的字符串
System.out.println(str1.replace("a","s"));
//tring[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。
String stri="好喝=冰淇凌+奶茶";
System.out.println(Arrays.toString(stri.split("=")));
//String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
//String substring(int beginIndex, int endIndex) endIndex不包含,返回一个新字符串,它是此字符串的一个子字符串。
System.out.println(str1.substring(2,5));
//char[] toCharArray() 将此字符串转换为一个新的字符数组。
System.out.println(Arrays.toString(str1.toCharArray()));
//String toLowerCase() 转小写
//String toUpperCase() 转大写
System.out.println(str1.toLowerCase());
System.out.println(str2.toUpperCase());
//String trim()去掉字符串的副本前端空白和尾部空白并返回副本
System.out.println(str1.trim());
//valueOf() 参数转为字符串
System.out.println(String.valueOf(123.1).length());
}
}
StringBuiAndBuf
public class StringBuiAndBuf {
public static void main(String[] args) {
//构造器
StringBuilder s=new StringBuilder();//构造一个不带字符的对象,初始容量为16个字符
System.out.println(s);
System.out.println(s.capacity());
System.out.println(s.length());
StringBuilder s2=new StringBuilder(12);//构造一个字符串对象并初始指定容量
System.out.println(s2);
System.out.println(s2.capacity());
System.out.println(s2.length());
StringBuilder s1=new StringBuilder("abcd");//构造一个字符串对象并初始指定内容
System.out.println(s1);
System.out.println(s1.capacity());
System.out.println(s1.length());
//方法
//append扩容=原容量size*2+2,如果还装不下则按内容容量扩容
StringBuilder s3=s.append("12345678901234567");
s.append(123.123);
System.out.println(s);
System.out.println(s==s3);//返回this,扩容后没有创建新对象
System.out.println(s.capacity()); //34
System.out.println(s.length()); //24
//StringBuilder delete(int start, int end)删除start到end之间的数 不包括end
System.out.println(s.delete(2, 7));
//StringBuilder insert(int offset, boolean b)插入 指定点插入boolean值内容
System.out.println(s.insert(2, false));//还可以插入别的内容,输入什么内容则插入什么类型的内容
//StringBuilder reverse() 倒叙
System.out.println(s.reverse());
}
}
Math类
public class MathMethod{
public static void main(String[] args) {
//常用math类方法
//static double floor(double a) 向下取整
System.out.println(Math.floor(1.66));
//static double ceil(double a) 向上取整
System.out.println(Math.ceil(3.26));
//static double max(double a, double b) 返回两个 double 值中较大的一个。返回什么值的类型取决于比较的两个值类型,相同类型返回相同各类型,不同类返回较大的值类型
System.out.println(Math.max(1.5, 5));//也可以写int类型的值,向上自动类型提升
//static int min(int a, int b) 返回两个 int 值中较小的一个。
System.out.println(Math.min(1.7, 3.6));
//static long round(double a) 返回最接近参数的 long值
System.out.println(Math.round(4.6));
}
}
基本数据类型的包装类型
byte ----- Byte
short ----- Short
int ----- Integer
long ----- Long
float ----- Float
double ----- Double
char ----- Character
boolean ----- Boolean
自动装箱: 从基本数据类型->包装类型
自动拆箱: 从包装类型->基本数据类型
实例:
public class DataType {
public static void main(String[] args) {
//想要运用包装类引用类型的方法:包装类型:自动装箱:Integer i=12;
//参与运算:基本数据类型:自动拆箱:int j=i;
int m=12;
int n=12;
Integer m1=12;
Integer m2=12;
Integer n1=128;
Integer n3=128;
Integer n2=new Integer(12);
System.out.println(m==n);// true比较基本数据类型直接比较内容
System.out.println(m1==m2);//true 常量值如果有了直接去常量池中
System.out.println(m==m1);// true 基本数据类型和引用比较,引用类型自动拆箱转换成基本数据类型再进行比较
System.out.println(m==n2);//true 同上
System.out.println(m1==n2);//false new对象比较了地址
System.out.println(n1==n3);//Integer缓存区对象的范围再[-128,127]之间,超出范围则new新的对象
Double d1=2.1;
Double d2=2.1;
System.out.println(d1==d2); //false 与Integer不同,double总是new一个新的对象,没有缓存区范围
//方法
//static int parseInt(String s, int radix) 将字符串参数作为有符号的十进制整数进行解析, radix是指定使用什么进制解析参数s
System.out.println(Integer.parseInt("101"));// 101
System.out.println(Integer.parseInt("101",2));//5,二进制表示
}
}
日期类
实例:
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
//构造器
Date date=new Date();
Date date5=new Date();
System.out.println(date);//Date()根据当前时间创建日期对象(本地)
System.out.println(date.getTime());//1560173441144 获取从1970年到当前时间的毫秒数
Date date2=new Date(date.getTime());
Date date3=new Date(1560173441144L);
System.out.println(date2);
System.out.println(date3);
//方法
//boolean after(Date when)测试此日期是否在指定日期之后。
System.out.println(date.after(date3));//返回值为boolean类型
//boolean before(Date when) 测试此日期是否在指定日期之前。
System.out.println(date.before(date3));
//Object clone()返回此对象的副本。
System.out.println(date.clone());
//int compareTo(Date anotherDate)比较两个日期的顺序。
System.out.println(date.compareTo(date3));
System.out.println(date.compareTo(new Date()));
System.out.println(date.compareTo(date5));
//boolean equals(Object obj)比较两个日期的相等性。
System.out.println(date.equals(date5));
}
}
SimpleDateFormat 日期格式类
以指定格式把字符串与日期对象之间进行转换
实例:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatTest {//日期格式类,指定格式把字符串与日期对象之间进行转换
public static void main(String[] args) throws ParseException {
//构造器
//方法
//format(Date) 日期对象准为字符串
//SimpleDateFormat() 默认的转换格式
SimpleDateFormat simple=new SimpleDateFormat();
System.out.println(simple.format(new Date()));
//SimpleDateFormat(String pattern) 指定转换格式
SimpleDateFormat simple1=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
System.out.println(simple1.format(new Date()));
//parse(String) 字符串准为日期对象
String string="2019年6月10日";
SimpleDateFormat sim=new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(sim.parse(string));
}
}
枚举类
表示所有可能|所有情况
所有的枚举都隐式的继承java.lang.Enum
枚举类中的所有成员,都是当前类型的一个实例 相当于public static final修饰
实例:
import java.util.Arrays;
public class EnumText {
public static void main(String[] args) {
Fruit f=Fruit.apple;
//方法:
System.out.println(f.name());//属性名称
System.out.println(f.ordinal());//枚举类中的索引位置
System.out.println(Arrays.toString(f.values()));
switch (f) {
case apple:
f.setName("苹果");
break;
case Pear:
f.setName("梨子");
break;
case watermelon:
f.setName("西瓜");
break;
default:
break;
}
f.show();
}
}
enum Fruit{//枚举中的所有成员都是当前类型的实例
apple,//public static final 静态不可改变
Pear,
watermelon;
private String name;
private Fruit() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.println (name);
}
}
File文件类
文件和目录路径名的抽象表示形式
实例:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class FIleText {
public static void main(String[] args) throws IOException {
//构造器,不能有空构造,至少得告诉路径|文件名|,文件没有真实存在没有影响
File file=new File("D:/");
File file1=new File("hanlo.txt");
//File(File parent, String child)
File file2=new File(file,"hanlo.txt");
//File(String parent, String child)
File file3=new File("D:/","hanlo.txt");
//方法
File file4=new File("D:/test.txt");
File file5=new File("D:/AAA");
File file6=new File("AAA");
//boolean createNewFile()不存在文件时创建文件
System.out.println(file4.createNewFile());//返回true创建成功
//boolean setReadOnly()设置只读模式
System.out.println(file4.setReadOnly());//返回true设置成功
//boolean canWrite() 检测是否能够修改,只读模式下不能修改
System.out.println(file4.canWrite());//false 不能修改
//boolean exists() 测试此抽象路径名表示的文件或目录是否存在
System.out.println(file4.exists());//返回true表示文件存在
//File getAbsoluteFile()返回此抽象路径名的绝对路径名形式
System.out.println(file4.getAbsolutePath());//返回的是newfile 返回file文件可以用file类方法
//String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串
System.out.println(file4.getCanonicalPath());//返回string类型可以string类的方法
//String getName()返回由此抽象路径名表示的文件或目录的名称:相对路径
System.out.println(file4.getName());
//String getParent()返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
System.out.println(file4.getParent());
//File getParentFile() 返回父目录文件
System.out.println(file4.getParentFile());
//boolean isAbsolute() 测试此抽象路径名是否为绝对路径名
System.out.println(file4.isAbsolute());//返回true表示是绝对路径
//boolean isDirectory()测试此抽象路径名表示的文件是否是一个目录
System.out.println(file4.isDirectory());//返回false不是文件目录
// boolean isFile() 测试此抽象路径名表示的文件是否是一个标准文件。
System.out.println(file4.isFile());//返回true表示是一个文件‘
//long lastModified() 返回此抽象路径名表示的文件最后一次被修改的时间
System.out.println(file4.lastModified());//1560180945754
System.out.println(new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(new Date(file4.lastModified())));//2019/06/10 11:35:45
//String[] list() 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录,目录里面的文件
// File[] listFiles()
System.out.println(Arrays.toString(file4.list()));//null
System.out.println(Arrays.toString(file4.listFiles()));//null
//boolean mkdir() 创建此抽象路径名指定的目录。
// boolean mkdirs() 创建目录及里面的目录文件
//boolean renameTo(File dest)
file5.renameTo(file4);
System.out.println(file4.toString());
file4.delete();
}
}