Object类的概述
Object类的概述------------------------------
java.lang.Object类是Java语言中类层次结构的根类,也就是说任何一个类都是该类的直接或者间接子类,有就不是
如果定义一个Java类时没有使用extends关键字声明其父类,则其父类为 java.lang.Object 类,有就不是,当然,你也可以手动的继承Object
Object类定义了"对象"的基本行为,被子类默认继承
常用的方法------------------------------
Object ( )
int hashCode ( )
String toString ( )
Class < ? > getClass ( )
equals的重写和优化------------------------------
用于判断调用对象是否与参数对象相等
该方法默认比较两个对象的地址是否相等,与 == 运算符的结果一致,若希望比较两个对象的内容,则需要重写该方法
若该方法被重写后,则应该重写hashCode方法来保证结果的一致性
package com. lagou. task11 ;
import java. util. Objects ;
public class Student extends Object {
private int id;
private String name;
public Student ( ) {
}
public Student ( int id, String name) {
setId ( id) ;
setName ( name) ;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
if ( id > 0 ) {
this . id = id;
} else {
System . out. println ( "学号不合理哦" ) ;
}
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Student student = ( Student ) o;
return id == student. id &&
Objects . equals ( name, student. name) ;
}
@Override
public int hashCode ( ) {
return Objects . hash ( id, name) ;
}
@Override
public String toString ( ) {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}' ;
}
}
package com. lagou. task11 ;
import javax. swing. text. Style ;
public class StudentTest {
public static void main ( String [ ] args) {
Student s1 = new Student ( 1001 , "张飞" ) ;
Student s2 = new Student ( 1001 , "张飞" ) ;
Student s3 = s1;
boolean b1 = s1. equals ( s3) ;
System . out. println ( "b1 = " + b1) ;
System . out. println ( s1 == s2) ;
System . out. println ( "-----------------------------" ) ;
int ia = s1. hashCode ( ) ;
int ib = s2. hashCode ( ) ;
System . out. println ( "ia = " + ia) ;
System . out. println ( "ib = " + ib) ;
System . out. println ( "-----------------------------" ) ;
String str1 = s1. toString ( ) ;
System . out. println ( "str1 = " + str1) ;
System . out. println ( "-----------------------------" ) ;
System . out. println ( s1) ;
String str2 = "hello" + s1;
System . out. println ( "str2 = " + str2) ;
}
}
hashCode方法------------------------------
返回对象的哈希码值,哈希码值可以称为内存地址的编号
用于获取调用对象的哈希码值(内存地址的编号)
若两个对象调用equals方法相等,则各自调用该方法的结果必须相同
若两个调用对象equals方法不相等,则各自调用该方法的结果应该不相同
为了使得该方法与equals方法保持一致,需要重写该方法
toString方法------------------------------
用于获取调用对象的字符串形式
该方法默认返回的字符串为:包名.类名@哈希码值的十六进制
为了返回更有意义的数据,需要重写该方法
使用print或println打印引用或字符串拼接引用都会自动调用该方法,因为底层都操作了他的该方法,自己看源码就行了
Class<?> getClass()------------------------------
用于返回调用对象执行时的Class实例,反射机制使用
equals和hashCode方法的生成------------------------------
在以后的开发中,上述这些方法其实不需要我们主动去写
包的名称和功能------------------------------
java.lang包:该包是Java语言的核心包,并且该包中的所有内容由Java虚拟机自动导入
如:System类、String类、…
java.util包:该包是Java语言的工具包,里面提供了大量工具类以及集合类等
如:Scanner类、Random类、List集合、…
java.io包:该包是Java语言中的输入输出包,里面提供了大量读写文件相关的类等
如:FileInputStream类、FileOutputStream类、…
java.net包:该包是Java语言中的网络包,里面提供了大量网络编程相关的类等
如:ServerSocket类、Socket类、…
java.sql 包:该包是Java语言中的数据包,里面提供了大量操作数据库的类和接口等
如:DriverManager类、Connection接口、…
包装类的概念------------------------------
通常情况下基本数据类型的变量不是对象
为了满足万物皆对象的理念就需要对基本数据类型的变量进行打包封装处理变成对象
而负责将这些变量声明为成员变量进行对象化处理的相关类,叫做包装类
Person p = new Person ( ) ;
int num = 10 ;
public class MyInt {
private int num = 10 ;
}
Integer类的概述------------------------------
public final class Integer
extends Number
implements Comparable < Integer >
public abstract class Number
extends Object
implements Serializable
java.lang.Integer类内部包装了一个int类型的变量作为成员变量
主要用于实现对int类型的包装并提供int类型到String类之间的转换等方法
常用的常量------------------------------
public static final int MAX_VALUE ,表示int 类型可以描述的最大值,即2 ^ 31 - 1
public static final int MIN_VALUE ,表示int 类型可以描述的最小值,即- 2 ^ 31
public static final int SIZE ,表示int 类型采用二进制补码形式的位数
public static final int BYTES ,表示int 类型所占的字节个数
public static final Class TYPE ,表示int 类型的Class 实例
常用的方法------------------------------
Integer ( int value) ,根据参数指定的整数来构造对象(已过时)
Integer ( String s) ,根据参数指定的字符串来构造对象 (已过时)
int intValue ( ) ,获取调用对象中的整数值并返回
static Integer valueOf ( int i) ,根据参数指定整数值得到Integer 类型对象
boolean equals ( Object obj) ,比较调用对象与参数指定的对象是否相等
String toString ( ) ,返回描述调用对象数值的字符串形式
static int parseInt ( String s) ,将字符串类型转换为int 类型并返回
static String toString ( int i) ,获取参数指定整数的十进制字符串形式
static String toBinaryString ( int i) ,获取参数指定整数的二进制字符串形式
static String toHexString ( int i) ,获取参数指定整数的十六进制字符串形式
static String toOctalString ( int i) ,获取参数指定整数的八进制字符串形式
package com. lagou. task11 ;
public class IntegerTest {
public static void main ( String [ ] args) {
System . out. println ( "最大值是:" + Integer . MAX_VALUE ) ;
System . out. println ( "最小值是:" + Integer . MIN_VALUE ) ;
System . out. println ( "所表示二进制的位数是:" + Integer . SIZE ) ;
System . out. println ( "所占的字节个数是:" + Integer . BYTES ) ;
System . out. println ( "对应int类型的Class实例是:" + Integer . TYPE ) ;
System . out. println ( "--------------------------" ) ;
Integer it3 = Integer . valueOf ( 123 ) ;
System . out. println ( "it3 = " + it3) ;
Integer it4 = Integer . valueOf ( "456" ) ;
System . out. println ( "it4 = " + it4) ;
int ia = it4. intValue ( ) ;
System . out. println ( "获取到的整数数据是:" + ia) ;
System . out. println ( "--------------------------" ) ;
Integer it5 = 100 ;
int ib = it5;
System . out. println ( "it5 = " + it5) ;
System . out. println ( "ib = " + ib) ;
System . out. println ( "--------------------------" ) ;
Integer it6 = 127 ;
Integer it7 = 127 ;
Integer it8 = new Integer ( 127 ) ;
Integer it9 = new Integer ( 127 ) ;
System . out. println ( it6 == it7) ;
System . out. println ( it6. equals ( it7) ) ;
System . out. println ( it8 == it9) ;
System . out. println ( it8. equals ( it9) ) ;
System . out. println ( "--------------------------" ) ;
int ic = Integer . parseInt ( "200" ) ;
System . out. println ( "字符串转化为整数的结果是:" + ic) ;
System . out. println ( "根据参数指定的整数获取对应的十进制字符串是:" + Integer . toString ( ic) ) ;
System . out. println ( "根据参数指定的整数获取对应的二进制字符串是:" + Integer . toBinaryString ( ic) ) ;
System . out. println ( "根据参数指定的整数获取对应的十六进制字符串是:" + Integer . toHexString ( ic) ) ;
System . out. println ( "根据参数指定的整数获取对应的八进制字符串是:" + Integer . toOctalString ( ic) ) ;
System . out. println ( it6. equals ( it7) ) ;
System . out. println ( it6. toString ( ) ) ;
}
}
Double类的概述------------------------------
public final class Double
extends Number
implements Comparable < Double >
public abstract class Number
extends Object
implements Serializable
java.lang.Double类型内部包装了一个double类型的变量作为成员变量
主要用于实现对double类型的包装并提供double类型到String类之间的转换等方法
常用的常量------------------------------
public static final int SIZE ,表示double 类型的二进制位数
public static final int BYTES ,表示double 类型的字节个数
public static final Class TYPE ,表示double 类型的Class 实例
常用的方法------------------------------
Double ( double value) ,根据参数指定的浮点数据来构造对象(已过时)
Double ( String s) ,根据参数指定的字符串来构造对象 (已过时)
double doubleValue ( ) ,获取调用对象中的浮点数据并返回
static Double valueOf ( double d) ,根据参数指定浮点数据得到Double 类型对象
boolean equals ( Object obj) ,比较调用对象与参数指定的对象是否相等
String toString ( ) ,返回描述调用对象数值的字符串形式
static double parseDouble ( String s) ,将字符串类型转换为double 类型并返回
boolean isNaN ( ) ,判断调用对象的数值是否为非数字
package com. lagou. task11 ;
public class DoubleTest {
public static void main ( String [ ] args) {
System . out. println ( "所表示二进制的位数是:" + Double . SIZE ) ;
System . out. println ( "所占的字节个数是:" + Double . BYTES ) ;
System . out. println ( "对应double类型的Class实例是:" + Double . TYPE ) ;
Double ia = new Double ( 1.0 ) ;
System . out. println ( "ia = " + ia) ;
Double ib = new Double ( "1.0" ) ;
System . out. println ( "ib = " + ib) ;
System . out. println ( ia. equals ( ib) ) ;
System . out. println ( ia. toString ( ) ) ;
Double db1 = Double . valueOf ( 3.14 ) ;
System . out. println ( "db1 = " + db1) ;
double d1 = db1. doubleValue ( ) ;
System . out. println ( "d1 = " + d1) ;
System . out. println ( "---------------------------------------------" ) ;
Double db2 = 3.14 ;
double d2 = db2;
System . out. println ( "---------------------------------------------" ) ;
double d3 = Double . parseDouble ( "13.14" ) ;
System . out. println ( "d3 = " + d3) ;
System . out. println ( "db2对象的判断结果是:" + db2. isNaN ( ) ) ;
Double db3 = Double . valueOf ( 0 / 0.0 ) ;
System . out. println ( "db2对象的判断结果是:" + db3. isNaN ( ) ) ;
}
}
java.lang.Number类是个抽象类,是上述几个类(也是所有整型和浮点的包装类的父类)的父类来描述所有类共有的成员
Boolean类的概述------------------------------
public final class Boolean
extends Object
implements Serializable , Comparable < Boolean >
java.lang.Boolean类型内部包装了一个boolean类型的变量作为成员变量
主要用于实现对boolean类型的包装并提供boolean类型到String类之间的转换等方法
常用的常量------------------------------
public static final Boolean FALSE ,对应基值为false 的对象
public static final Boolean TRUE ,对应基值为true 的对象
public static final Class TYPE ,表示boolean 类型的Class 实例
常用的方法------------------------------
Boolean ( boolean value) ,根据参数指定的布尔数值来构造对象(已过时)
Boolean ( String s) ,根据参数指定的字符串来构造对象 (已过时)
boolean booleanValue ( ) ,获取调用对象中的布尔数值并返回
static Boolean valueOf ( boolean b) ,根据参数指定布尔数值得到Boolean 类型对象
boolean equals ( Object obj) ,比较调用对象与参数指定的对象是否相等
String toString ( ) ,返回描述调用对象数值的字符串形式
static boolean parseBoolean ( String s) ,将字符串类型转换为boolean 类型并返回
package com. lagou. task11 ;
public class BooleanTest {
public static void main ( String [ ] args) {
System . out. println ( "对应基值为false的对象是:" + Boolean . FALSE ) ;
System . out. println ( "对应基值为true的对象是:" + Boolean . TYPE ) ;
System . out. println ( "boolean类型的Class实例是:" + Boolean . TYPE ) ;
Boolean ia = new Boolean ( true ) ;
System . out. println ( "ia = " + ia) ;
Boolean ib = new Boolean ( "false" ) ;
System . out. println ( "ib = " + ib) ;
System . out. println ( ia. equals ( ib) ) ;
System . out. println ( ia. toString ( ) ) ;
Boolean bo1 = Boolean . valueOf ( true ) ;
System . out. println ( "bo1 = " + bo1) ;
boolean b1 = bo1. booleanValue ( ) ;
System . out. println ( "b1 = " + b1) ;
System . out. println ( "----------------------------------------------" ) ;
Boolean bo2 = false ;
boolean b2 = bo2;
System . out. println ( "b2 = " + b2) ;
System . out. println ( "----------------------------------------------" ) ;
boolean b3 = Boolean . parseBoolean ( "TRUE" ) ;
System . out. println ( "b3 = " + b3) ;
}
}
Character类的概述------------------------------
public final class Character
extends Object
implements Serializable , Comparable < Character >
java.lang.Character类型内部包装了一个char类型的变量作为成员变量
主要用于实现对char类型的包装并提供字符类别的判断和转换等方法
常用的常量------------------------------
public static final int SIZE ,表示char 类型的二进制位数
public static final int BYTES ,表示char 类型的字节个数
public static final Class TYPE ,表示char 类型的Class 实例
常用的方法------------------------------
Character ( char value) ,根据参数指定的字符数据来构造对象(已过时)
char charValue ( ) ,获取调用对象中的字符数据并返回
static Character valueOf ( char c) ,根据参数指定字符数据得到Character 类型对象
boolean equals ( Object obj) ,比较调用对象与参数指定的对象是否相等
String toString ( ) ,返回描述调用对象数值的字符串形式
static boolean isUpperCase ( char ch) ,判断参数指定字符是否为大写字符
static boolean isLowerCase ( char ch) ,判断参数指定字符是否为小写字符
static boolean isDigit ( char ch) ,判断参数指定字符是否为数字字符
static char toUpperCase ( char ch) ,将参数指定的字符转换为大写字符
static char toLowerCase ( char ch) ,将参数指定的字符转换为小写字符
package com. lagou. task11 ;
public class CharacterTest {
public static void main ( String [ ] args) {
System . out. println ( "char类型的二进制位数是:" + Character . SIZE ) ;
System . out. println ( "char类型的字节个数是:" + Character . BYTES ) ;
System . out. println ( "char类型的Class实例是:" + Character . TYPE ) ;
Character ia = new Character ( 'a' ) ;
System . out. println ( "ia = " + ia) ;
Character ib = new Character ( 'b' ) ;
System . out. println ( ia. equals ( ib) ) ;
System . out. println ( ia. toString ( ) ) ;
Character ca1 = Character . valueOf ( 'a' ) ;
System . out. println ( "ca1 = " + ca1) ;
char c1 = ca1. charValue ( ) ;
System . out. println ( "c1 = " + c1) ;
System . out. println ( "----------------------------------------" ) ;
Character ca2 = 'b' ;
char c2 = ca2;
System . out. println ( "c2 = " + c2) ;
System . out. println ( "----------------------------------------" ) ;
System . out. println ( Character . isUpperCase ( c2) ) ;
System . out. println ( Character . isLowerCase ( c2) ) ;
System . out. println ( Character . isDigit ( c2) ) ;
System . out. println ( "转换为大写字符是:" + Character . toUpperCase ( c2) ) ;
System . out. println ( "转换为小写字符是:" + Character . toLowerCase ( c2) ) ;
}
}
包装类的使用总结------------------------------
基本数据类型转换为对应包装类的方式调用包装类的构造方法或静态方法(xxx.valueOf方法)即可
获取包装类对象中基本数据类型变量数值的方式调用包装类中的xxxValue方法即可
字符串转换为基本数据类型的方式调用包装类中的parseXxx方法即可
Math类的概述------------------------------
public final class Math
extends Object
java.lang.Math类主要用于提供执行数学运算的方法,如:对数,平方根
常用的方法------------------------------
static int max ( int a, int b) ,返回两个参数中的最大值
static int min ( int a, int b) ,返回两个参数中的最小值
static double pow ( double a, double b) ,返回第一个参数的幂
static int abs ( int a) ,返回参数指定数值的绝对值
static long round ( double a) ,返回参数四舍五入的结果
static double sqrt ( double a) ,返回参数的平方根
static double random ( ) ,返回0.0 到1.0 的随机数,[ 0.0 ~ 1.0 ) ,包括0.0 ,不包括1.0
package com. lagou. task11 ;
public class MathTest {
public static void main ( String [ ] args) {
System . out. println ( "获取两个整数中最大值的结果是:" + Math . max ( 10 , 20 ) ) ;
System . out. println ( "获取两个整数中最小值的结果是:" + Math . min ( 10 , 20 ) ) ;
System . out. println ( "获取次方的结果是:" + Math . pow ( 2 , 3 ) ) ;
System . out. println ( "获取绝对值的结果是:" + Math . abs ( - 5 ) ) ;
System . out. println ( "进行四舍五入的结果是:" + Math . round ( 3.14 ) ) ;
System . out. println ( "该整数的平方根是:" + Math . sqrt ( 16 ) ) ;
System . out. println ( "生成的随机数是:" + Math . random ( ) ) ;
System . out. println ( Math . round ( 3.5 ) ) ;
System . out. println ( Math . sqrt ( 9 ) ) ;
System . out. println ( Math . abs ( - 39 ) ) ;
System . out. println ( Math . abs ( 89 ) ) ;
System . out. println ( Math . pow ( 2 , - 2 ) ) ;
System . out. println ( Math . random ( ) * 4 ) ;
}
}
BigDecimal类的概述------------------------------
public class BigDecimal
extends Number
implements Comparable < BigDecimal >
public abstract class Number
extends Object
implements Serializable
由于float类型和double类型在运算时可能会有误差
若希望实现精确运算则借助java.math.BigDecimal类型加以描述
常用的方法------------------------------
BigDecimal ( String val) ,根据参数指定的字符串来构造对象
BigDecimal add ( BigDecimal augend) ,用于实现加法运算
BigDecimal subtract ( BigDecimal subtrahend) ,用于实现减法运算
BigDecimal multiply ( BigDecimal multiplicand) ,用于实现乘法运算
BigDecimal divide ( BigDecimal divisor) ,用于实现除法运算
package com. lagou. task11 ;
import java. math. BigDecimal ;
import java. math. RoundingMode ;
public class BigDecimalTest {
public static void main ( String [ ] args) {
BigDecimal bd1 = new BigDecimal ( "5.2" ) ;
BigDecimal bd2 = new BigDecimal ( "1.3" ) ;
System . out. println ( "实现加法运算的结果是:" + bd1. add ( bd2) ) ;
System . out. println ( "实现减法运算的结果是:" + bd1. subtract ( bd2) ) ;
System . out. println ( "实现乘法运算的结果是:" + bd1. multiply ( bd2) ) ;
System . out. println ( "实现除法运算的结果是:" + bd1. divide ( bd2) ) ;
System . out. println ( "---------------------------------------------------------------" ) ;
System . out. println ( 0.1 + 0.2 ) ;
BigDecimal bd3 = new BigDecimal ( "0.1" ) ;
BigDecimal bd4 = new BigDecimal ( "0.2" ) ;
System . out. println ( "精确计算的结果是:" + bd3. add ( bd4) ) ;
System . out. println ( "---------------------------------------------------------------" ) ;
BigDecimal bd5 = new BigDecimal ( "2" ) ;
BigDecimal bd6 = new BigDecimal ( "0.3" ) ;
System . out. println ( "除法运算的结果是:" + bd5. divide ( bd6, RoundingMode . HALF_UP ) ) ;
}
}
BigInteger类的概念------------------------------
public class BigInteger
extends Number
implements Comparable < BigInteger >
public abstract class Number
extends Object
implements Serializable
若希望表示比long类型范围还大的整数数据,则需要借助java.math.BigInteger类型描述
常用的方法------------------------------
BigInteger ( String val) ,根据参数指定的字符串来构造对象
BigInteger add ( BigInteger val) ,用于实现加法运算
BigInteger subtract ( BigInteger val) ,用于实现减法运算
BigInteger multiply ( BigInteger val) ,用于实现乘法运算
BigInteger divide ( BigInteger val) ,用于实现除法运算
BigInteger remainder ( BigInteger val) ,用于实现取余运算
BigInteger [ ] divideAndRemainder ( BigInteger val) ,用于实现取商和余数的运算
package com. lagou. task11 ;
import java. math. BigInteger ;
public class BigIntegerTest {
public static void main ( String [ ] args) {
BigInteger bi1 = new BigInteger ( "20" ) ;
BigInteger bi2 = new BigInteger ( "8" ) ;
System . out. println ( "实现加法运算的结果是:" + bi1. add ( bi2) ) ;
System . out. println ( "实现减法运算的结果是:" + bi1. subtract ( bi2) ) ;
System . out. println ( "实现乘法运算的结果是:" + bi1. multiply ( bi2) ) ;
System . out. println ( "实现除法运算的结果是:" + bi1. divide ( bi2) ) ;
System . out. println ( "实现取余运算的结果是:" + bi1. remainder ( bi2) ) ;
System . out. println ( "-----------------------------------------------------" ) ;
BigInteger [ ] arr = bi1. divideAndRemainder ( bi2) ;
for ( int i = 0 ; i < arr. length; i++ ) {
System . out. println ( "下标为" + i + "的元素是:" + arr[ i] ) ;
}
}
}
实际上他们之所以可以解决精度,一般都是翻倍解决(但是这样的解决通常考虑字符串,因为数字在变化时,可能会出现问题,他也并没有将其变成字符串,可能有其他的考虑影响吧,或者没有修复),比如都变成整数后,然后再变回来,像这样的思想,大概如此,可能有其他的操作来完成,并且可能给出限制(如除不尽就会报错,而不会舍弃结果的小数,寻常的直接打印2/0.3就会舍弃结果的小数的)
当然,前面也提到了随机数,这里可以说明一下,计算机是如何处理随机数的,实际上计算机的都是伪随机(并不是真正的随机),这里实现一下:
package com ;
public class SimpleRandom {
private long seed;
private static final long a = 1664525 ;
private static final long c = 1013904223 ;
private static final long m = ( long ) Math . pow ( 2 , 32 ) ;
public SimpleRandom ( long seed) {
this . seed = seed;
}
public int nextInt ( ) {
seed = ( a * seed + c) % m;
return ( int ) seed;
}
public int nextInt ( int bound) {
return Math . abs ( nextInt ( ) ) % bound;
}
public static void main ( String [ ] args) {
SimpleRandom random = new SimpleRandom ( System . currentTimeMillis ( ) ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( random. nextInt ( 100 ) ) ;
}
}
}
明显,伪随机的基本原理是使用自身来赋值自身,只不过数字是比较合理,让随机最大化的,当然,还是有问题,如果时间戳固定那么每次重新开始,基本都不是随机了(具体可以参照更好的伪随机代码)