Difference between String and Character array in Java
Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties.
Differences between Strings and Character Arrays:
Strings | Character ArraysString refers to a sequence of characters represented as a single data type. | Character Array is a sequential collection of data type char. | Strings are immutable. | Character Arrays are mutable. | Built in functions like substring(), charAt() etc can be used on Strings. | No built in functions are provided in Java for operations on Character Arrays. | ‘+’ can be used to appended strings together to form a new string. | ‘+’ cannot be used to append two Character Arrays. | The charAt() method can be used to access characters at a particular index in a String. | The characters in a Character Array can be accessed normally like in any other language by using []. | Strings can be stored in any any manner in the memory. | Elements in Character Array are stored contiguously in increasing memory locations. | All Strings are stored in the String Constant Pool. | All Character Arrays are stored in the Heap. | Not preferred for storing passwords in Java. | Preferred for storing passwords in Java. | A String can be converted into Character Array by using the toCharArray() method of String class. | Eg: String s = “GEEKS”; char [] ch = s.toCharArray(); A Character Array can be converted into String by passing it into a String Constructor. | Eg: char[] a = {‘G’, ‘E’, ‘E’, ‘K’, ‘S’}; String A = new String(a); |
---|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.