Java 在运行时更改字符串
我想在运行时对字符串进行一些更改。在下面的代码中,我试图改变int变量Java 在运行时更改字符串,java,string,Java,String,我想在运行时对字符串进行一些更改。在下面的代码中,我试图改变int变量count,这样字符串s也会改变 我想结果是这样的: You have invited 0 users. You have invited 5 users. System.out.println(getString()); // prints: You have invited 0 users. this.count = 5; System.out.println(getString()); // prints: You h
count
,这样字符串s也会改变
我想结果是这样的:
You have invited 0 users.
You have invited 5 users.
System.out.println(getString()); // prints: You have invited 0 users.
this.count = 5;
System.out.println(getString()); // prints: You have invited 5 users.
但在这两种情况下,都有0:
You have invited 0 users.
You have invited 0 users.
你能解释一下为什么我的想法不起作用,我该怎么做才能使它起作用
static int count;
static String s = "You have invited " + count + " users.";
public static void main(String[] args) {
System.out.println(s);
count = 5;
System.out.println(s);
}
字符串是不可变的,所以当您使用
count=5
事实上,您没有更改s
。要进行更改,您必须使用:
count = 5;
s = "You have invited " + count + " users.";
System.out.println(s);
字符串是不可变的,所以当您使用
count=5
事实上,您没有更改s
。要进行更改,您必须使用:
count = 5;
s = "You have invited " + count + " users.";
System.out.println(s);
您可以创建一个访问器方法,如
getter
,并将count
作为一个实例变量:
private int count = 0;
private String getString(){
return "You have invited " + this.count + " users.";
}
可以这样称呼:
You have invited 0 users.
You have invited 5 users.
System.out.println(getString()); // prints: You have invited 0 users.
this.count = 5;
System.out.println(getString()); // prints: You have invited 5 users.
这是因为正如@YCF_Ls answer
中所说,字符串是不可变的。这意味着一旦创建了字符串。这是无法改变的。唯一的方法是用一个新字符串覆盖它(它仍然不会更改值,但只会将变量s
的引用更改为新字符串)您可以创建一个访问器方法,如getter
,并将count
作为一个实例变量:
private int count = 0;
private String getString(){
return "You have invited " + this.count + " users.";
}
可以这样称呼:
You have invited 0 users.
You have invited 5 users.
System.out.println(getString()); // prints: You have invited 0 users.
this.count = 5;
System.out.println(getString()); // prints: You have invited 5 users.
这是因为正如@YCF_Ls answer中所说,字符串是不可变的。这意味着一旦创建了字符串。这是无法改变的。唯一的方法是用新字符串覆盖它(它仍然不会更改值,但只会将变量s
的引用更改为新字符串)字符串是不可变的。每次对现有字符串进行任何更改时,都需要重新分配该字符串,以反映更改。在您的情况下,您已经更改了count变量。但是,更改后,需要将字符串重新分配给变量s
。或者,简单地说,您需要使用count
变量创建新字符串,并将其分配给某个变量,即在您的情况下,字符串是不可变的。每次对现有字符串进行任何更改时,都需要重新分配该字符串,以反映更改。在您的情况下,您已经更改了count变量。但是,更改后,需要将字符串重新分配给变量s
。或者,简单地说,您需要使用count
变量创建新字符串,并将其分配给某个变量,即在您的情况下,它是s
,正如其他人所说,您对count
的修改不会改变s
的值
您可以做的是使用:
正如其他人所说,您对count
的修改不会改变s
的值
您可以做的是使用:
虽然计数已更改,但s未更改,始终为“您已邀请0个用户”
1.intcount是一种基本的数据类型变量
2.即使count是引用类型,字符串也是不可变的,除非重新赋值
static int count;
static String s = "You have invited " + count + " users.";
public static void main(String[] args) {
System.out.println(s);
count = 5;
s = "You have invited " + count + " users.";
System.out.println(s);
}
虽然计数已更改,但s未更改,始终为“您已邀请0个用户”
1.intcount是一种基本的数据类型变量
2.即使count是引用类型,字符串也是不可变的,除非重新赋值
static int count;
static String s = "You have invited " + count + " users.";
public static void main(String[] args) {
System.out.println(s);
count = 5;
s = "You have invited " + count + " users.";
System.out.println(s);
}
现在我懒得解释事情,所以我会让其他人为此获得声誉积分。这里有一个便宜的解决方案:@kavita你应该发布一个答案谢谢@scarywombatt现在我懒得解释事情,所以我会让其他人为此获得声誉积分。这里有一个便宜的解决方案:@kavita你应该发布一个答案谢谢@ScaryWombatcount
不必是实例变量getString
可以将count
作为参数。这是一个很好的解决方案,尽管它没有回答“您能解释一下为什么我的想法不起作用…”啊,更好;)@当然是AshwaniDausodia。我的答案是只有一种方法可以用来解决老年退休金问题。您的方法也很有效。count
不必是实例变量getString
可以将count
作为参数。这是一个很好的解决方案,尽管它没有回答“您能解释一下为什么我的想法不起作用…”啊,更好;)@当然是AshwaniDausodia。我的答案是只有一种方法可以用来解决老年退休金问题。您的方式也很有效。@kavita每次都会对已经存在的字符串进行任何更改。您不能更改字符串。只能将变量的引用更改为新变量string@kavita每次,您都会对已经存在的字符串进行任何更改。您不能更改字符串。您只能将变量的引用更改为新字符串无论该字段是否为静态字段都不会影响OP将INGCOUNT定义为静态的问题,不应使用实例变量来解决问题,对吗?虽然此代码段可能是解决方案,但确实有助于提高帖子的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。字段是否为静态不会影响OP将ingCount定义为静态的问题,不应该是实例变量来解决问题,对吗?虽然这个代码片段可能是解决方案,但确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。