java.lang-API整理(1)

本文深入探讨了Java中几个关键的接口与类,包括Cloneable接口、Iterable接口、Thread.UncaughtExceptionHandler接口以及Byte类、Character类和Class类。解析了它们的功能、使用场景及内部实现机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

一.Cloneable接口(浅复制)

此类实现了 Cloneable 接口,以指示 Object.clone() 方法可以合法地对该类实例进行按字段复制。此接口只是一个标记,无实际方法。

如果在没有实现 Cloneable 接口的实例上调用 Object 的 clone 方法,则会导致抛出 CloneNotSupportedException 异常。

按照惯例,实现此接口的类应该使用公共方法重写 Object.clone(它是受保护的)。请参阅 Object.clone()(一个本地方法),以获得有关重写此方法的详细信息。

注意,此接口不 包含 clone 方法。因此,因为某个对象实现了此接口就克隆它是不可能的。即使 clone 方法是反射性调用的,也无法保证它将获得成功。 

(一个可被clone的类,需要实现Cloneable接口同时还要重写Object.clone()方法)

 

 

二.Iterable接口

实现此接口的类,可以被foreach语句支持。(foreach底层也是被iterable支持)。

此接口只有一个方法:Iterator<T> iterator(),获取一个支持线性遍历的iterator实例。

 

三.Thread.UncaughtExceptionHandler(接口)

当 Thread 因未捕获的异常而突然终止时,调用处理程序的接口.

void uncaughtException(Thread t,Throwable e):当给定线程因给定的未捕获异常而终止时,调用该方法。

 

可以通过Thread.setUncaughtExceptionHandler(Handler)来设定“未捕获异常”的处理器。此时线程已经退出执行。

 

 

四.Byte类:

byte的对象类,提供了针对多种进制的string转换成byte的操作。

  • byte parseByte(String,int radix):将string按照指定的基数(进制)进行转换。
  • Byte valueOf(String,int radix):同上,底层基于parseByte
  • Byte decode(String):将合法的10、16、8等多种进制的string,转换成Byte对象,内部根据字符串匹配获取器基数特征,比如16进制以0x或者#开头,然后使用valueOf操作。
  • String toString():以基数为10的形式,返回字符串。如果需要其他基数的字符串转换,参见Integer.toString(int,int radix).

上述方法,最终依赖于Integer.parseInt(String,int radix)---->最终依赖Character.digit(char,radix).

 

 

五.Character类

Character 类在对象中包装一个基本类型 char 的值。Character 类型的对象包含类型为 char 的单个字段。字符信息基于 Unicode 标准。

char值也是可以和int值互相转换(最大值为1<<16),byte可以和short转换一样。

Character类中提供了大量辅助方法,请参考API。(大小写校验,空白校验,unicode值获取与转换)

 

六.Class类

Class类,是一种特殊的类,它实现了Serializable、Type等接口,Type接口是个标记接口以标记此类为一种“类型”(Type接口在reflect包中),

 Class 类的实例表示正在运行的 Java 应用程序中的类和接口。枚举是一种类,注释是一种接口。每个数组属于被映射为 Class 对象的一个类,

 所有具有相同元素类型和维数的数组都共享该 Class 对象。基本的 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示为 Class 对象。

Class 没有公共构造方法(构造器为private,只能有JVM来创造Class实例)。Class 对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass(ClassLoader.defineClass()) 方法自动构造的。Object.getClass()方法是本地方法,所有的统一类型的实例共享其Class实例。

 

  • static Class<?> forName(String className) throws ClassNotFoundException
  • static Class<?> forName(String className,boolean inited,ClassLoader loader) throws ClassNotFoundException

返回指定字符串类名或者接口相关的Class实例,等价于Class.forName(className,true,currentClassLoader),其中currentClassLoader为当前类的定义类加载器.

注意Class.forName将会导致类被初始化(加载,static区块被执行,static属性被初始化;注意static区块或者属性是按照其声明的顺序被实例化的).

public class TClass {

static {
System.out.println("Tclass static");
System.out.println("i:" + i);// 编译错误,i需要在此前 被声明,因为static信息是按照声明顺序被 编译和实例化
}
static Integer i = new Integer(2);//
}

Class.forName(String className)与ClassLoader.loadClass(String className)都能获取Class实例,其区别:

1) Class.forName(String className) == Class.forName(String className,true,currentClassLoader),其中第二个参数为"是否初始化class",所以forName(String className)

会初始化Class的static区块以及static属性.

2) classLoader.loadClass(String)(备注:此方法并非静态方法) == Class.forName(String className,false,classLoader),如果排除classLoader实例的不同,这两个方法的执行方式是一样的.

3) 通过字面值的方式获取Class实例,比如Class t = Timer.class,它和2)一样(即不初始化Class).

 

如果类信息在没有初始化,那么Class.newInstance()/new对象等都会触发实例化,且实例化只会触发一次.

 

  • public T newInstance():创建Class的一个对象实例,和执行空参数列表的构造函数一样.内部实现基于Constructor.newInstance(null).

1) 如果该类或其 默认构造方法是不可访问,抛出IllegalAccessException.

2) 如果此 Class 表示一个抽象类、接口、数组类、基本类型或 void; 或者该类没有默认构造方法(只有含参构造器); 

或者由于其他某种原因导致实例化失败;抛出InstantiationException.

3) 如果package不可访问(protected)或者具有安全管理器,抛出securityException.

 

 

  • public boolean isInsance(Object obj):具有和instanceOf运算符等效,用于判断某个对象是否是某种Class类型.
  • public boolean isAssignableFrom(Class clazz): 检测当前Class实例是否为clazz的超类或者接口或者和clazz为同一类型:Serializable.class.isAssignableFrom(String.class) 将返回true.
  • public boolean isInterface()/isArray()/isPrimitive():isPrimitive()检测class实例是否为一个基本类型,有九种预定义的 Class 对象,表示八个基本类型和 void。这些类对象由 Java 虚拟机创建,与其表示的基本类型同名,即 boolean、byte、char、short、int、long、float 和 double。
  • public boolean isAnnotation():当前Class实例是否为"注释",注意:如果是"注释"类型,它的isInterface()必将返回true.
  • public ClassLoader getClassLoader():获取当前Class实例的类加载器,有些实现可能使用null来表示引导类的加载器(bootstrap classLoader.).

如果此对象表示一个基本类型或者void或者是Object,则返回null.例如:Object.getClassLoader() == null将返回true.

java的API中的类,使用getClassLoader()将得到null(例如Stirng,Integer,Date,Timer等,这些JDK API均有引导类加载.)

 

  • public int getModifiers():获取当前Class的修饰词位标识符,需要通过Modifier类来辅助获取.

比如:Modifier.isPrivate(int value)可以计算出此类的modifiers值是否为private.

  • public Method[] getMethods()
  • public Field[] getFields()
  • public Constructor<?>[] getConstrunctor()

通过反射机制获取当前class的所有成员信息.包括来自继承类的信息.

getDeclaredFields/getDeclaredMethods/getDeclaredConstructors获取当前class直接声明的成员信息,不包括继承类的信息.

  • public T cast(Object obj):将一个对象强制转换成Class对象所表示的类或者接口.如果转换失败则抛出ClassCastException.
  • public Class asSubcalss(Class clazz):强制将当前class对象转换成指定的clazz的子类,如果强制转换有效,则返回子类型的引用,否则抛出"ClassCastException".
  • public boolean isAnnotationPresent(Annotation anno):检测annotation是否存在于当前class上.

同时Class还提供了获取类上的annotation列表的方法等.

uninstall silentlyCommand Line: abort vfprintf -XX:ErrorFile=C:\Users\Administrator\java_error_in_pycharm_%p.log -XX:HeapDumpPath=C:\Users\Administrator\java_error_in_pycharm.hprof -Xms256m -Xmx1500m -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -XX:CICompilerCount=2 -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:TieredOldPercentage=100000 -ea -Dsun.io.useCanonCaches=false -Dsun.java2d.metal=true -Djbr.catch.SIGABRT=true -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Djdk.nio.maxCachedBufferSize=2097152 -Djava.util.zip.use.nio.for.zip.file.access=true -Dkotlinx.coroutines.debug=off -Dllm.show.ai.promotion.window.on.start=false -Djb.vmOptionsFile=C:\Program Files\JetBrains\PyCharm Community Edition 2025.1.1\bin/pycharm64.exe.vmoptions -Xbootclasspath/a:C:\Program Files\JetBrains\PyCharm Community Edition 2025.1.1/lib/nio-fs.jar -Djava.system.class.loader=com.intellij.util.lang.PathClassLoader -Didea.vendor.name=JetBrains -Didea.paths.selector=PyCharmCE2025.1 -Djna.boot.library.path=C:\Program Files\JetBrains\PyCharm Community Edition 2025.1.1/lib/jna/amd64 -Djna.nosys=true -Djna.noclasspath=true -Dpty4j.preferred.native.folder=C:\Program Files\JetBrains\PyCharm Community Edition 2025.1.1/lib/pty4j -Dio.netty.allocator.type=pooled -Dintellij.platform.runtime.repository.path=C:\Program Files\JetBrains\PyCharm Community Edition 2025.1.1/modules/module-descriptors.jar -Didea.platform.prefix=PyCharmCore -Dwsl.use.remote.agent.for.nio.filesystem=true -Djava.nio.file.spi.DefaultFileSystemProvider=com.intellij.platform.core.nio.fs.MultiRoutingFileSystemProvider -Dsplash=true -Daether.connector.resumeDownloads=false -Dcompose.swing.render.on.graphics=true --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.ref=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.locks=ALL-UNNAMED --add-opens=java.base/jdk.internal.vm=ALL-UNNAMED --add-opens=java.base/sun.net.dns=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/sun.nio.fs=ALL-UNNAMED --add-opens=java.base/sun.security.ssl=ALL-UNNAMED --add-opens=java.base/sun.security.util=ALL-UNNAMED --add-opens=java.desktop/com.sun.java.swing=ALL-UNNAMED --add-opens=java.desktop/java.awt=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd.peer=ALL-UNNAMED --add-opens=java.desktop/java.awt.event=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED --add-opens=java.desktop/java.awt.image=ALL-UNNAMED --add-opens=java.desktop/java.awt.peer=ALL-UNNAMED --add-opens=java.desktop/javax.swing=ALL-UNNAMED --add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED --add-opens=java.desktop/javax.swing.text=ALL-UNNAMED --add-opens=java.desktop/javax.swing.text.html=ALL-UNNAMED --add-opens=java.desktop/sun.awt=ALL-UNNAMED --add-opens=java.desktop/sun.awt.datatransfer=ALL-UNNAMED --add-opens=java.desktop/sun.awt.image=ALL-UNNAMED --add-opens=java.desktop/sun.awt.windows=ALL-UNNAMED --add-opens=java.desktop/sun.font=ALL-UNNAMED --add-opens=java.desktop/sun.java2d=ALL-UNNAMED --add-opens=java.desktop/sun.swing=ALL-UNNAMED --add-opens=java.management/sun.management=ALL-UNNAMED --add-opens=jdk.attach/sun.tools.attach=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-opens=jdk.jdi/com.sun.tools.jdi=ALL-UNNAMED -Dide.native.launcher=true -Djcef.sandbox.ptr=000001C3FF477220 -Djcef.sandbox.cefVersion=122.1.9+gd14e051+chromium-122.0.6261.94 com.intellij.idea.Main Host: Intel(R) Core(TM) i9-14900K, 32 cores, 95G, Windows 11 , 64 bit Build 26100 (10.0.26100.3912) Time: Wed May 14 10:42:32 2025 Windows 11 , 64 bit Build 26100 (10.0.26100.3912) elapsed time: 10.140251 seconds (0d 0h 0m 10s)
05-15
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=1107de00-a0e9-4202-b416-fd01f3a35a3c, hashCode=2af8ed27)=datacenter1. Current DCs in this cluster are: datacenter1Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=1107de00-a0e9-4202-b416-fd01f3a35a3c, hashCode=2af8ed27)=datacenter1. Current DCs in this cluster are: datacenter1aused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=1107de00-a0e9-4202-b416-fd01f3a35a3c, hashCode=2af8ed27)=datacenter1. Current DCs in this cluster are: datacenter1Caused by: java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=1107de00-a0e9-4202-b416-fd01f3a35a3c, hashCode=2af8ed27)=datacenter1. Current DCs in this cluster are: datacenter1
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值