是否所有类名都保证可以插入所有JVM中? [英] Are all class names guaranteed to be interned in all JVM?

查看:75
本文介绍了是否所有类名都保证可以插入所有JVM中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在尝试高效地切换类,并且想知道这是否可行:生成与编译器生成的切换字符串"代码相同的代码,但使用intern而不是equals.

I am still trying to switch efficiently on classes, and I am wondering if this could work : generate the same code as a compiler-generated "switch on strings" code but using intern instead of equals.

有关:为什么我们不能在Java 7+中打开类?

推荐答案

我怀疑您可能会遇到这种方法的麻烦.简单的答案是,JVM默认情况下会实习所有Strings.但是,您的问题远不止于此-类的名称在单个JVM中不是唯一的.如果同一类由两个不同的类加载器加载,则JVM中将有该类对象的两个实例,这些实例具有相同的名称,但彼此互不相同.如果要对切换类的语义建模,则需要一种捕获行为的方法.

I suspect you could get into trouble with this kind of approach. The simple answer is that the JVM interns all Strings by default. However, your problem is much deeper than that - the name of a class is not unique within a single JVM. If the same class is loaded by two different classloaders then there will be two instances of that class object within the JVM which have the same name but which are not equal to each other. If you want to model the semantics of switching on a class then you need a way of capturing that behaviour.

这篇关于是否所有类名都保证可以插入所有JVM中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆