这两种方法有区别吗?
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中的字符串处理感到困惑,因为有时字符串被视为基本类型,即使它们是类实例.
我经常看到人们说String str = new String("my string")写作效率低于写作,String str = "my string",因为前者创建了一个静态的"my string"对象,然后new是一个从静态复制的String对象.
但是,鉴于这里的语言非常简单明了,我很难想象Java优化器不会花费任何精力简单地将前者转换为后者.为什么它会选择以更费力的方式做到这一点?如果Java优化它会有什么负面影响?
我知道String s = new String("Hello World")应该避免,因为它会为"Hello World"创造额外的空间,这在大多数情况下是不必要的.
解释为什么String s = new String("Hello World")应该避免的相关问题在这里:
"text"和new String("text")之间有什么区别?
但我们什么时候需要使用String s = new String("Hello World"),而不是String s = "Hello World"?这是我遇到的面试问题.
如果String s = new String("Hello World")在大多数情况下应该避免,为什么Java仍然允许这样做?
显然,在Java中,String s = "foo"首选String s = new String("foo").
为什么?在这两种情况下都不是创建新的字符串对象吗?为什么第一种情况会阻止调用构造函数?
能否请您澄清一下,在下面的案例中会创建多少个对象?为什么?我对此略感不安.
String s1 = "cat";
String s2 = "cat";
String s3 = "c"+"at";
String s4 = new String("cat");
String s5 = "kitty"+"cat";
String s6 = new String("kittycat");
String s7 = s5;
String s8= new String(s5); // Newly Added in this Question
Run Code Online (Sandbox Code Playgroud) 这听起来很容易,但我一直试图这样做, I want to initialize my custom class object array using curly braces
这是失败的例子:
类:
class Tranforminfo{
int left;
int top;
int right;
int bottom;
float rorate;
public Tranforminfo(int left, int top, int right, int bottom, float rorate) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.rorate = rorate;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:(不正确)
// attempt 1
Tranforminfo somedamn = new Tranforminfo[]{(1,2,3,4,5),(6,4,3,5,6)};
// attempt 2
Tranforminfo somedamn = new Tranforminfo[]{{1,2,3,4,5},{6,4,3,5,6}};
// attempt 3
Tranforminfo somedamn = new …Run Code Online (Sandbox Code Playgroud) 在面向对象的语言中,新对象的创建是通过使用new关键字(因为java中的内存分配是动态完成的).
即使String是一个类,如何在没有新关键字的情况下创建其对象?
即使它使用字符串池我也无法清楚地理解它: "可以创建一个用户定义的类,我们可以像String一样直接初始化变量"
我对String的实习方法没有很好的理解.
String s1="java"; // should create one object in String Constant pool
String ss="java"; // no object is created (java is already in String pool)..it refers to object in String constant pool
String s2= new String("Android").intern(); // should create 2 objects one in heap and second in String constant pool
String s3= new String("java").intern()// i guess only one object is created on heap and s3 will point to object in String constant pool (as 'java' already exist).so the object in …Run Code Online (Sandbox Code Playgroud) 正如我们所知,当我们创建String对象时,将创建String value = new String("ABC");
新String对象,当我们再次使用值变量时,将创建value="xyz"一个新String对象.
所以我的问题是,之前创建的String对象将在何时被垃圾收集?
我有这样的方法repeatedMethod:
public static void repeatedMethod() {
// something else
anotherMethod("myString");
// something else
}
public static void anotherMethod(String str) {
//something that doesn't change the value of str
}
Run Code Online (Sandbox Code Playgroud)
我repeatedMethod多次打电话.
我想问一下myString,static final在这种方法之外声明是否明智是这样的:
public static final String s = "myString";
public void repeatedMethod() {
anotherMethod(s);
}
Run Code Online (Sandbox Code Playgroud)
我认为,当我这样做时anotherMethod("myString"),String会创建一个新实例.由于我多次这样做,String因此创建了许多实例.
因此,最好只创建一个String外部实例,repeatedMethod每次只使用一个实例.
java ×10
string ×8
android ×1
class ×1
object ×1
oop ×1
optimization ×1
performance ×1
types ×1