I am trying to finish an assignment in my intro to Java course, and I have some questions. First off, what does it mean when there is a -- in FRONT of an int value? Also what is a String Builder? I had some help but want to understand what it is I'm using in the code. Thanks.
-
3Hi, welcome to StackOverflow. These are both good questions, but they are very unrelated to each other. Please post separately. – JamesFaix Apr 03 '18 at 18:26
3 Answers
The --
in front of a value simply means subtract 1 from it. Similarly, ++
in front of a value means add 1 to it.
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.

- 175
- 9
-
1Note also, `n++` is not the same as `++n` https://stackoverflow.com/questions/7031326/what-is-the-difference-between-prefix-and-postfix-operators – JamesFaix Apr 03 '18 at 18:29
My experience is mostly with C# not Java, but in C# strings cannot be changed, when you concatenate two strings like "hello" + "world"
you do not change either string, you create a new one and the old two still exist. If you need to do this many times (dozens or hundreds) it can use a lot of memory. A StringBuilder
allows you to conserve memory by appending characters to the same block of memory while you are building your string, and then you can turn the result into a normal string for passing around to other functions.

- 8,050
- 9
- 37
- 73
-- is a predecrement
Java StringBuilder class is used to create mutable (modifiable) string. A String is immutable i.e string cannot be changed once created and everytime a value is change it create new string.
But in case of StringBuilder which is mutable string can change.

- 61
- 3