- 1

why string!! in spite of char?

In this code we are using single letters, so we should use char but why did thay use string? public class Program { public static void main(String[] args) { String[ ] myNames = { "A", "B", "C", "D"}; System.out.println(myNames[2]); } }

21st Apr 2018, 9:21 AM
RH Tasin
RH Tasin - avatar
2 Answers
+ 11
a string can be a single character as well furthermore, if you were to use char[ ] myNames = ..... you would have needed to declare the array elements with a single quatation marks: char[ ] myNames = {'A', 'B', 'C', 'D'}; and for next time, please make an actual code and post it instead of just pasting it here.... it would allow others to help you more easily
21st Apr 2018, 10:25 AM
Burey
Burey - avatar
+ 3
Maybe they shouldn't, maybe they want multiple characters sometimes. Who knows without the actual code.
21st Apr 2018, 12:07 PM
Timon Paßlick