首页 \ 问答 \ 字符串池与concat一起使用时的行为有所不同?(String Pool behaves differently when used with concat? [duplicate])

字符串池与concat一起使用时的行为有所不同?(String Pool behaves differently when used with concat? [duplicate])

这个问题在这里已经有了答案:

String s1 = "Hello".concat("World");
String s3 = new String("HelloWorld"); //Line-2
String s2 = s1.intern();
System.out.println(s1 == s2); //false
System.out.println(s1 == s3); //false
System.out.println(s2 == s3); //false

如果我删除了Line-2并比较s1 == s2,它将返回true。 任何人都可以解释我在Line-2之后的字符串池中究竟发生了什么? 什么是在堆中的每一行和常量池中发生的?

据我所知,s1将在常量池中创建“HelloWorld”。 但仍然是s1 == s2是错误的?


This question already has an answer here:

String s1 = "Hello".concat("World");
String s3 = new String("HelloWorld"); //Line-2
String s2 = s1.intern();
System.out.println(s1 == s2); //false
System.out.println(s1 == s3); //false
System.out.println(s2 == s3); //false

If I removed Line-2 and compare s1==s2, it will return true. Could anyone explain me what exactly happens in string pool after Line-2? And whats happening in each line in heap and in constant pool ?

From what i understand s1 will create "HelloWorld" in constant pool. But still s1 == s2 is false ?


原文:https://stackoverflow.com/questions/48139521
更新时间:2023-11-21 10:11

最满意答案

你的transform()方法必须覆盖超类中的通用抽象transform()方法,即你应该有这样的东西:

public class MyAssembler extends AbstractAssembler {

    @Override
    public <T extends AbstractValue> void transform(T value) {

    }
}

如果你想用一个实际的类型(例如MyValue )来调用它,你应该这样做:

MyValue value = new MyValue();
new MyAssembler().transfer(value);

其中明确指定类型参数( new MyAssembler().<MyValue>transfer(value); )是可选的,因为它将由编译器推断。


如果你希望MyAssembertransform方法只能用于MyValue ,那么你应该这样做:

public class MyAssembler extends AbstractAssembler<MyValue> {

    @Override
    public void transform(MyValue value) {

    }
}

Your transform() method must override the generic abstract transform() method in the super class, i.e. you should have something like:

public class MyAssembler extends AbstractAssembler {

    @Override
    public <T extends AbstractValue> void transform(T value) {

    }
}

If you want to invoke it with an actual type (e.g. MyValue) you should do:

MyValue value = new MyValue();
new MyAssembler().transfer(value);

where explicitly specifying the type-parameter (new MyAssembler().<MyValue>transfer(value);) is optional, as it will be inferred by the compiler.


If you however wish MyAssember's transform method to work only with MyValue, then you should do:

public class MyAssembler extends AbstractAssembler<MyValue> {

    @Override
    public void transform(MyValue value) {

    }
}

相关问答

更多
  • fooMethod(Class) , fooMethod(Class)的签名与fooMethod(Class)的签名相同,因为Class的擦除只是Class ( JLS 4.6 )。 因此, fooMethod(Class)是fooMethod(Class)的子签名,但不是相反的( JLS 8.4.2 )。 要覆盖实例方法,您需要重写方法作为重写方法的子签名( JLS 8.4.8.1 )。 这显然不是这种情况。 现在我们已经确定了您的子类方法不会根据JLS重写超类方法,让我们看看类型 ...
  • 你想要myMethod的重载版本吗? 那么你不应该使用T两次,但是像这样: public interface Interface { public void myMethod(T x); } public interface ExtendedInterface extends Interface { public void myMethod(V x); } 现在有可能有这样的事情: class MyClass implements ExtendedInterface
  • 这个答案你可以在这里找到: http : //golang.org/doc/faq#generics 为什么Go没有通用类型? 在某些时候可能会添加泛型。 我们对他们并不感到紧迫,尽管我们了解一些程序员的意图。 通用型是方便的,但它们在系统和运行时的复杂性方面都是昂贵的。 我们还没有发现一个设计,赋予与复杂性相称的价值,尽管我们继续考虑。 同时,Go的内置地图和切片,以及使用空接口构建容器(具有明确的取消装箱)的能力意味着在许多情况下,如果不太顺利,可以编写执行泛型启动的代码。 这仍然是一个公开的问题。 t ...
  • 你不能覆盖声明为的方法 void say(List list) // A 同 void say(List list) // B 只是因为类型不相同。 例如, List匹配List List但不是List ,所以 List integers = Arrays.asList(1, 2, 3 ...
  • 这个错误非常明显: getProducts()被声明为返回一个List但是你返回一个List 。 我认为我们同意这两个不相同。 看看你的代码,它看起来像你实际上不想要一个泛型类,因为ComputerOrder只接受ComputerPart 。 你想要的东西如下所示: public class ComputerOrder extends GenericOrder { @Override public List
  • 感谢类型擦除 ,这: public void doSomething(T data); 真的意味着这一点: public void doSomething(Object data); 所以不,没有办法用更严格的参数类型来覆盖。 也在这个代码中: class ClassA { public void doSomething(T data) {}; } 类名中的类型参数和方法中的类型参数实际上是不同的参数。 这就像在更高的范围内声明与变量相同名称的局部变量。 您可以在Clas ...
  • 根据我的评论,当由AVLTree专门化时, AVLTree缺少一个类型参数。 class BinaryTree> implements Iterable { public void add(E toInsert) { // ... } public Iterator iterator() { return null; } } class AVLTree
  • 基本上你要求的是逆变方法参数,例如非通用示例如下: interface I { void m(String s); } class C implements I { @Override public void m(Object o) {} } void(Object)是void(String)的子签名,因为扩展转换始终是OK。 Java没有这个。 对于泛型,您可以将泛型方法覆盖为非泛型: class NotGeneric implements Thing { @Overr ...
  • 你可以让Generics有两个参数: public class Node 然后你可以声明像这样的对象: Node> node; You can have Generics with two parameters like: public class Node Then you can declare objects like: Node> node;
  • 你的transform()方法必须覆盖超类中的通用抽象transform()方法,即你应该有这样的东西: public class MyAssembler extends AbstractAssembler { @Override public void transform(T value) { } } 如果你想用一个实际的类型(例如MyValue )来调用它,你应该这样做: MyValue value = new MyVal ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。