基本数据类型与Object的关系
我知道大家是不是都听说过Object是所有类型的基类,但是这句话其实并不是正确的,因为java中基本数据类型跟Object是没有任何关系的.
这里举一个例子
这里在进行调用swap方法时,是不能直接将int类型传给swap(Object obj)方法的,因为Object其实跟基本数据类型没有一点关系,这个时候a就发现我们类型不匹配, 所以就自动包装成了Integer类型了, 这个时候就能跟Object产生联系了, 才能成功调用swap方法了.
Object、基本数据类型的包装类
Object类
Object类的基本方法:
getClass()、hashcode()、equals()、clone()、finalize()、toString()
1
2
3
4
5
6
7
8
9
10
11
|
public final native Class<?> getClass() //返回此 Object 运行时的类 public native int hashCode() //返回对象的哈希码 public boolean equals(Object obj) //判断其他对象与此对象是否“相等” protected native Object clone() throws CloneNotSupportedException //创建并返回此对象的一个副本 public String toString() //返回对象的字符串表示 protected void finalize() throws Throwable {} //垃圾回收时调用该方法 public final native void notify() //唤醒在此对象监视器上等待的单个线程 public final native void notifyAll() //唤醒在此对象监视器上等待的所有线程 public final native void wait( long timeout) throws InterruptedException //使当前对象的线程等待 timeout 时长 public final void wait( long timeout, int nanos) throws InterruptedException //使当前对象的线程等待 timeout 时长,或其他线程中断当前线程 public final void wait() throws InterruptedException //使当前对象的线程等待 |
基本数据类型
byte、int、short、long、double、float、boolean、char;
对应的包装类型也有八种:
Byte、Integer、Short、Long、Double、Float、Character、Boolean;(已final,不可重写)
将基本数据类型 转成 对象包装类型------装箱,反之为拆箱
1
2
3
4
5
6
7
8
|
public static void main(String[] args) { int num1 = 1 ; //将基本数据类型装箱成对象包装类型,编译器内置方法 Integer num2 = num1; Integer num3 = 3 ; //将对象数据类拆箱,该方法是java.lang.Number类中的 int num4 = num3; } |
继承关系:
Number类是 基本数据类型包装类 的父类
Number类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package java.lang; public abstract class Number implements java.io.Serializable { public abstract int intValue(); \\拆箱方法 public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); public byte byteValue() { return ( byte )intValue(); } public short shortValue() { return ( short )intValue(); } private static final long serialVersionUID = -8742448824652078965L; } |
Integer类常用方法:
-
parseInt(String s)
将字符串转换成Int -
toString()
转换成字符串 - 还有一种方法,任何类型+“ ” 即变成String类型
-
max()
、min()
。两个int的比较 - 两个静态成员变量:MAX_VALUE;MIN_VALUE(在其他数值类型中也有相同变量)
-
compare
方法,比较两个数。返回-1、0、1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class BasicNumber { public static void main(String args[]) { //最大最小值 int intmax=Integer.MAX_VALUE; int intmin=Integer.MIN_VALUE; System.out.println(intmax);System.out.println(intmin); //String to Int String string= "55" ; int testInt= 100 ; System.out.println(Integer.parseInt(string)+ 12 ); //parseInt方法 System.out.println(Integer.toString(testInt)); //toString方法 System.out.println(String.valueOf(testInt)); //valueOf方法 System.out.println(testInt+ "" ); //+""的隐式转换 //查看int占用的位数 System.out.println(Integer.SIZE); //compare方法 int intbig= 17 ; int intsmall= 6 ; System.out.println(Integer.compare(intsmall, intbig)); } } |
character类
Character类的判断功能
1
|
public static boolean isDigit( char ch) |
确定指定字符是否为数字。
1
|
public static boolean isLetter(charch) |
确定指定字符是否为字母。
1
|
public static boolean isLowerCase( char ch) |
确定是否是小写字母字符
1
|
public static boolean isUpperCase( char ch) |
确定是否大写字母字符
两个转换功能
1
|
public static int toLowerCase( char ch) |
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为小写。
1
|
public static int toUpperCase( char ch) |
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为大写。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_50370837/article/details/121340741