java 中的 in 值前有一个 -- 是什么意思?还有什么是 StringBuilder

What does it mean when there is a -- before an in value in java? Also what is a StringBuilder

我正在尝试完成 Java 课程介绍中的作业,但我有一些问题。首先,在 int 值前面有一个 -- 是什么意思?还有什么是 String Builder?我得到了一些帮助,但想了解我在代码中使用的是什么。谢谢

一个值前面的--只是表示从中减去1。同样,++在一个值前面表示加1。

If you write ++ before the number it is called prefix operator and if after then its post fix preFix: ++a will increase the value before using it, will first increase and then use it. postFix a++ will first use it and then use it, for later use you will get the incremented value.

我的经验主要是使用 C# 而不是 Java,但在 C# 中,字符串无法更改,当您连接两个字符串(如 "hello" + "world" 时,您不会更改任何一个字符串,而是创建一个新字符串,然后旧的两个仍然存在。如果您需要多次(数十次或数百次)执行此操作,它会占用大量内存。 StringBuilder 允许您在构建字符串时通过将字符附加到同一内存块来节省内存,然后您可以将结果转换为普通字符串以传递给其他函数。

-- 是预减

Java StringBuilder class 用于创建可变(可修改)字符串。 字符串是不可变的,即字符串一旦创建就不能更改,每次更改值时都会创建新字符串。

但是对于可变字符串的 StringBuilder,字符串可以改变。