aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes from Khalid Mughal review questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "from Khalid Mughal review questions" Watch "from Khalid Mughal review questions" New topic
Author

from Khalid Mughal review questions

vinal sen
Ranch Hand

Joined: Dec 13, 2007
Posts: 37
Recently I have joined this forum.Could someone explain to me what #1 means.

how is the testone,which does not implement face, cast to face?


-----------------

Array#1



Array#2

int[]a = new int[]{1};
int[]b = new int[]{1};
System.out.println(a.equals(b)); //return false

Also to me,both the arrays look the same.but the result are different why?
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

first one is good question, At run time it should be fine as the actual Object is tester which does implement face, but point is why compiler support it at the compile time, as there is no relationship between test and face.
Compiler allows casting with in the same hirerachy tree.
I thinked a lot but no result from my side, lets hope someone else will find a proper explanation : why test and face are coming under same hirerachy tree ?

regarding your second question :

Arrays.equals(a, b) --> checks the content of arrays, as both have same content so result is true.

a.equals(b) --> this check whether both a and b refers to same Object on heap or not.Arrays are also object and you have created two different array for a and b, so answer should be false.
[ April 21, 2008: Message edited by: Sunny Jain ]

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
import java.lang.*;
public class Arraydemo
{
public static void main(String[] args)
{

int a[]= {1};
int b[] = new int[] {1};
System.out.println(a.equals(b));

}
}

output - false.
Can now someone explain why o/p is false.
Also i am not able to find Arrays.equals() method.
Please help...


Thanks<br />Dinesh
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

For Arrays.equals see the following link :
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html

and Regarding a.equals(b), Why do you think that it should be "true" and what do you understand by equals method ?

Let me know your understanding then I will explain as per according to that.
sakthi karthik
Greenhorn

Joined: Apr 19, 2008
Posts: 28
hi dinesh

here output is false because equals() method is comparing whether the object a and object b are same. since they are different objects its returning false.

if you import utils to your program you will get Arrays.equals(a,b), here the value stored in a and b are compared

if you are using K&B book, just go throough generics and collections once again.

good luck

regards
sakthi karthik
Arie Prastowo
Greenhorn

Joined: Apr 20, 2008
Posts: 22
I have try several other cases and found the following:
1. when you try to cast a non-final class to an interface, it will compile whether or not they are in same hierarchy. But it will throws ClassCastException when it's not in the same hierarchy.
2. the above point won't work on final Class.

Correct me if I'm wrong



SCJP 5.0<br />Use the power of mind
Arie Prastowo
Greenhorn

Joined: Apr 20, 2008
Posts: 22
Come from additional investigation that when your code:


the compiler expect that one of Test subclasses might implement face and it just compile fine. But that's won't work on final classes, since it will never has subclasses
vinal sen
Ranch Hand

Joined: Dec 13, 2007
Posts: 37
From Arie:
[QUOTEI have try several other cases and found the following:
1. when you try to cast a non-final class to an interface, it will compile whether or not they are in same hierarchy. But it will throws ClassCastException when it's not in the same hierarchy.
2. the above point won't work on final Class.


Thanks to all of you.The above statements works.

1.Interface need not be in the same heirarchy as the class.It will compile and throw classcastexception at runtime

2.But if the object created belongs to a class that implements an interface,then the program is running fine.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: from Khalid Mughal review questions