4 常用类库

1Object类

Object()    默认构造方法

clone()    创建并返回此对象的一个副本。

equals(Object obj)    指示某个其他对象是否与此对象“相等”

getClass()   返回一个对象的运行时类

hashCode()     返回该对象的哈希码值

 toString()    返回该对象所在的类名和哈希码值。

equals(Object obj):判断两个对象是否指向同一块内存区域。和==的区别,基本数据类型之间==比较的是他们的数值,符合数据类型之间==比较的是他们在内存中的存放地址。String和Date类等对equals进行重写,比较的是对象的内容。

public class test{
	public static void main(String[] args){
		int a = 3;
		int b = 3;
		Integer c = 3;
		Integer d = 3;//自动装箱。c、d堆地址指向相同的常量池,堆地址也相同
        //指向相同常量池,没有new则堆地址相同。
		Integer e = 4;
		Integer f = new Integer(4);//新建堆空间,再指向常量池。f在内存中有两个对象,分别在方法区的常量池和堆中。
		String g = "hello";
		String h = new String("hello");
		String i = h;//引用拷贝,i、h在栈中指向同一个堆地址
		
		System.out.println(a == b);//true
		System.out.println(a == c);//true
		System.out.println(c == d);//true
		System.out.println(c == f);//false
		System.out.println(c.equals(b));//true
		System.out.println(c.equals(d));//true
		System.out.println(c.equals(f));//false
		//System.out.println(a.equals(b));//报错
		System.out.println(c.equals(e));//false
		
		System.out.println(g == h);//false
		System.out.println(g.equals(h));//true
		System.out.println(i == h);//true
		System.out.println(i.equals(h));//true
		h = "hi";
		//深拷贝:引用对象的值等信息,复制一份一样的。             
		//浅拷贝:只复制引用,另一处修改,你当下的对象也会修改。
		//浅拷贝:能复制变量,如果对象内还有对象,则只能复制对象的地址,意味着两个内部对象为一个对象。
		//深拷贝:能复制变量,也能复制当前对象的内部对象,对内部对象修改只会影响一个
		System.out.println(i);
		System.out.println(h);
		System.out.println(i == h);//false
		System.out.println(i.equals(h));//false。这里是深拷贝。其实拷贝分为引用拷贝和对象拷贝,对象拷贝才分深拷贝和浅拷贝。
		
	}
}
import java.io.*;
import java.util.regex.*;
public class CheckQQDemo
{
	public static void main(String[] args) throws Exception
	{
		String h = new String("hello");
		String i = h;//引用拷贝,i、h在栈中指向同一个堆地址
		System.out.println(i.equals(h));
		System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(h)));
		System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(i)));
		//true
		//java.lang.String@15db9742              堆地址
		//java.lang.String@15db9742
		h = "hi";//相当于= new String("hi");
		System.out.println(i.equals(h));
		System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(h)));
		System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(i)));
		String g = new String("hello");
		System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(g)));
		String z = "hi";
		System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(z)));
		//false
		//java.lang.String@6d06d69c.  h(和i值不同,地址也不同)
		//java.lang.String@15db9742.  i
		//java.lang.String@7852e922.  g(和h的值相同,地址不同)
		//java.lang.String@6d06d69c.  z(和h值相同,地址也相同)
	}
}

2System类

public final class System extends Object

System类所有属性都是静态的,类名.进行访问。System类是公共最终类,不能被继承,也不能被实例化,因为构造函数是私有的。

    /** Don't let anyone instantiate this class */
    private System() {
    }

System.gc():垃圾收集机制,内部调用 Runtime.getRuntime().gc()

currentTimeMillis():返回以毫秒为单位的当前时间

 exit(int status):系统退出 ,如果status为0就表示退出

arraycopy(Object src, int srcPos, Object dest, int desPos, int length) :数组拷贝操作

getProperties() :取得当前系统的全部属性

getProperty(String key) :根据键值取得属性的具体内容

3String、StringBuffer类

public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence
public final class StringBuffer
extends Object
implements Serializable, CharSequence

字符串是常量对象(和常数一样,放在常量池中,可以被共享),值在创建后不能修改,变量指向可以变;字符串缓冲区支持可变的字符串,即值可以变。Java不允许程序员重载任何操作符,但用于String类的“+”和“+=”,两个符号被重载过(写元源码的人改的)。

public class test{
	public static void main(String[] args){
		String a = "字符串";
		a = "更改";
		System.out.println(a);//a
		//a是引用数据类型可以更改(其指向),“字符串”被创建后不能更改
	}
}

线程不同步StringBuilder

4包装类

基本数据类型   包装类

byte        Byte

short        Short

int        Integer

long        Long

boolean    Boolean

float        Float

double        Double

char        Character

基本数据类型(数值类型)转字符串:

        “5+5=”5+5     ---5+5=55

        包装类对象.toString(值)

        String.valueof(值) 静态方法

字符串转基本数据类型(数值类型):

        包装类名.parse***(string)   静态方法

         [***表示基础数据类型名,如parseInt,没有Character.parseChar]

基本数据类型转包装类:

        包装类构造函数

        自动装箱

包装类转基本数据类型:

        包装类对象.***Value()

        [***表示基础数据类型名,如intValue,没有Character.parseChar]

        自动拆箱

public class test{
	public static void main(String[] args){
		int m = 5;
		Integer o = m;//自动装箱。相当于new Integer(m);
		int n = o;//自动拆箱。相当于o.intValue();
		System.out.println(n);//5
		System.out.println(o.equals(m));//true
		
	}
}

字符串转包装类:

       包装类构造函数参数可以接收String

public class test{
	public static void main(String[] args){
		String s = "5";
		Integer i = new Integer(s);//5
        //相当于Integer i = new Integer(Integer.parseInt(s));
		System.out.println(i);
		int m = 5;
		System.out.println("5+5="+m+m);//5+5=55
	}
}

包装类转字符串:

        包装类对象.toString(空);

Integer o = new Integer(5);
System.out.println(o.toString());//5

5Math、Random、BigInteger、BigDecimal、Date、SimpleDateFormat、Calendar类

Math.ramdom()   floor()  ceil()   round()

Random对象.next***(值/空) 

BigDecimal("0.1")   BigDecimal(0.1) 

Date.getTime()//Date构造函数内部调用System.currentTimeMillis()

SimpleDateFormat对象.format(Date对象)

Calendar.getInstance()

Calendar.getTime()

6正则表达式

.,\d,\s,\w,\b,大写,[ ],^,X?,X+,X*,X{ },?i,?s,

用IO流和String类方法checkQQ:

import java.io.*;
public class CheckQQDemo{
	public static void main(String[] args)throws Exception{
		//throws Exception
		//1.用缓存字符流
		//BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		//String str = bf.readLine();//只有缓存流有readLine,相当于对char[]的封装
		//boolean flag = checkQQ(str);
		
		//2.用缓存字节流或字节流
		//BufferedInputStream inputStream = (BufferedInputStream)System.in;
		InputStream inputStream = System.in;
		boolean flag = checkQQ1(inputStream);
		
		System.out.println("此QQ号码为"+flag);	
	}
	public static boolean checkQQ(String str) throws QQException{
			/* 规则1:号码长度在4到11之间
		   规则2:应该是纯数字
		   规则3:首字母不为0
		   */
		   boolean flag = true;
		   if (!(str.length()>=4&&str.length()<=11)){
			   flag = false;
			   throw new QQException("长度非法!");//或者直接打印错误
		   }
		   for(int i=0;i<str.length();i++){
			   if(!(str.charAt(i)>='0'&&str.charAt(i)<='9')){
				flag = false;				
				throw new QQException("字符非法!");  
			   }			
		   }
		   if(str.startsWith("0")){
			   flag = false;
			   throw new QQException("首字符为0!"); 
		   }
		   return flag;
	}
	public static boolean checkQQ1(InputStream inputStream) throws IOException,QQException{
		boolean flag = true;
		byte[] buff = new byte[20];
		int len;
		while (((len = inputStream.read(buff)) != -1)) {
			String str = new String(buff, 0, len - 2);//2代表windows的回车符\r\n
			System.out.println("您输入的数据是:" + str + (len-2));
			if (!(str.length()>=4&&str.length()<=11)){
			   flag = false;
			   throw new QQException("长度非法!");
		   }
		   for(int i=0;i<str.length();i++){
			   if(!(str.charAt(i)>='0'&&str.charAt(i)<='9')){
				flag = false;				
				throw new QQException("字符非法!");  
			   }			
		   }
		   if(str.startsWith("0")){
			   flag = false;
			   throw new QQException("首字符为0!"); 
		   }
		}
		return flag;
	}
}
class QQException extends Exception{
	QQException(String str){
		super(str);	
	}
}

用包装类检查是否为数字

try 
{
	long l = Long.parseLong(qq);
	System.out.println("qq:"+l);
}
catch (NumberFormatException e)
{
	System.out.println("出现非法字符.......");
}

用正则表达式检查 QQ:

import java.io.*;
public class CheckQQDemo
{
	public static void main(String[] args) throws Exception
	{
		BufferedReader  fr = new BufferedReader(new InputStreamReader(System.in));
		String qq = fr.readLine();
		String regex = "[1-9]\\d{4,14}";//5-15位QQ
		boolean flag = qq.matches(regex);
		if(flag)
			System.out.println(qq+"...is ok");
		else
			System.out.println(qq+"... 不合法");
	}
}

正则表达式检查邮箱:

import java.io.*;
public class CheckQQDemo
{
	public static void main(String[] args) throws Exception
	{
		BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
		String s = "\\w+@\\w+(\\.\\w+)+";
		//jht@hpu.edu.cn
		//1958262680@qq.com
		String t = b.readLine();
		System.out.println(t.matches(s));
	}
}

java正则表达其他语言一个斜杠需要两个,一个斜杠。其他语言\\,java\\\\

import java.io.*;
import java.util.regex.*;
public class CheckQQDemo
{
	public static void main(String[] args) throws Exception
	{
		//1用String的matches方法匹配,返回boolean。
		//BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
		//String s = "\\+\\w+@\\w+(\\.\\w+)+\\\\";
		//jht@hpu.edu.cn
		//1958262680@qq.com
		//String t = b.readLine();
		//System.out.println(t.matches(s));
		//--------------------------------
		//2用Pattern的matcher方法匹配,返回一个Matcher对象可以进行具体操作。find,group
		BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
		String t = b.readLine();
		String s = "\\+\\w+@\\w+(\\.\\w+)+\\\\";
		//将规则封装成对象。
		Pattern p = Pattern.compile(s);
		//让正则对象和要作用的字符串相关联。获取匹配器对象。
		Matcher m  = p.matcher(t);
		while(m.find())
		{
			System.out.println(m.pattern());//返回正则表达式
			/*  \+\w+@\w+(\.\w+)+\\   \+代表普通+,\\代表普通\*/
			System.out.println(m.group());//返回匹配的子序列
			System.out.println(m.start()+"...."+m.end());
		}
	}
}

?的非贪心匹配:每次尽可能匹配到少的字符,但是要遵循从左到右

import java.io.*;
import java.util.regex.*;
public class test
{
	public static void main(String[] args) throws Exception
	{
		
		String t = "oooo";
		String s = "o+?";
		//将规则封装成对象。
		Pattern p = Pattern.compile(s);
		//让正则对象和要作用的字符串相关联。获取匹配器对象。
		Matcher m  = p.matcher(t);
        System.out.println(m.pattern());//返回正则表达式
		while(m.find())
		{	
			System.out.println(m.group());//返回匹配的子序列
			System.out.println(m.start()+"...."+m.end());
		}
	}
}

运行结果:

 

o+?
o
0....1
o
1....2
o
2....3
o
3....4

再看一题:

 

import java.io.*;
import java.util.regex.*;
public class test
{
	public static void main(String[] args) throws Exception
	{
		String t = "aabaaabaaaab";
		String s = "aaa??b";
		//将规则封装成对象。
		Pattern p = Pattern.compile(s);
		//让正则对象和要作用的字符串相关联。获取匹配器对象。
		Matcher m  = p.matcher(t);
		System.out.println(m.pattern());//返回正则表达式
		while(m.find())
		{
			System.out.println(m.group());//返回匹配的子序列
			System.out.println(m.start()+"...."+m.end());
		}
	}
}

 运行结果:

aaa??b
aab
0....3
aaab
3....7
aaab
8....12

有用到IO流一定要抛出IOException/Exception异常,用throws可以不用try catch处理异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值