object 介绍及方法
object类是所有类的超类 object 是Java语言中的唯一一个没有父类的类,
常用的方法
可以覆盖的
1.equals方法,比较两个对象是否相同 Student a=new Student(); Student b=new Student();
a.equals(b) 比较a和b是否相同
注意:在没有覆盖equals方法前,功能和==相同
equals()与==的区别:
equals只能比较引用类型,可以被覆盖
== 可以比较基本类型和引用类型,不能被覆盖
关系:equals 默认使用==比较
equals 和hashcode
注意:如果两个对象的equals 相同,那么两个对象必须有相同的hashcode值
如果两个对象的equals 不相同 也可以有相同的hashcode值
所以,要重写equals方法必须重写hashcode
2.hashcode();获取对象的hashcode值
toString();返回对象的类名和内存地址
不能别被覆盖的
wait():等待
notify():唤醒
notifyAll():唤醒所有
getClass( ):返回当前对象的类型
如果equals()返回两个对象是相等的,那这两个对象上调用hashCode()返回的整数必须相等。
如果equals()返回两个对象是不相等的,他们返回的hashCode()返回的整数也可以是相 等的。
二、包装类:
就是对基本类型之间以及基本类型和String之间转换的工具;
int Integer
char Character
double Double
long Long
1) String 如何转换基本类型,在程序运行过程中,客户端获取数据都是String 类型
String str=“123”;
int a=Integer.parseInt(str); double b=Double.parseDouble(str);
2)基本类型转String
int a=123;
String str=String.valueOf(a);
c)基本类型和包装类的转换
i)标准
int a=123;
Integer b=new Integer;
int c=b.intValue();
int a=123;
Integer b=a;
int c=a;
package Diyitongjin.dmon; public class demo_01 { public static void main(String[] args) { String str="123"; int a=Integer.parseInt(str);//字符串转数字 str=String.valueOf(a);//数字转字符串
//如果包装类的值为null 那么在自动拆箱的是 什么情况 Integer m=null; int n=m;
//发生空指针异常;
}
}Exception in thread "main" java.lang.NullPointerException
at Diyitongjin.dmon.demo_01.main(demo_01.java:10)
3.String 表示字符串 对应的是 数据库中的 char 和 vachar2;
char 长度固定 长度不够 以空格补齐 vachar2 长度不固定
以上两种类型 默认以 字节 为单位;
String 不属于基本类型 属于引用类型,但是可以和基本类型一样使用
String a=“abc”;
String b=new String ("aaa");
b)以下代码构建了几个对象;
String b=new String ("aaa");// b 是对象的一个引用,就是一个变量
通过 new String () 构建的
“aaa” 常量值 也称为 常量对象;
e)字符串的不变性:字符串一旦赋值后 常量值不能改变
String aa=“abc”;
String bb=aa+"ef";//在内存中又产生了两个常量
ef abcef
public class demo_01 { public static void main(String[] args) { String a="abc"; String b="abc"; System.out.println(a==b);//ture String c=new String("abc"); String d=new String("abc"); System.out.println(c==d);//flase c.eequals(d)//ture }
}判断字符串中是否包含汉子
public static void main(String[] args) throws
UnsupportedEncodingException { String a="abc你"; byte b []=a.getBytes("gbk"); System.out.println(b.length); } }
f) 如何判断一个字符串的真实长度(所占的字节数)
先把字符串 转换为字节数组 在计算长度
String str="abc你";
byte [] b =str.getBytes("gb18030");
System.out,println(b.length);
g) 常用的方法
1)length ():获取字符串中字符的个数
2)String substring(int begin,int end):从字符串红截取一定区间的值;
public static void main(String[] args) { String str="370238199709337683"; String year =str.substring(6,14);//开头包含,结束不包含[6,10) System.out.println(year); } }
3)char charAt(int index);获取下标对应的字符
char f=str.charAt(16); System.out.println(f%2==0?"女孩":"男孩");
System.out.print(f+10);//65 4=51 0=48
4)int indexOf(String str):返回 str 咋字符串中第一次出现的下标,如果没有返回 -1;
用于判断 字符串是否包含某个字符串;
5) int lastIndexOf(String str):表示 从右边开始找第一次出现的下标
public class Demo02 { //从给定邮箱中获取邮箱的服务器 public static void main(String[] args) { String email="daimi@[email protected]"; //获取邮箱的服务器 和 账户名 int index=email.lastIndexOf("@"); String post=email.substring(index+1);//左边包含 System.out.println(post); String name=email.substring(0,index);//右边不包含 System.out.println(name); } }
6)String replace(String a,String b):把字符串中 a 统一替换为b;
public static void main(String[] args) { String star="*********************************"; String[] word={"爱","泡"}; String str="小明爱吃泡泡堂"; for (int i = 0; i <word.length ; i++) { str=str.replace(word[i],star.substring(0,word[i].length())); }//动态的替换 System.out.println(str); String t="adm193nd13i401nd";//只替换两个连续的数字 String n=t.replaceAll("[0-9]{2}","**");//0-9出现的数字; System.out.println(n); //把字符串中的中文替换[\u4e00-\u9fa5] } }
7)boolean equals(String other);返回true当且仅当参数不为null,并且是一个布尔对象,表示同样的布尔值同为此对象。
8) String [] split(String regex); 表示按照 “ ”进行分割;
public static void main(String[] args) { //爱好 String liake="弹琴,吃屎,唱歌"; String [] liakes=liake.split(","); for (int i = 0; i <liakes.length ; i++) { System.out.println(liakes[i]); // . 表示任意一个字符 } } }
=======================================
判断一个字符串中数字的出现的个数
String str="12h313hiu4h2uh2i13";
public class Edada { public static void main(String[] args) { String str="12h313hiu4h2uh2i13"; String n=str.replaceAll("[0-9]{1,}",""); System.out.println(str.length()); System.out.println(n.length()); int l=str.length()-n.length(); System.out.println(l); } }
4.可变字符串
StringBuffer StringBuilder
a)问题 :
i) String 与StringBuffer 或 StringBuilder的区别
Sting 是不可变字符串,一旦赋值 内容不能改变,每次改变都会产生一个新的字符串
StringBuffer或StringBuilder
可变字符串,内容可以改变,不能产生新的字符串对象
ii) StringBuffer 和 StringBuilder的区别
StringBuffer
b)练习:
如果数字长度过大,不方便阅读
int a=31312312;
如何完成数字千分位显示 31,312,312
import java.text.DecimalFormat; public class Kdadada { public static void main(String[] args) { //对数字千分为显示 int a=31312312; menthod(a); } public static void menthod(int n){ DecimalFormat f=new DecimalFormat("#,###"); String str =f.format(n); System.out.println(str); }
}
==================================================
public static void menthod2(int n){ StringBuilder t= new StringBuilder(); t.append(n); t.reverse();//翻转 int a=t.length()%3==0?t.length()/3-1:t.length()/3; for (int i = 0; i <a ; i++) { t.insert(4*i+3,","); } t.reverse(); System.out.println(t); } }
三、Math类(final类)数学类
a)特点:
final类修饰,不能被继承
所有的方法和属性都是 static 修饰的
构造方法是private 的 不能被访问
b)方法:
random() 0.0-1.0
long round(double i): 对小数保留整数
double ceil(double a) 返回大于等于 a 的最小整数,以double 格式显示
double floor(double a) 返回 小于等于 a 的最大整数 以double 格式显示
c)练习:
如何对一个小数保留两位小数
double a=123.434343;
double b=Math.round(a*100)/100.0;
四、日期类(Date)、
import java.util.Calendar; public class Canaemad { public static void main(String[] args) { //根据身份证号码确定 出生当天是星期几 String no="370283199606305615"; String[]weehk={"","日","一","二","三","四","五","六"}; String year=no.substring(6,10); String month=no.substring(10,12); String day =no.substring(12,14); int y=Integer.parseInt(year); int m=Integer.parseInt(month)-1; int d=Integer.parseInt(day); Calendar c=Calendar.getInstance(); c.set(y,m,d); int w=c.get(Calendar.DAY_OF_WEEK); System.out.println("星期"+weehk[w]); } }