String obj = null;
obj= new String("Samuel");
//vs
String obj = null;
obj="Samuel";
Run Code Online (Sandbox Code Playgroud)
这两种初始化String的方法有什么区别吗?
String s="hi";
String s1=new String("hi");
Run Code Online (Sandbox Code Playgroud)
从内存的角度来看,s和s1存储在哪里?无论是在堆内存还是堆栈中。
s 指向“hi”,s1 指向 hi 所在的内存位置?
请帮忙?
考虑一下代码:
public class Stringer {
public static void main(String[] args) {
String s1 = "SomeLine";
System.out.println(s1); // prints SomeLine
s1 = "AnotherLine";
System.out.println(s1); // prints AnotherLine
}
}
Run Code Online (Sandbox Code Playgroud)
当我改变s1来自SomeLine于AnotherLine,因为字符串是不可改变的,这是否意味着SomeLine丢失,可享有GC?
非常感激
我真的不知道何时以及为什么我应该使用String的构造函数而不是文字方式.我已经看过了,发现了一些差异的答案,这些差异并不是很清楚......但是最好的做法还是"为什么?" 什么时候?"
我知道文字定义由JVM处理,但它们不由垃圾收集器处理(如静态员工,注释......)......这真的是JVM内存的问题吗?
我在这里添加了我发现的最短比较:
创建String作为new()和literal有什么区别? 当我们使用new()运算符创建字符串时,它是在堆中创建的,而不是添加到字符串池中,而使用文字创建的字符串是在存在于堆的PermGen区域中的字符串池本身中创建的.
感谢您的时间和帮助.
我碰到了这段代码,有些困惑
java.lang.String s = new String("hello");
Run Code Online (Sandbox Code Playgroud)
我不确定将哪个变量s初始化为java.lang.String,以及此vs String hold =“ hello”的目的是什么。我试图浏览一些文档,但找不到任何东西。
iv创建了一个Android应用程序,我的问题是,当我输入一个数字之前我按+ = - /*按钮我的应用程序停止工作,无论如何我可以使它,如果没有数字输入它不会停止工作?
package steven.mcilhone.calculatorcoursework;
import java.math.BigDecimal;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
EditText firstValue;
EditText secondValue;
TextView result;
Button addbtn, subtractbtn, dividebtn, multiplybtn, equalbtn, clearbtn;
BigDecimal firstNum, secondNum;
String Operator = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
addbtn = (Button) findViewById(R.id.addbtnID);
subtractbtn = (Button) findViewById(R.id.subtractbtnID);
dividebtn = (Button) findViewById(R.id.dividebtnID);
multiplybtn = (Button) findViewById(R.id.multiplybtnID);
equalbtn = (Button) findViewById(R.id.equalbtnID);
clearbtn = (Button) findViewById(R.id.clearbtn); …Run Code Online (Sandbox Code Playgroud) 所以我的问题是关于声明和分配字符串.
我通常声明字符串的方式是执行以下操作:
String s1 = "Stackoverflow";
Run Code Online (Sandbox Code Playgroud)
然后,如果我需要更改s1的值,我会执行以下操作:
s1 = "new value";
Run Code Online (Sandbox Code Playgroud)
今天我找到了另一种方法,然后声明一个字符串就像:
String s2 = new String("Stackoverflow");
Run Code Online (Sandbox Code Playgroud)
然后更改值将是:
s2 = new String("new value");
Run Code Online (Sandbox Code Playgroud)
我的问题是两者之间有什么区别,还是只是优惠.从第四行查看代码
s2 = new String ("new value");
Run Code Online (Sandbox Code Playgroud)
我假设这样做会创建一个新的内存位置,然后s2指向它,所以我怀疑它会用于更改值,但我可以看到它在声明字符串时被使用.
考虑一下代码:
public class Strings {
public static void createStr()
{
String s1 = new String("one");
String s2 = "one";
System.out.println(s1);
System.out.println(s2);
}
public static void main(String args[])
{
createStr();
}
}
Run Code Online (Sandbox Code Playgroud)
String s1 = new String("one");和之间有什么区别String s2 = "one";?
String str = "ABC"和之间有什么区别String str = new String("ABC")?
What is the difference between creating Object, for example in my code, type Obj with operator "new" and Object type String?
public class Objs {
int a;
public Objs(int a)
{
this.a = a;
}
public static void main(String[] args)
{
String str = new String("Hello");
String str1 = str; // (str1 == str) == true
str += ", world!!"; // after this (str1 == str) == false - Why?
Objs o = new Objs(4);
Objs o1 = o; //(o …Run Code Online (Sandbox Code Playgroud) 下面显示了几种创建字符串的方法.在注释的表达式后面添加问题.
String str = "test";
String str1 = new String(str); //Will it invoke the Constructor of String(String)?
String str2 = new String("test");//Will it invoke the Constructor of String(String)?
String str3 = str; //Which Constructor will it invoke? Or str3 only reference to str and "test" without being constructed?
String str4 = "test";//Which Constructor will it invoke? Or str4 only reference to str and "test" without being constructed?
String strnew = new String("testnew");//Will this expression create "testnew" in String Constant Pool …Run Code Online (Sandbox Code Playgroud) java ×11
string ×8
android ×2
new-operator ×2
difference ×1
if-statement ×1
immutability ×1
object ×1
string-pool ×1