0

True purpose of char

What is the purpose for 'char' if I can use a 'string'? Could someone please show me code that it would be better to use 'char' over 'string'?

6th May 2018, 4:03 AM
Wildman Fizat
Wildman Fizat - avatar
2 Respuestas
+ 6
Note that char is a primitive data type in Java, whereas String is an object. String has several useful methods to manipulate strings, char does not own any of them. On the other hand, Strings are immutable, every change you make in them will produce a new object (AFAIK). Since they are objects they also consume more memory. And its location in memory is also different. Use char when you want to represent a single character. There are also practices such as storing passwords in char arrays to facilitate their handling and prevent them from being in memory all the time.
6th May 2018, 4:30 AM
Mickel
Mickel - avatar
+ 3
for(char i='a';i<'k';i++){} //that,you cannot do with strings
6th May 2018, 4:20 AM
᠌᠌Code X
᠌᠌Code X - avatar