• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

R&H questions

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,

plz help me with this
correct me if iam wrong.

1. float f = new float[100];

line 1 : f[99]=0;

is line true or false

My answer : false

2. A (parent) -> B (Subclass of A ) -> c (subclass of B)

is B instanceof Object?
My answer : true

3. class compareeq
{ int i;

compareeq(int i)
{ this.i = i; }

public static void main(String a[])
{
compareeq c1 = new compareeq(50);

compareeq c2= new compareeq(50);

Integer i1 = new Integer(10);

Integer i2 = new Integer(10);


if(i1.equals(i2))
System.out.println("Integer true");

if(c1.equals(c2))
System.out.println("YES");
}
}

Answer: Integer true.
Why just integer ? shouldnt be both true?

thanks in advance
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavin,

Ur answer is correct for the 1st & 2nd question.But for 3rd it is not correct and here given the explanation for the same.




3. class compareeq
{ int i;

compareeq(int i)
{ this.i = i; }

public static void main(String a[])
{
compareeq c1 = new compareeq(50);

compareeq c2= new compareeq(50);

Integer i1 = new Integer(10);

Integer i2 = new Integer(10);


if(i1.equals(i2))
System.out.println("Integer true");

if(c1.equals(c2))
System.out.println("YES");
}
}

Answer: Integer true.
Why just integer ? shouldnt be both true?



This is because all wrapper classes have overridden equals method & when it is used by wrapper objects the values contained in those objects will be checked for equality.Where as compareeq is ur own defined class.Unless you override equals method(inherited from object class) only reference equality will be checked.Since both references c1 & c2 refer to two different objects if condition evaluates to false.

Hope this helps!

Regards,
Priya.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't see how the above line even go thru compiler. You are trying to assign an array object reference to primitive variable.



Even the above statement won't get thru compiler. B is class and not a reference, but the left hand operand of instanceof operator should be a reference rather a class name.
 
kavin kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks priya.i got it.

Babu
For class B i meant the reference only.thanks for all ur response
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavin & Vneeth,

I assume the code given is not the exact one.For the first one,as Vneeth said if u try to compile this code it wont even get thru.coz float array cant be assigned to a float variable.I guess it must be a typo error.
For the second one it is not the actual code he is juz asking the question thru hierarchy diagram.

Regards,
Priya.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really..
this question was ood one..
felt like something is missing some where!!! keep up guys!!!
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more point related to overriding equals method.

Donot forget to override the hashCode method as well when you override the equals method for your class.

The objects with equals overridden but not overriding hashCode might work well with List or Set but will exhibit odd behavior when used as key in a HashMap.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic