# ID:8 SCJP Question of the day !!!

Sahil Kapoor

Ranch Hand

Posts: 316

posted 5 years ago

Here goes the 8th question testing Collections and Wrapper classes !!!

Refernce to rules :- See ID:1 SCJP Question of the day !!!

http://www.coderanch.com/t/498524/java-programmer-SCJP/certification/ID-SCJP-day-Wednesday-June

Refernce to rules :- See ID:1 SCJP Question of the day !!!

http://www.coderanch.com/t/498524/java-programmer-SCJP/certification/ID-SCJP-day-Wednesday-June

SCJP 6.0 96%

(Connecting the Dots ....)

Unmesh Chowdhury

Ranch Hand

Posts: 45

posted 5 years ago

The second System.out.println() will print: 65392

Actually, the Character class stores its value in an (private final char value) instance field. And it implements the compareTo(T) method of Comparable<T> interface as follows:

Character c1 = (char)-1;

Character c2 = 143;

Here,

the value of the c1 will be evaluated as 65535 (value of the lower two bytes).

the value of the c2 will be evaluated as 143.

Hence,

System.out.println(c1.compareTo(c2)) will print: 65392 (65535 -143).

The others System.out.println() will print result as expected, that is, in the ascending order.

Actually, the Character class stores its value in an (private final char value) instance field. And it implements the compareTo(T) method of Comparable<T> interface as follows:

Character c1 = (char)-1;

Character c2 = 143;

Here,

the value of the c1 will be evaluated as 65535 (value of the lower two bytes).

the value of the c2 will be evaluated as 143.

Hence,

System.out.println(c1.compareTo(c2)) will print: 65392 (65535 -143).

The others System.out.println() will print result as expected, that is, in the ascending order.

M.Sc. in CS, OCPJP6 93%

Sahil Kapoor

Ranch Hand

Posts: 316

Unmesh Chowdhury

Ranch Hand

Posts: 45

posted 5 years ago

I want to state that every sorting in Java collection framework is done in natural order, that is, in the ascending order. The sorting in Java is accomplished by two interfaces which are Comparable<T> and Comparator<T>. Comparable<T> lets us sort the collection based on only one item whereas the Comparator<T> lets us sort the collection based on multiple items.

Now, if any class wants to implement the Comparable<T> interface then it has to implement the following method. And when a class implements the Comparable<T> interface then the objects of that class will be mutually comparable with each other.

int compareTo(T o)

See, the return type of the above method is int, which has restricted the method to return a value which is within the following set of values: negative integer, zero, or positive integer. And based on the return value of this method after compare two mutually comparable objects, any sorting algorithm sorts the compared objects in natural order, that is, in ascending order. To clarify this, suppose the following Person class implements Comparable<T> interface:

class Person implements Comparable<Person> {

String firstName;

String lastName;

Person(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

public int compareTo(Person p) {

return this.firstName.compareTo(p.firstName);

}

}

Person unmesh = new Person(“Unmesh”, “Chowdhury”);

Person khokon = new Person(“Khokon”, “Chowdhury”);

Now, if any sorting algorithm calls unmesh.compareTo(khokon) then it will get a positive integer and sorts these two objects as follows: khokon, unmesh, what is in the ascending order according to the returned value.

Let me change the implementation of the compareTo(Person) method of the Person class as follows:

public int compareTo(Person p) {

return p.firstName.compareTo(this.firstName);

}

Now, suppose again the previous sorting algorithm calls unmesh.compareTo(khokon) then it will get a negative integer and sorts these two objects as follows: unmesh, khokon, what is in the ascending order according to the returned value.

First version of Person class:

Second version of Person class:

Now, Mr. Sahil Kapoor, come into your case. The output of the collection after sorting must be in the natural order or ascending order which is based on the implementation of the compareTo(T) method of the specific collection type.

In the case of list of Boolean; the Boolean class implementation of the compareTo(Boolean) method can be explained by the following example:

Suppose there are two Boolean objects,

Boolean b1 = new Boolean(true);

Boolean b2 = new Boolean(false);

• b1.compareTo(b2) will return positive integer

• b1.compareTo(b1) will return zero

• b2.compareTo(b2) will return zero

• b2.compareTo(b1) will return negative integer

Thus, in your case the output will be (ascending order):

false, false, true, true

In the case of list of Float; the Float class implements the compareTo(Float) in such a way that the sorting result will be in the following natural order or ascending order:

negative infinity

negative finite nonzero values

negative zero

positive zero

positive finite nonzero values

positive infinity

not a number (NaN)

Thus, in your case the output will be (ascending order):

-Infinity, -0.0, 0.0, Infinity, NaN

Now, if any class wants to implement the Comparable<T> interface then it has to implement the following method. And when a class implements the Comparable<T> interface then the objects of that class will be mutually comparable with each other.

int compareTo(T o)

See, the return type of the above method is int, which has restricted the method to return a value which is within the following set of values: negative integer, zero, or positive integer. And based on the return value of this method after compare two mutually comparable objects, any sorting algorithm sorts the compared objects in natural order, that is, in ascending order. To clarify this, suppose the following Person class implements Comparable<T> interface:

class Person implements Comparable<Person> {

String firstName;

String lastName;

Person(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

public int compareTo(Person p) {

return this.firstName.compareTo(p.firstName);

}

}

Person unmesh = new Person(“Unmesh”, “Chowdhury”);

Person khokon = new Person(“Khokon”, “Chowdhury”);

Now, if any sorting algorithm calls unmesh.compareTo(khokon) then it will get a positive integer and sorts these two objects as follows: khokon, unmesh, what is in the ascending order according to the returned value.

Let me change the implementation of the compareTo(Person) method of the Person class as follows:

public int compareTo(Person p) {

return p.firstName.compareTo(this.firstName);

}

Now, suppose again the previous sorting algorithm calls unmesh.compareTo(khokon) then it will get a negative integer and sorts these two objects as follows: unmesh, khokon, what is in the ascending order according to the returned value.

First version of Person class:

Second version of Person class:

Now, Mr. Sahil Kapoor, come into your case. The output of the collection after sorting must be in the natural order or ascending order which is based on the implementation of the compareTo(T) method of the specific collection type.

In the case of list of Boolean; the Boolean class implementation of the compareTo(Boolean) method can be explained by the following example:

Suppose there are two Boolean objects,

Boolean b1 = new Boolean(true);

Boolean b2 = new Boolean(false);

• b1.compareTo(b2) will return positive integer

• b1.compareTo(b1) will return zero

• b2.compareTo(b2) will return zero

• b2.compareTo(b1) will return negative integer

Thus, in your case the output will be (ascending order):

false, false, true, true

In the case of list of Float; the Float class implements the compareTo(Float) in such a way that the sorting result will be in the following natural order or ascending order:

negative infinity

negative finite nonzero values

negative zero

positive zero

positive finite nonzero values

positive infinity

not a number (NaN)

Thus, in your case the output will be (ascending order):

-Infinity, -0.0, 0.0, Infinity, NaN

M.Sc. in CS, OCPJP6 93%

It is sorta covered in the JavaRanch Style Guide. |