What is the use of plus "+"
symbol in code?
Eg: (char) (myRandom.nextInt(26) **+** 'A')
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this communityIn this case, the + is moving to the next char in the alphabet from where you are.
The random part doesn't help explain, so this snippet may help.
char x = (char)(1 + 'A'); // returns B. Pos 1 + A (1) gives 2 => B
x = (char)(2 + 'A'); // returns C. Pos 2 + A (1) gives 3 => C
x = (char)(3 + 'A'); // returns D. Pos 3 + A (1) gives 6 => D
x = (char)(1 + 'A'); // returns B. Pos 1 + A (1) gives 2 => B
x = (char)(2 + 'B'); // returns D. Pos 2 + B (2) gives 4 => D
x = (char)(3 + 'C'); // returns F. Pos 3 + C (3) gives 6 => F
Your random statement is seeded with 26. So nextInt(26) will return a random number between 0 and 26. Whatever is returned by that is that moved on one position by the + 'A' part.
This particular line means that you take a random shift from 'A'
. Say if
myRandom.nextInt(26) == 1
then you will get 'B'