0

Why do I need to add double quote (" " +) to print first (nptel[1]) and last element (nptel[nptel.length-1]) of a char array.

char nptel [] = {'J','A','V','A','N','P','T','E','L'};
System.out.println(" " + nptel[1] + nptel[nptel.length-1]);
Peter Csala
  • 17,736
  • 16
  • 35
  • 75

1 Answers1

2

In java the addition of two chars is interpreted as int following their ascii values. It's equivalent to (int)'a' + (int)'b'

By adding the " " + in front of the addition, you implicitly cast the chars into strings. So it's equivalent to Character.toString('a') + Character.toString('b').

Julien Sorin
  • 703
  • 2
  • 12