String text1 = "check";
char c[] = Arrays.sort(text1.toCharArray());
Output:
error: incompatible types
char c[] = Arrays.sort(text1.toCharArray());
required: char[]
found: void
1 error
Why does it not work that way?
The Arrays.sort() method has a return type of void, meaning it does not return an object/primitive, so you can't really assign the absent return value to a char[]. The array will be sorted through the reference to it (arrays are objects) passed to the method.
BTW, the same applies for Collections.sort()
See the documentation
FIX
String text1 = "check";
char c[] = text.toCharArray();
Arrays.sort(c);
Note that Arrays.sort does not return type.
Source code is as follows:
public static void sort(char[] a) {
DualPivotQuicksort.sort(a);
}
If you want to sort the char array You can use the following way:
String text1 = "hello";
char c[] = text1.toCharArray();
//Sort char array
Arrays.sort(c);
//Print [e,h,l,l,o]
System.out.println(Arrays.toString(c));
sortis a destructive void method.