2
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?

1
  • 2
    sort is a destructive void method. Commented Nov 29, 2013 at 13:29

4 Answers 4

13

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);
Sign up to request clarification or add additional context in comments.

Comments

3

Arrays.sort(); returns void you cannot assign it to an char array i.e char[]

Comments

3

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));

Comments

2

Arrays.sort() returns void not char[]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.