Java学习
常用类
Object类
超类、基类,所有类的直接或间接父类,位于继承数的最顶层
1. getClass()方法
返回引用中存储的实际对象类型 通常用于判断两个引用中实际存储对象类型是否一致 Class<? extends 类名>
public class Test {
public static void main ( String[ ] args) {
Test t1 = new Test ( ) ;
Class< ? extends Test > c1 = t1. getClass ( ) ;
System. out. println ( c1) ;
Test t2 = new Test ( ) ;
Class< ? extends Test > c2 = t2. getClass ( ) ;
if ( c1 == c2) {
System. out. println ( "存储对象类型相同" ) ;
} else {
System. out. println ( "不同" ) ;
}
}
}
2. hashCode()方法
返回该对象的哈希码值 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值 一般情况下相同对象放回相同哈希码
System. out. println ( t1. hashCode ( ) ) ;
System. out. println ( t2. hashCode ( ) ) ;
Test t3 = t1;
System. out. println ( t3. hashCode ( ) ) ;
3.equals()方法
比较两个对象地址是否相同 可进行重写,比较两个对象的内容是否相同
public class TestStudent {
public static void main ( String[ ] args) {
Student s1 = new Student ( ) ;
Student s2 = new Student ( ) ;
s1. setName ( "yjj" ) ; s1. setAge ( 3 ) ;
s2. setName ( "yjj" ) ; s2. setAge ( 3 ) ;
System. out. println ( s1. equals ( s2) ) ;
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
public class Student {
private String name;
private int 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 o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Student student = ( Student) o;
return age == student. age && Objects. equals ( name, student. name) ;
}
}
4.finalize()方法
当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列 垃圾对象:没有有效引用指向此对象时,为垃圾对象 垃圾回收:由GC销毁垃圾对象,释放数据存储空间 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象 手动回收机制:使用System。gc();通知JVM执行垃圾回收
public class TestStudent {
public static void main ( String[ ] args) {
new Student ( ) . setName ( "yjj" ) ;
System. gc ( ) ;
System. out. println ( "回收垃圾" ) ;
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
public class Student {
private String name;
private int 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
protected void finalize ( ) throws Throwable {
System. out. println ( this . name+ "被回收了" ) ;
}
}
包装类
基本数据类型所对应的引用数据类型 Object可统一所有数据,包装类的默认值时null 基本数据类型存放在栈中,引用数据类型存放在堆中
基本数据类型 引用数据类型 byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
类型转换中的装箱和拆箱
装箱:基本数据类型转换成引用数据类型 拆箱:引用数据类型转换成基本数据类型
public class Demo01 {
public static void main ( String[ ] args) {
int num1 = 10 ;
Integer i1 = new Integer ( num1) ;
Integer i2 = Integer. valueOf ( num1) ;
System. out. println ( i1) ;
System. out. println ( i2) ;
System. out. println ( "=======================" ) ;
Integer i3 = new Integer ( 20 ) ;
int num3 = i3. intValue ( ) ;
System. out. println ( num3) ;
System. out. println ( "======================" ) ;
int age = 30 ;
Integer i4 = age;
System. out. println ( i4) ;
int age1 = i4;
System. out. println ( age1) ;
System. out. println ( "=======================" ) ;
int n1 = 100 ;
String s1 = n1+ "" ;
System. out. println ( s1) ;
String s2 = Integer. toString ( n1) ;
System. out. println ( s2) ;
String s3 = Integer. toString ( n1, 16 ) ;
System. out. println ( s3) ;
String str = "150" ;
Integer. parseInt ( str) ;
System. out. println ( str) ;
String str2 = "true" ;
boolean b1 = Boolean. parseBoolean ( str2) ;
System. out. println ( b1) ;
}
}
Integer缓冲区
public class Demo02 {
public static void main ( String[ ] args) {
Integer i1 = new Integer ( 100 ) ;
Integer i2 = new Integer ( 100 ) ;
System. out. println ( i1 == i2) ;
Integer i3 = 100 ;
Integer i4 = 100 ;
System. out. println ( i3 == i4) ;
Integer i5 = 200 ;
Integer i6 = 200 ;
System. out. println ( i5 == i6) ;
}
}
String
public class Demo03 {
public static void main ( String[ ] args) {
String name = "张三" ;
name = "李四" ;
String str = new String ( "Java" ) ;
String str2 = new String ( "Java" ) ;
System. out. println ( str== str2) ;
System. out. println ( str. equals ( str2) ) ;
}
}
几种常用方法
public class Demo04 {
public static void main ( String[ ] args) {
String str = "12345678123" ;
System. out. println ( str. length ( ) ) ;
System. out. println ( str. charAt ( 0 ) ) ;
System. out. println ( str. contains ( "0" ) ) ;
System. out. println ( str. toCharArray ( ) ) ;
System. out. println ( Arrays. toString ( str. toCharArray ( ) ) ) ;
System. out. println ( str. indexOf ( "123" ) ) ;
System. out. println ( str. indexOf ( "123" , 3 ) ) ;
System. out. println ( str. lastIndexOf ( "3" ) ) ;
System. out. println ( "============================================" ) ;
String str1 = " hello Java " ;
System. out. println ( str1. trim ( ) ) ;
System. out. println ( str1. toUpperCase ( ) ) ;
System. out. println ( str1. toLowerCase ( ) ) ;
System. out. println ( str1. endsWith ( "java" ) ) ;
System. out. println ( str1. startsWith ( "Hello" ) ) ;
System. out. println ( str1. replace ( "Java" , "C++" ) ) ;
String[ ] s1 = str1. split ( " " ) ;
System. out. println ( s1. length) ;
for ( String s : s1) {
System. out. println ( s) ;
}
System. out. println ( "============================================" ) ;
String a = "JAVA" ;
String b = "java" ;
System. out. println ( a. equalsIgnoreCase ( b) ) ;
System. out. println ( a. compareTo ( b) ) ;
String c = "abc" ;
String d = "abcdef" ;
System. out. println ( c. compareTo ( d) ) ;
}
}
System. out. println ( "=====================案例===================" ) ;
String string = "this is a text" ;
String[ ] xx = string. split ( " " ) ;
for ( String s : xx) {
System. out. println ( s) ;
}
System. out. println ( string. replace ( "text" , "practice" ) ) ;
string. replace ( "text" , "easy text" ) ;
String finalString= "" ;
for ( int i = 0 ; i < xx. length; i++ ) {
char first = xx[ i] . charAt ( 0 ) ;
char upperFirst = Character. toUpperCase ( first) ;
String newString = upperFirst + xx[ i] . substring ( 1 ) ;
finalString += newString + " " ;
}
System. out. println ( finalString) ;
}
}