• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Code block help

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CODE BLOCK 1


This compiles, but the if condition at LINE x1 returns false all the time. I think it should return false,
only for the call at LINE x2


CODE BLOCK 2

This does not even compile, because of condition at LINE x1.

Can anyone let me know
1.The difference between Code blk 1 & 2
2. Why do Code blk 1, LINE x1 returns false
3. How to do write the code to make sure the comparision is done, only when both x & y are comparable.
 
Ganesh Pujar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me on this!
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know in what way you comile that code

You cant use x instanceof y (y must be a Type)

thats correct
x instanceof Comparable
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,

Both the codes that you have given will not compile and the reason as stated by Dantheman is, incorrect usage of 'instanceof' operator
The syntax is <refrence> instanceof <object type>
you cannot use instanceof operator for <reference> instanceof <reference>.


public class MapIt
{
public <T extends Comparable<T>> T findLarger(T z , T y1)
{
if(z instanceof Comparable)// LINE x1
{
if(z.compareTo(y1) > 0)
{return z;}
else
{return y1;}
}
else
{
System.out.println("Check your arguments, they are not related to each other");
return null;
}
}

public static void main(String args[])
{
MapIt t = new MapIt();
Object x = t.findLarger("123", "456");
System.out.println(x);
x = t.findLarger(123, 333);
System.out.println(x);

//This shoud throw exception
x = t.findLarger("123", 333);//LINE x2
System.out.println(x);

float fx=12;
float fy = 13;
x = t.findLarger(fx, fy);
System.out.println(x);

}
}

In the above code if you comment out line x2 this code will compile fine and give you the output.

Reason why line x2 doesnot compile is you are passing arguments of two
different type i.e findLarger(String, int), the compiler does not find method with this signature so it gives you error.

One more point that I would like to add here is, there is no need of instanceof test. Like you said you want comparison to be done only when x and y are comparable, your method's signature is findLarger(T, T) so obviously compiler will not allow any other type of parameter to be passed other than T, not even a supertype of T or subtype of T. It will STRICTLY require parameter of type T to be passed.

I hope this clears why your code was not compiliing and running.

Thanks,
Rancy
 
You can't expect to wield supreme executive power just because
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic