笔记10
课程内容:
1、Object
2、Scanner
3、String
4、Math
5、System
一、API介绍
1、API:Application Programming Interface,应用程序接口
2、接口:用于规定方法名称的规则。定义的就是方法的修饰符,返回值类型,方法的名称,方法的参数列表
3、API:包含了各种有实现内容的方法,类型,接口,枚举
4、使用的时候,不关注方法,类型,接口到底是怎么实现的,更关注的是怎么使用这个方法,这个类,这个接口
类库
1、java源代码编译之后,形成.class字节码文件,就包含了这个类中的所有内容,在脱离源代码的前提下,我们可以继续使用这个类。
2、java中可以将.class字节码文件,打包生成jar包。
(1)jar包就是.class文件的压缩包,是java的专用的压缩包,一旦获取了jar包,就相当于获取了里面所有的.class字节码文件
(2)jar的生成,在eclipse中选中要打包的类型(包,工程),右键选择 export ---> java--.>jar file-->选择生成的jar包路径
(3)jar的使用,将下载好的jar包,拷贝到工程中,选择jar包,右键build path --- add to build path
(4)jar的删除:先把jar文件删除掉,将依赖路径也要删除
右键工程 :builde path --》 configure build path -->libraries --》remove丢失的jar文件
3、类型库
(1)jar类型太少,一系列的jar包,构成了一个类型库 ,library
(2)一旦导入类型库,就相当于导入了类库中所有的jar包,相当于获取到了所有的.class文件
eg:如果JUnit类库之后,就可以进行单元测试
(3)添加类库的方式
选中工程----》右键——————》build path --- add libraries选择要添加的类型库即可
4、关联源码
attach source---》external location---》根据源码的类型判断选择,如果是压缩包格式就选择external file--->选择源码文件即可
二、Object类型
1、Object类型是所有类型的顶层父类,所有的类型直接或者间接的父类,所有的类型中都含有Object类型中所有的方法。
2、随意定义一个类型,不手动的显示定义其父类,那么这个类的父类就是Object类型
3、Object类型的构造方法
(1)可以自己创建对象
(2)让子类访问,所有子类都会直接或者间接的访问到这个顶层父类的构造方法
(3)Object类在执行构造方法时,不会去访问自己的父类,因为没有父类了。
package com. ujiuye. demos ;
public class Demo_2 {
public static void main ( String [ ] args) {
Object obj = new Object ( ) ;
System . out. println ( obj) ;
ObjectSon oSon = new ObjectSon ( ) ;
oSon. equals ( oSon) ;
oSon. getClass ( ) ;
}
}
class ObjectSon extends Demo_2 {
}
toString方法
1、返回当前对象的字符串表示
2、Object类型中,这个方法的实现:全类名+“@”+哈希码值的十六进制表示
getClass().getName() + "@" + Integer.toHexString(hashCode());
简称:对象的地址值
3、对象返回一个地址值的字符串,没有什么意义的,因此对于子类而言,需要重写父类的toString方法
4、重写的原则:返回的是该对象中所有成员变量的值
5、最终操作
alt + shift +s --》 s
6、打印对象的时候,结果和调用toString方法的结果是一模一样
package com. ujiuye. demos ;
public class Demo_3 {
public static void main ( String [ ] args) {
Object obj = new Object ( ) ;
String string = obj. toString ( ) ;
System . out. println ( string) ;
System . out. println ( obj) ;
System . out. println ( obj. getClass ( ) . getName ( ) + "@" + Integer . toHexString ( obj. hashCode ( ) ) ) ;
Person_1 per = new Person_1 ( ) ;
String str = per. toString ( ) ;
System . out. println ( str) ;
System . out. println ( per) ;
}
}
package com. ujiuye. demos ;
public class Person_1 {
private String name;
private int age;
public Person_1 ( ) {
super ( ) ;
}
public Person_1 ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Person_1 [name=" + name + ", age=" + age + "]" ;
}
}
equals方法
1、用于比较两个对象是否相等的方法,比较的就是调用者和参数这两个对象
boolean equals(Object obj)
2、在Object类型中,比较的是两个引用是否指向了同一个对象,如果是,才返回true,相当于是在比较两个对象的地址值是否相同。
3、Object类型中的equals方法比较的是地址值,没有什么意义,因此在自定义类中,重写equals方法,最终实现比较的是成员变量值是否相同。
4、重写原则:
一般比较的是对象的所有属性,是否全部相同
5、操作:
alt + shift + s
不需要自己手动写的,直接使用eclipse中的快捷键即可
package com. ujiuye. demos ;
public class Person_1 {
private String name;
private int age;
public Person_1 ( ) {
super ( ) ;
}
public Person_1 ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj)
return true ;
if ( obj == null )
return false ;
if ( getClass ( ) != obj. getClass ( ) )
return false ;
Person_1 other = ( Person_1 ) 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 ;
}
@Override
public String toString ( ) {
return "Person_1 [name=" + name + ", age=" + age + "]" ;
}
}
package com. ujiuye. demos ;
public class Demo_4 {
public static void main ( String [ ] args) {
Object obj1 = new Object ( ) ;
Object obj2 = new Object ( ) ;
System . out. println ( obj1) ;
System . out. println ( obj2) ;
boolean equals = obj1. equals ( obj2) ;
System . out. println ( obj1 == obj2) ;
System . out. println ( equals) ;
Person_1 person_1 = new Person_1 ( "张三" , 23 ) ;
Person_1 person_2 = new Person_1 ( "张三" , 23 ) ;
System . out. println ( person_1. equals ( person_2) ) ;
}
}
==和equals的区别
1、==和equals都是用于比较数据是否相等的方式
2、不同点:
(1)比较的内容不同
== 可以比较任意类型的数据,既可以比较基本数据类型,也可以比较引用数据类型
equals方法,只能比较引用数据类型
(2)比较的规则不同
== 在比较基本数据类型的时候,比较的就是数据的值;比较引用数据类型的时候,比较的是地址值是否相同
equals方法在重写之前,比较是两个对象的地址值,在重写之后,比较的是两个对象的属性值
package com. ujiuye. demos ;
public class Demo_5 {
public static void main ( String [ ] args) {
System . out. println ( 1 == 2 ) ;
Object obj1 = new Object ( ) ;
Object obj2 = new Object ( ) ;
Object obj3 = obj1;
System . out. println ( obj1 == obj3) ;
System . out. println ( obj1. equals ( obj2) ) ;
Person_1 person_1 = new Person_1 ( "李四" , 24 ) ;
Person_1 person_2 = new Person_1 ( "李四" , 27 ) ;
System . out. println ( person_1. equals ( person_2) ) ;
}
}
三、Scanner
1、概述:
Scanner就是一个扫描器,可以扫描指定设备的基本数据和字符串
2、构造方法:
Scanner(InputStream source)
扫描指定的输入流
System.in标准的输入流,默认关联的是键盘
录入基本数据类型的方法
1、录入基本数据类型的方法
nextByte():获取一个byte类型的数据
nextShort();
nexInt();
nextLong();
nextFloat();
nextDouble();
nextBoolean();
2、注意:
没有nextChar这个方法,一般都是录入字符串
键盘录入的数据,不是java代码,所以录入long类型或者float类型的数据常量不需要加f的
一次向录入多个数据,可以使用空格进行分隔
package com. ujiuye. demos ;
import java. io. InputStream ;
import java. util. Scanner ;
public class Demo_6 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "键盘录入byte类型的" ) ;
byte nextByte = sc. nextByte ( ) ;
System . out. println ( nextByte) ;
System . out. println ( "键盘录入一个指定进制的数字,转成是十进制的byte类型的数字" ) ;
byte nextByte2 = sc. nextByte ( 16 ) ;
System . out. println ( nextByte2) ;
System . out. println ( "键盘录入Short类型的" ) ;
short nextShort = sc. nextShort ( ) ;
System . out. println ( nextShort) ;
System . out. println ( "键盘录入long类型的" ) ;
long nextLong = sc. nextLong ( ) ;
System . out. println ( nextLong) ;
System . out. println ( "键盘录入float类型的" ) ;
float nextFloat = sc. nextFloat ( ) ;
System . out. println ( nextFloat) ;
System . out. println ( "请输入布尔类型的数据" ) ;
boolean nextBoolean = sc. nextBoolean ( ) ;
System . out. println ( nextBoolean) ;
}
}
键盘录入字符串
1、next()方法:可以录入下一个完整的标记,返回一个字符串,通过空格来分隔各个标记。
2、nextLine()方法:可以录入下一个完整的标记,返回一个字符串,通过换行符来分隔各个标记。
package com. ujiuye. demos ;
import java. util. Scanner ;
public class Demo_7 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入字符串" ) ;
String str = sc. nextLine ( ) ;
String str1 = sc. nextLine ( ) ;
String str2 = sc. nextLine ( ) ;
System . out. println ( str) ;
System . out. println ( str1) ;
System . out. println ( str2) ;
}
}
Scanner类型的小问题以及避免
1、问题:当调用完以空格作为分隔标记的方法之后,再调用一个一换行符为分隔符的方法时,这个方法不会再等待我们键盘录入了,而是直接结束程序,如果打印返回的内容就是一个空的字符串
2、解决办法
(1)连续调用两次的nextLine方法,第一会讲换行标记符读出来,第二次就会等待用户录入
(2)创建一个新的Scanner对象,新的Scanner对象中没有上次录入的数据,是干净的,所以不会影响下次的录入
(3)所有的录入都以字符串的形式录入到内存中,然后再进行数据类型的转换
基本的数据类型的字符串都能通过方法解析为对应类型的数字
package com. ujiuye. demos ;
import java. util. Scanner ;
public class Demo_8 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个数字" ) ;
int nextInt = sc. nextInt ( ) ;
System . out. println ( nextInt) ;
Scanner ssc = new Scanner ( System . in) ;
System . out. println ( "请输入你要录入的数字" ) ;
String nextLine = ssc. nextLine ( ) ;
int parseInt = Integer . parseInt ( nextLine) ;
System . out. println ( parseInt) ;
}
}
四、String
1、String就是一个字符串类型,属于java.lang包不需要导包
2、所有的字符串常量(“i love java”,"helloworld") 都属于String类型的对象
3、字符串字面值属于常量,存储在方法区中的常量池的。
4、字符串常量,在创建之后就无法修改(是一个不可变的字符序列)
5、不可变得原因:String类型中维护的是一个final修饰的数组,是不可变的,而且在String类型中本身没有提供改变数组成员变量的方法。
package com. ujiuye. demos ;
public class Demo_9 {
public static void main ( String [ ] args) {
String str1 = "a" ;
String str2 = "a" ;
System . out. println ( str1 == str2) ;
str1 = "hello" ;
System . out. println ( str1) ;
}
}
String类中构造方法
1、String();创建一个空字符串
2、String(String original):创建参数字符串的一个副本(参数字符串是在常量池中,构造方法创建出来的字符串是在堆内存中)
3、String(byte[] arr):将一个字节数组转为字符串
查询的是平台的默认编码
4、String(byte[] bytes, int offset, int length)
将字节数组的一部分转成字符串p
5、String(char[] value) 将字符数组转成字符串
6、String(char[] value, int offset, int count) 将字符数组的一部分转成字符串
package com. ujiuye. demos ;
public class Demo_10 {
public static void main ( String [ ] args) {
byte [ ] bytes = { 97 , 98 , 99 , - 99 , - 80 } ;
String str = new String ( bytes) ;
System . out. println ( str) ;
String str1 = new String ( bytes, 0 , 2 ) ;
System . out. println ( str1) ;
char [ ] chs = { 'a' , 'b' , 'b' , 'l' , 'o' , 'v' , 'e' } ;
String strChs = new String ( chs) ;
System . out. println ( strChs) ;
String strChs1 = new String ( chs, 3 , 4 ) ;
System . out. println ( strChs1) ;
}
private static void string_1 ( ) {
String str = new String ( ) ;
System . out. println ( str) ;
String str1 = "java" ;
String str3 = "java" ;
String str2 = new String ( "java" ) ;
System . out. println ( str1 == str3) ;
System . out. println ( str1 == str2) ;
String str11 = "ja" + "va" ;
String str22 = "java" ;
System . out. println ( str11 == str22) ;
String str33 = "ja" ;
String str44 = "va" ;
String str55 = str33 + str44;
System . out. println ( str55 == str22) ;
}
}
String类中的判断方法
1、equals(Object obj):判断调用者和参数对象描述的字符串内容是否相同
2、equalsIgnoreCase(String anotherString) 忽略大小写判断两个字符串内容是否相同
3、contains(String str):判断调用者是否包含了str这个子串
4、startsWith(String prefix) :判断调用者是否以prefix字符串开头
5、endsWith(String suffix) 断调用者是否以suffix字符串结尾
6、isEmpty():判断调用者是否为空串
package com. ujiuye. demos ;
public class Demo_11 {
public static void main ( String [ ] args) {
String str1 = "java" ;
String str2 = "java" ;
System . out. println ( str1 == str2) ;
String str3 = new String ( "java" ) ;
System . out. println ( str1 == str3) ;
System . out. println ( str1. equals ( str2) ) ;
System . out. println ( str1. equals ( str3) ) ;
String str11 = "java" ;
String str22 = "jAva" ;
System . out. println ( str11. equals ( str22) ) ;
System . out. println ( str11. equalsIgnoreCase ( str22) ) ;
String strCon = "I love java" ;
System . out. println ( strCon. contains ( "java" ) ) ;
System . out. println ( strCon. contains ( "j ava" ) ) ;
String strSw = "我来成都吃串串" ;
System . out. println ( "startsWith方法: " + strSw. startsWith ( "我来成都" ) ) ;
String strEw = "我来重庆吃火锅" ;
System . out. println ( "endsWith方法: " + strEw. endsWith ( "火锅" ) ) ;
String strIe = "" ;
System . out. println ( "isEmpty 方法:" + strIe. isEmpty ( ) ) ;
}
}
String类型中的获取方法
1、length();获取是该字符串中的字符的个数
2、charAt() 返回调用者字符串中指定索引为index位置上的字符
3、substring(int beginIndex) 获取一个字符串,内容是从当前字符串的索引beginIndex开始到结尾
4、substring(int beginIndex, int endIndex) 获取一个指定索引范围的子串
1、包含头不包含尾
2、所有的方法都无法修改原有的字符串,都是返回的是一个新的字符串对象
5、indexOf家族:
indexOf(int ch):返回的是ch字符在当前调用者字符串中,第一次出现的索引
indexOf(int ch, int fromIndex) 从指定的fromIndex索引出开始寻找,找到ch字符在调用者字符串中,第一次出现的索引
indexOf(String str) 返回的是str字符串在当前调用者字符串中,第一次出现的索引
indexOf(String str, int fromIndex) 从指定的fromIndex索引出开始寻找,找到str字符串在调用者字符串中,第一次出现的索引
6、lastIndexOf家族
和indexOf一样,只不过在查找的时候,从后向前找,所有的字符和索引不会发生变化
package com. ujiuye. demos ;
public class Demo_12 {
public static void main ( String [ ] args) {
String strLen = "1234567" ;
System . out. println ( "length方法: " + strLen. length ( ) ) ;
String strCa = "我想吃火锅" ;
char ch = strCa. charAt ( 3 ) ;
System . out. println ( ch) ;
for ( int i = 0 ; i < strCa. length ( ) ; i++ ) {
System . out. print ( strCa. charAt ( i) + "\t" ) ;
}
System . out. println ( ) ;
String strSs = "Good Afternoon" ;
String newStrSs = strSs. substring ( 5 ) ;
System . out. println ( strSs) ;
System . out. println ( newStrSs) ;
String substring = strSs. substring ( 5 , 10 ) ;
System . out. println ( substring) ;
String strIo = "sosappleapple" ;
int indexOf = strIo. indexOf ( 'a' ) ;
System . out. println ( indexOf) ;
int indexOf1 = strIo. indexOf ( 'a' , 5 ) ;
System . out. println ( indexOf1) ;
int indexOf2 = strIo. indexOf ( "apple" ) ;
System . out. println ( indexOf2) ;
int indexOf3 = strIo. indexOf ( "apple" , 4 ) ;
System . out. println ( indexOf3) ;
int lastIndexOf = strIo. lastIndexOf ( 'a' ) ;
System . out. println ( lastIndexOf) ;
}
}
String类型中的转换方法
1、byte[] getBytes();将当前字符串,转成字节数组
2、char[] toCharArray():将当前字符串转成字符数组
3、toUpperCase():将当前字符串,转成大写形式
4、toLowerCase();将当前字符串,转成小写形式
5、valueOf家族:可以将任意的数据类型,转成字符串 静态方法
package com. ujiuye. demos ;
import java. util. Arrays ;
import com. sun. org. apache. bcel. internal. generic. NEW;
public class Demo_13 {
public static void main ( String [ ] args) {
String strGb = "abc" ;
byte [ ] bytes = strGb. getBytes ( ) ;
System . out. println ( Arrays . toString ( bytes) ) ;
String strCa = "abc" ;
char [ ] chs = strCa. toCharArray ( ) ;
System . out. println ( Arrays . toString ( chs) ) ;
String strTc = "java" ;
String upperCase = strTc. toUpperCase ( ) ;
System . out. println ( upperCase) ;
String strLc = "JAVA" ;
System . out. println ( strLc. toLowerCase ( ) ) ;
int a = 10 ;
String valueOf = String . valueOf ( a) ;
System . out. println ( valueOf + 1 ) ;
System . out. println ( String . valueOf ( false ) + 1 ) ;
System . out. println ( String . valueOf ( 'a' ) + 'b' ) ;
String valueOf2 = String . valueOf ( new Person_1 ( ) ) ;
System . out. println ( valueOf2) ;
}
}
String类型中其他方法
1、replace(char oldChar, char newChar) :将调用者中的老串替换成新串
2、trim():去掉字符串左右两边的空格和制表符
package com. ujiuye. demos ;
public class Demo_14 {
public static void main ( String [ ] args) {
String str = "i love Php" ;
String newStr = str. replace ( "Php" , "java" ) ;
System . out. println ( newStr) ;
String strs = " hello gril " ;
String trim = strs. trim ( ) ;
System . out. println ( trim) ;
}
}
五、Math
1、常量:
E:自然对数的底数,2.7....
PI:圆周率,3.1415926
2、常用方法;
abs(数字类型) 求一个数字的绝对值
cbrt(double b)返回b的立方根
sqrt(double b)返回b的正平方根
ceil(double b)返回b的向上取整
floor(double b)返回b的向下取整
round(double b)返回b的四舍五入的结果
pow(int a,int b)返回的是a的b次幂
random():生成一个0.0----0.99999999的伪随机数
package com. ujiuye. demos ;
public class Demo_15 {
public static void main ( String [ ] args) {
System . out. println ( Math. E ) ;
System . out. println ( Math . PI) ;
System . out. println ( Math . abs ( - 9 ) ) ;
System . out. println ( Math . abs ( 1.99 ) ) ;
System . out. println ( Math . cbrt ( 9 ) ) ;
System . out. println ( Math . cbrt ( 8 ) ) ;
System . out. println ( Math . sqrt ( 9 ) ) ;
System . out. println ( Math . ceil ( 1.1 ) ) ;
System . out. println ( Math . floor ( 1.9 ) ) ;
System . out. println ( Math . round ( 1.5 ) ) ;
System . out. println ( Math . round ( 1.3 ) ) ;
System . out. println ( Math . pow ( 2 , 3 ) ) ;
System . out. println ( Math . pow ( 3 , 10 ) ) ;
}
}
六、System
1、用于描述系统资源的类型,该类没有构造方法,不能创建对象,直接使用静态常量和静态方法即可
2、常用字段:(该类中定义的常量)
System.in:标准的输入流,默认关联的是键盘
System.out:标准的输出流,默认关联的是控制台
System.err:标准的错误输出流,默认关联的是控制天,在eclipse中,错误流打印的结果是红色的
3、常用方法
(1)currentTimeMillis() :
返回的就是当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)
1s = 1000ms
作用:通过计算两次的差值,获取某段代码大概的运行时间
通过一些手段,将毫秒值,转成指定格式的日期
(2)gc():强制垃圾回收器回收内存中的垃圾对象
package com. ujiuye. demos ;
import java. io. IOException ;
import java. io. PrintStream ;
public class Demo_16 {
public static void main ( String [ ] args) throws IOException {
PrintStream out = System . out;
out. println ( "ad" ) ;
System . err. println ( "我是红色的" ) ;
long currentTimeMillis = System . currentTimeMillis ( ) ;
System . out. println ( currentTimeMillis) ;
long start = System . currentTimeMillis ( ) ;
for ( int i = 0 ; i<= 100 ; i++ ) {
System . out. println ( "9" ) ;
}
long end = System . currentTimeMillis ( ) ;
System . out. println ( "运算的时间为" + ( end - start) ) ;
System . out. println ( "-----------------gc方法------------------------------" ) ;
for ( int i = 0 ; i < 9 ; i++ ) {
new Laji ( ) ;
}
System . gc ( ) ;
}
}