Java常用类
String类
- String
- 字符串一旦创建不可变
- 双引号括起来的字符串存储在字符串常量池中
- 字符串的比较必须使用equals方法
- String已经重写了toString()和equals()方法
2.String的构造方法
- String s = "";
- String s = new String();
- String s = new String(byte数组);
- String s = new String(byte数组,起始下标,长度);
- String s = new String(char数组);
- String s = new String(char数组,起始下标,长度);
package com.Cedric.javase.commentclass;
/*
字符串常用类测试
*/
public class CommentClassTest01 {
public static void main(String[] args) {
//String对象创建后是不可变的
String str = "abc";
System.out.println(str);//abc
char[] chars = {'a','b','c'};
String str2 = new String(chars);
System.out.println(str2);//abc
/*
== 判断两个字符串的内存地址是否一样
str = abc;
str2 = abc;
两个abc都创建在 方法区的字符串常量池中,是同一个abc
str是直接指向常量池中的abc
str2 new了一个对象,在堆内存中开辟内存地址,所以str2指向的是堆内存中开辟的地址
所以结果为false
*/
System.out.println(str2 == str);//false
// String(byte[] bytes, Charset charset)
byte[] bytes = {65,66,67,68};
// String str3 = new String(); //创建好字符串对象后什么都不输入则为空
// System.out.println(str3);
String str4 = new String("xy"); //String也可以new对象之后再传值
System.out.println(str4);
String str5 = new String(bytes,0,4);
System.out.println(str5);
}
}
3.String类常用的21个方法(多敲多练)
package com.Cedric.javase.commentclass;
public class CommentClassTest02 {
public static void main(String[] args) {
//charAt()
char c = "中国人".charAt(0);
System.out.println(c);//中
//compareTo(String anotherString)
String str1 = "xyz";
String str2 = "zyx";
System.out.println(str1.compareTo(str2));//-2
System.out.println(str2.compareTo(str1));//2
System.out.println(str1.compareTo("xyz"));//0
// compareToIgnoreCase(String str) //忽略大小写
String str3 = "abC";
String str4 = "Bca";
System.out.println(str3.compareToIgnoreCase(str4));//-1
//String concat(String str)
String str5 = "abcd";
System.out.println(str5.concat("ef"));//abcdef
//boolean contains(CharSequence s)
System.out.println("java.net.util.c++.script".contains("java"));//true
// boolean endsWith(String suffix)
System.out.println("http://www.baidu.com".endsWith("com"));//true
//boolean equals(Object anObject)
String s = "java";
System.out.println("c++".equals(s));//false
System.out.println("acc".equals("acc"));//true
System.out.println(s.equals("JAVA"));//false
//boolean equalsIgnoreCase(String anotherString)
System.out.println("JAVA".equalsIgnoreCase(s));//true
//byte[] getBytes()
String s1 = "abc";
byte[] bytes = s1.getBytes();
for(int i=0;i<bytes.length;i++){
System.out.println(bytes[i]);//97 98 99
}
// int indexOf(int ch)
String s2 = "Hello Jack";
System.out.println(s2.indexOf("o")); //4
//boolean isEmpty()
System.out.println("".isEmpty()); //true
//int length()
System.out.println("abcdefg".length());//7
// String replace(char oldChar, char newChar)
String s3 = "Hello Jack";
System.out.println(s3.replace("Jack","Marry"));//Hello Marry
//int lastIndexOf(String str)
System.out.println("ccddc".lastIndexOf("c"));//4
// boolean startsWith(String prefix)
System.out.println("https://wwwbaidu.com".startsWith("https://"));//true
//split()
String[] s4 = "2018/8/8".split("/");
// System.out.println( s4.split("-"));
for (int i = 0; i < s4.length; i++) {
System.out.println(s4[i]);
}
String[] strings = "中 通 快 递".split(" ");
System.out.println(strings);//中通快递
// String substring(int beginIndex)
String a = "12456";
System.out.println(a.substring(0,2));// 12
//char[] toCharArray()
char[] chars = "我是中国人".toCharArray();
for (int i = 0; i < chars.length ; i++) {
System.out.println(chars[i]);
}
// String toUpperCase()
System.out.println("ofcurse".toUpperCase());//OFCURSE
// String toLowerCase()
System.out.println("OFCURSE".toLowerCase());//ofcurse
//String trim()
System.out.println(" 2121 ".trim());//2121
System.out.println(String.valueOf(10));//10
boolean b = true;
System.out.println(String.valueOf(b));//true
}
}
StringBuffer/StringBuilder
- StringBuffer/StringBuilder 可以看做可变长度字符串
- StringBuffer/StringBuilder初始化容量16
- StringBuffer/StringBuilder是完成字符串拼接操作的,方法名:append
- StringBuffer是线程安全的,StringBuilder是非线程安全的
- 频繁进行字符串拼接不建议使用 “ + ”
package com.Cedric.javase.commentclass;
// StringBuffer线程安全的
public class BufferTest01 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(6);
stringBuffer.append(false);
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
stringBuffer.append('我');
System.out.println(stringBuffer);
}
}
package com.Cedric.javase.commentclass;
// 非线程安全的
public class BuilderTest01 {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("abc");
stringBuilder.append(1232);
stringBuilder.append(5678);
stringBuilder.append('你');
System.out.println(stringBuilder);
}
}
八种基本数据类型对应的包装类
包装类有什么用?
方便编程
八种包装类的类名是什么?
- Byte
- Short
- Integer
- Long
- Double
- Float
- Boolean
- charcter
package com.Cedric.javase.commentclass;
import java.text.DecimalFormat;
public class Integer01 {
public static void main(String[] args) {
int a = 10;
doSome(a);
Integer integer = new Integer(3);
Float f = new Float(integer);
System.out.println(f);
int i = f.intValue();
System.out.println(i);
Integer integer1 = new Integer("123");
System.out.println(integer1);
DecimalFormat decimalFormat = new DecimalFormat("###,##.00000");
String s = decimalFormat.format(12);
System.out.println(s);
}
public static void doSome(Object obj){
System.out.println(obj);
}
}
package com.Cedric.javase.commentclass;
public class Integer02 {
public static void main(String[] args) {
int retValue = Integer.parseInt("123");
System.out.println(retValue + 2); //125
Float f = Float.parseFloat("33259");
System.out.println(f + 1);//33260.0
Double d = new Double("321");
System.out.println(d + 1); //322.0
}
}
学习Integer,其他的模仿Integer
什么是自动装箱和自动拆箱,代码怎么写?
Integer x = 100;
Integer y = 100;
System.out.println(x == y); //true
Integer x = 128;
Integer y = 128;
System.out.println(x == y); //false
Integer类常用方法
Integer.valueOf() // 将基本类型int转换成包装类
Integer.parseInt("123") ;
Integer.parseInt("中文"); //NumberFormatException
Integer String int 三种类型互相转换
package com.Cedric.javase.integer;
public class IntegerTest08 {
public static void main(String[] args) {
//String --> int
int i1 = Integer.parseInt("100"); //i1是100数字
System.out.println(i1 + 1); //101
//int --> String
String s2 = i1 + "";//"100"字符串
System.out.println(s2 + 1);//1001
//int --> Integer
//自动装箱
Integer x = 100;
//Integer --> int
//自动拆箱
int y = x;
//String --> Integer
Integer integer = Integer.valueOf("123");
//Integer --> String
String s3 = String.valueOf(integer);
}
}
日期类
//获取当前系统时间
Date d = new Date();
// 日期格式化:Date --> String
// yyyy-MM-dd HH:mm:ss SSS
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String s = sdf.format(new Date());
// String --> Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date d = sdf.parse("2008-8-8 08:08:08 888");
//获取毫秒数
long begin = System.currentTimeMillis();
Date d = new Date(begin - 1000 * 60 * 60 * 24); //获取昨天这个时候的毫秒数
数字类
3.1.DecimalFormat数字格式化 ###,##.## 表示加入千分位,保留两个小数 ###,###.0000 表示加入千分位,保留四位小数,不够补0 3.2.BigDecimal 财务软件中通常使用BigDecimal(精度较高)
随机数
//产生int类型随机数?
Random r = new Random();
int i = r.nextInt();
//怎么产生某个范围之内的随机数
Random r = new Random();
int i = r.nextInt(101); //产生[0 - 100]之间的随机数
枚举
package com.Cedric.javase.enum1;
/*
以下程序返回布尔类型的值虽然是改良版,但是布尔值只有true and false
这样的话如果需要进行三个或以上的进行判断和列出来就会出现问题
*/
public class EnumTest01 {
public static void main(String[] args) {
boolean retValue1 = divide(10,5);
System.out.println( retValue1 ? "计算成功" : "计算失败");
boolean retValue = divide(10,0);
System.out.println(retValue ? "计算成功" : "计算失败");
}
public static boolean divide(int a,int b){
try{
int c = a / b;
return true;
}catch (Exception e){
return false;
}
}
}
package com.Cedric.javase.enum1;
// 采用枚举的方式改造程序
/*
总结:
1.枚举是一种引用数据类型
2.枚举类型怎么定义?
enum 枚举类型名{
枚举值1,枚举值2
}
3.结果只有两种情况的,建议使用布尔类型
结果超过两种并且还是可以一枚一枚列出来的,建议使用枚举类型
*/
public class EnumTest02 {
public static void main(String[] args) {
Result r = divide(10,0);
System.out.println(r == Result.SUCCESS ? "计算成功" : "计算失败");
}
public static Result divide(int a,int b){
try{
int c = a / b;
return Result.SUCCESS;
} catch (Exception e){
return Result.FAIL;
}
}
}
// 枚举:一枚一枚可以列出来的才建议使用枚举类型
// 枚举编译之后也是生成class文件
// 枚举也是引用数据类型
// 枚举中的每一个值可以看做是常量
enum Result{
// SUCCESS 是枚举Result类型的一个值
// FAIL 是枚举Result类型的一个值
// 枚举中的每一个值可以看做是“常量”
SUCCESS,FAIL
}