Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

from Khalid Mughal review questions

 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Arie Prastowo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic