1)Object
①clone()
使用clone()方法的步骤:
1.实现clone的类首先需要实现Cloneable接口。Cloneable接口实质上是一个标识接口
2.在类中重写Object类中的clone方法
3.在clone方法中调用super.clone()。无论clone类的继承结构是什么,super.clone会直接或间接调用java.lang.Object类的clone()方法。
4.把浅复制的引用指向原型对象新的克隆体。
public class UseObjectFunction {
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
Student s1=new Student();
s1.setAge(12);
s1.setName("郜哦");
System.out.println(s1.hashCode());
Student s3=(Student) s1.clone();
Student s2=new Student();
s2.setAge(12);
s2.setName("郜哦");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s2.getClass().getName());
}
}
class Student{
int age;
String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
②equals
指示其他某个对象是否与此对象“相等”。 未重写前比较的是对象的地址;但是其常常需重写,重写后比较的是对象是否相等。
重写前:
public boolean equals(Object obj) {
return (this == obj);
}
重写后
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
equal和==俩者均为表示相等的意思,但是它们相等的含义却有所区别:
“==”运用在基本数据类型的时候,通过比较它们实际的值来判断是否相同;而用于比较引用数据类型的时候,则是比较俩个引用的地址是否相等,也就是是否指向一个对象。
equals方法是java.lang.Object的方法,也就是所有的java类都会有的方法。它可以被程序员覆盖重写,通过自定义的方式来判断俩个对象是否相等。对于字符串java.lang.String类来说,它的equals方法用来比较字符串的字符序列是否完全相等。
③getClass()
返回此 Object 的运行时类。通过getClass()可以获取实例化对象的属性名方法等。将返回a对象的信息描述,当然这些信息都是动态的,比如a对象的某个属性有什么值阿等等,你可以在程序运行时通过写代码动态的获得这些信息。
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
Student s1=new Student();
s1.setAge(12);
s1.setName("郜哦");
System.out.println(s1.hashCode());
Student s3=(Student) s1.clone();
Student s2=new Student();
s2.setAge(12);
s2.setName("郜哦");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s2.getClass().getName());
}
}
class Student{
int age;
String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
④hashCode()
这一般是通过将该对象的内部地址转换成一个整数来实现的
csdn载录:
1、hashCode的存在主要是用于查找的快捷性,如Hashtable,HashMap等,hashCode是用来在散列存储结构中确定对象的存储地址的;
2、如果两个对象相同,就是适用于equals(java.lang.Object) 方法,那么这两个对象的hashCode一定要相同;
3、如果对象的equals方法被重写,那么对象的hashCode也尽量重写,并且产生hashCode使用的对象,一定要和equals方法中使用的一致
4、两个对象的hashCode相同,并不一定表示两个对象就相同,也就是不一定适用于equals(java.lang.Object) 方法,只能够说明这两个对象在散列存储结构中,如Hashtable,他们“存放在同一个篮子里”。
重写前代码:
public native int hashCode();
重写后代码
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
⑤toString()
返回该对象的字符串表示,该对象常需要重写。
重写前
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
重写后
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
2)Scanner
①hasNext()
如果此扫描器的输入中有另一个标记,则返回 true。在等待要扫描的输入时,此方法可能阻塞。扫描器将不执行任何输入;
注:当且仅当此扫描器有另一个标记时才返回 true
②hasNextLine()
如果在此扫描器的输入中存在另一行,则返回 true。在等待输入信息时,此方法可能阻塞。扫描器不执行任何输入。
注:当且仅当此扫描器有另一行输入时才返回 true
③next()
查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息。即使以前调用 hasNext() 返回了 true,在等待要扫描的输入时此方法也可能阻塞。
返回:下一个标记
④nextLine()
此扫描器执行当前行,并返回跳过的输入信息。 此方法返回当前行的其余部分,不包括结尾处的行分隔符。当前位置移至下一行的行首.
返回:跳过的行
转录:
next与nextLine()区别:
next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab、或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后的输入的空格键、Tab或Enter键等视为分隔符或结束符。简单地说,next()查找并返回来自此扫描器的下一下完整标记。完整标记的前后是与分隔模式的输入信息,所以next方法不能得到带空格的字符串。
而nextLine()方法的结束符只是Enter键,即nextLine()方法返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的。
public class NextTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.print("请输入第一个字符串:");
s1=sc.nextLine();
System.out.print("请输入第二个字符串:");
s2=sc.next();
System.out.println("输入的字符串是:"+s1+" "+s2);
}
}
运行结果:
请输入第一个字符串:home
请输入第二个字符串:work
输入的字符串是:home work
但如果把程序改一下, s1=sc.next(); s2=sc.nextLine();
运行结果是:
请输入第一个字符串:home
请输入第二个字符串:输入的字符串是:home
完整例子:
public static void main(String[] args) {
// TODO Auto-generated method stub
testScanner();
}
public static void testScanner(){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextLine()){
String string=scanner.nextLine();
System.out.println(string);
}
}
}
3)String
①equals()
将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
其重写Object后的方法:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
②equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
③getBytes()
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
④hashCode()
返回此字符串的哈希码
其底层代码为:
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
⑤indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。indexOf(int ch, int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
lastIndexOf():返回指定字符在此字符串中最后一次出现处的索引
其底层代码为:
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return indexOfSupplementary(ch, fromIndex);
}
}
⑥isEmpty():如果 length() 为 0,则返回 true;否则返回 false
⑦matches(String regex):告知此字符串是否匹配给定的正则表达式。
public class TestFact {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Pattern p = Pattern.compile("[1-9]\\d{6,16}@(163|qq)\\.com");
// Matcher m = p.matcher("2408223721@qq.com");
// boolean b = m.matches();
// System.out.println(b);
// sss ss=new sss();
Pattern p2=Pattern.compile("(www|edu)\\.(\\d+|\\w+)\\.(com|cn)");
Matcher m1=p2.matcher("www.baidu.com");
boolean b2=m1.matches();
System.out.println(b2);
// System.out.println(ss.toString());
// System.out.println(ss.hashCode());
// String s=Integer.toHexString(ss.hashCode());
// System.out.println(s);
}
}
⑧contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true。
底层调用了indexOf()方法。
⑨replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
⑩toLowerCase():使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
toUpperCase():使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
⑪valueOf(Object obj):返回 Object 参数的字符串表示形式。
public class TestString2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
test1();
test2();
test3();
System.out.println( test4("asasasasadrasasfdsd","as"));
}
public static void test1(){
String string="afytsfsfasf";
System.out.println(string.length());
}
public static void test2() {
String string="afytsfsfasf";
System.out.println(string.charAt(3));
System.out.println(string.indexOf('t'));
System.out.println(string.indexOf('s'));
System.out.println(string.lastIndexOf('s'));
System.err.println(string.substring(3, 8));
System.out.println(string.substring(4));
System.out.println(string.contains("tsf"));
}
public static void test3(){
String string="afytsfsfasf";
System.out.println(string.getBytes());
System.out.println(string.toCharArray());
System.out.println(string.toUpperCase());
System.out.println(string.toLowerCase());
System.out.println(string.concat(string));
}
public static int test4(String string,String string2){
int j=string.length();
int count=0;
int k=string2.length();
for(int i=0;i<j-k;i++){
if(string2.equals(string.substring(i,i+k))){
count++;
}
}
return count;
}
}
注:String,Stringbuilder,StringBuffer区别:
①StringBuffer线程安全,Stringbuilder线程不安全
②String为不可变序列,其他为可变序列
③StringBuilder>StringBuffer>String
4)日期
注:数据库一般存的是long整数。
①Date
1.boolean after(Date when)
测试此日期是否在指定日期之后。
2.boolean before(Date when)
测试此日期是否在指定日期之前。
3. int compareTo(Date anotherDate)
比较两个日期的顺序。
import java.util.Date;
public class TestDate {
public static void main(String[] args){
Date date=new Date(14351);
Date date1=new Date(124141);
long dateTime=date.getTime();
System.out.println(date.after(date1));
System.out.println(date.before(date1));
System.out.println(date.compareTo(date1));
}
}
4.String toString()
把此 Date 对象转换为以下形式的 String:
dow mon dd hh:mm:ss zzz yyyy
与打印date默认的一致
public class TestDate {
public static void main(String[] args){
Date date=new Date(14351);
System.out.println(date);
System.out.println(date.toString());
}
}
5.long getTime()
返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
public class TestDate {
public static void main(String[] args){
Date date=new Date();
System.out.println(date.getTime());
}
}
6.java.sql.Date中的public static Date valueOf(String s)
将 JDBC 日期转义形式的字符串转换成 Date 值。
s - 表示 “yyyy-mm-dd” 形式的日期的 String 对象
表示给定日期的 java.sql.Date 对象 。
②Calendar 日历类
注:为抽象类,不可被实例化
1.Static Calendar getInstance(Locale aLocale)
使用默认时区和指定语言环境获得一个日历。返回的 Calendar 基于当前时间,使用了默认时区和给定的语言环境。
2. Date getTime()
返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。
3. setTime(Date date)
使用给定的 Date 设置此 Calendar 的时间。
public class TestDate {
public static void main(String[] args){
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
System.out.println(calendar.getTime());
}
}
③SimpleDateFormat
1.SimpleDateFormat(String pattern)
用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
注:此构造方法可能不支持所有语言环境。要覆盖所有语言环境,请使用 DateFormat 类中的工厂方法
2.Date parse(String text, ParsePosition pos):
解析字符串的文本,生成 Date。
public class TestDate {
public static void main(String[] args) throws ParseException {
String date2="1953-12-12";
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
date=df.parse(date2);
System.out.println(date);
}
}
- StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos):
将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。
public class TestDate {
public static void main(String[] args) throws ParseException {
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
df.format(date);
System.out.println(date);
}
}
5)基本数据类型包装类与自动拆箱,装箱
自动装箱:把基本类型转换为包装类型
自动拆箱:把包装类型转换为基本类型
基本数据类型对应的包装类:
byte————》Byte
short————》Short
int—————》Integer
long————》Long
float————》Float
double————》Double
char————》Char
boolean————》Boolean
public class TestCase {
public static void main(String[] args){
Integer in=5;
System.out.println(in);
int num=in;
System.out.println(num);
}
}
6)BigInterger与BigDecimal
①BIgInter可以使超过Integer范围内的数据进行运算
1.BigInterger(String val)
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
2. BigInteger add(BigInteger val)
返回其值为 (this + val) 的 BigInteger。
3.BigInteger subtract(BigInteger val)
返回其值为 (this - val) 的 BigInteger。
4. multiply(BigInteger val)
返回其值为 (this * val) 的 BigInteger
5.divide(BigInteger val)
返回其值为 (this / val) 的 BigInteger。
public class TestCase {
public static void main(String[] args){
BigInteger bigInteger=new BigInteger("1314253512351313");
BigInteger bigInteger2=new BigInteger("15123214312435145");
System.out.println(bigInteger.add(bigInteger2));
System.out.println(bigInteger.subtract(bigInteger2));
System.out.println(bigInteger.multiply(bigInteger2));
System.out.println(bigInteger2.divide(bigInteger));
}
}
②BigDecimal类可以精确的表示计算浮点数
1.BigDecimal(BigInteger val)
将 BigInteger 转换为 BigDecimal。
2.add(BigDecimal augend)
返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。
3.subtract(BigDecimal subtrahend)
返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
4.multiply(BigDecimal multiplicand)
返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() +multiplicand.scale())。
public class TestCase {
public static void main(String[] args){
BigDecimal bigDecimal=new BigDecimal("3.121");
BigDecimal bigDecimal1=new BigDecimal("2.122");
System.out.println( bigDecimal.add(bigDecimal1));
System.out.println(bigDecimal.subtract(bigDecimal1));
System.out.println(bigDecimal.multiply(bigDecimal1));
System.out.println(bigDecimal.divide(bigDecimal));
}
}