相关疑难解决方法(0)

IdentityHashMap返回不正确的值

根据我的理解,下面的代码应该false在进行identity基础比较时进行打印.

但是,当我运行以下代码时,它正在打印true:

public class Test1 {
  public static void main(String[] args) {
    IdentityHashMap m = new IdentityHashMap();
    m.put("A", new String("B"));
    System.out.println(m.remove("A", new String("B")));
  }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我理解这种行为吗?

java

33
推荐指数
2
解决办法
1476
查看次数

Java字符串声明

是什么区别String str = new String("SOME")String str="SOME" 是否这些声明给人的性能变化.

java string

23
推荐指数
2
解决办法
18万
查看次数

创建不可变对象,在没有new的情况下实例化

我可以创建一个只使用=运算符实例化的String类,就像类一样吗?或者这是StringJava中特定于类的功能?

java string object instance

16
推荐指数
3
解决办法
1984
查看次数

Java方法调用预期

这是一个java程序,有两个按钮用于更改整数值并显示它.但是在IntelliJIDEA中有两行

increase.addActionListener(incListener());
decrease.addActionListener(decListener());
Run Code Online (Sandbox Code Playgroud)

继续显示错误'预期方法调用'.

我不知道该怎么做才能解决这个问题.

任何帮助将不胜感激

谢谢

注意:完整代码附在下面.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JDialog {
public JPanel contentPane;
public JButton decrease;
public JButton increase;
public JLabel label;

public int number;

public Main() {
    setContentPane(contentPane);
    setModal(true);

    increase = new JButton();
    decrease = new JButton();
    increase.addActionListener(incListener());
    decrease.addActionListener(decListener());

    number = 50;
    label = new JLabel();
}

public class incListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        number++;
        label.setText("" + number);
    }
}

public class decListener implements …
Run Code Online (Sandbox Code Playgroud)

java

10
推荐指数
2
解决办法
6万
查看次数

为什么jvm每次使用new关键字创建字符串时都会创建新的字符串Object

如果为内存优化jvm创建string pool,那么为什么每次我们使用new关键字创建字符串时它都会创建新的对象,即使它存在于string pool

java string string-pool

10
推荐指数
2
解决办法
2702
查看次数

分配和创建字符串实例之间有什么区别?

在一次采访问题中,采访者问我

以下陈述之间的共同点和区别是什么:

String s = "Test";

String s = new String("Test");
Run Code Online (Sandbox Code Playgroud)

内存分配有什么不同吗?

java memory string

8
推荐指数
2
解决办法
3594
查看次数

Scala中的比较

val a=new String("Hello")和之间有什么区别val a="Hello"

例:

val a="Hello"
val b="Hello"
a eq b
res:Boolean=True
Run Code Online (Sandbox Code Playgroud)

同理:

val a=new String("Hello")
val b=new string("Hello")
a eq b
res:Bolean=False
Run Code Online (Sandbox Code Playgroud)

scala

7
推荐指数
1
解决办法
780
查看次数

Java: Beginner question regarding Strings

When creating a String in Java, what is the difference between these two:

String test = new String();
test = "foo";
Run Code Online (Sandbox Code Playgroud)

and

String test = "foo";
Run Code Online (Sandbox Code Playgroud)

When do I need to use the keyword new? Or are these two basically the same and they both create a new String object?

java string object

7
推荐指数
1
解决办法
87
查看次数

return(string expr)和返回New String(string expr)之间有什么区别?

这两种方法有区别吗?

public String toString() {
    return this.from.toString() + this.to.toString();
}

public String toString() {
    return new String(this.from.toString() + this.to.toString());
}
Run Code Online (Sandbox Code Playgroud)

(当然,假设from.toString()to.toString()方法正在返回字符串).

基本上我对Java中的字符串处理感到困惑,因为有时字符串被视为基本类型,即使它们是类实例.

java string types class object

6
推荐指数
1
解决办法
4717
查看次数

为什么使用new运算符创建的String在字符串池中创建字符串文字

我的问题是什么是在字符串池中创建字符串对象以及在声明字符串时在Heap上创建字符串对象的用途是什么String a = new String("abc");

为什么我们在创建字符串时在堆中创建字符串String a = "abc".

java string string-pool

6
推荐指数
1
解决办法
4054
查看次数

标签 统计

java ×9

string ×7

object ×3

string-pool ×2

class ×1

instance ×1

memory ×1

scala ×1

types ×1