Prev: JDK 1.5.0_21 released
Next: LWUIT and J2ME
From: grz01 on 9 Sep 2009 12:28 Hi In JUnit I want to compare an Integer with a Long, as 2 Integers -- say like this: Integer i = ...; Long l = ...; assertEquals(i,l); // assertEquals is ambigous, doesnt work Now, if I try this instead: Integer i = ...; Long l = ...; assertEquals(i,l.intValue); assertEquals() still complains it doesnt know whether I mean assertEquals(int, int) or assertEquals(Integer, Integer) Thus, if I write Integer i = ...; Long l = ...; assertEquals(i,(Integer)l.intValue); it does what I want, but seems a bit verbose with two "casts". So just wondered, Is there a direct way to go from Long to Integer, without the intermediate int-step ? / grz01
From: Daniel Pitts on 9 Sep 2009 13:04 grz01 wrote: > Hi > In JUnit I want to compare an Integer with a Long, as 2 Integers -- > say like this: > > Integer i = ...; > Long l = ...; > assertEquals(i,l); // assertEquals is ambigous, doesnt work This will always fail anyway, because they are different classes. > > Now, if I try this instead: > > Integer i = ...; > Long l = ...; > assertEquals(i,l.intValue); there is no such field called intValue. > > assertEquals() still complains it doesnt know whether I mean > assertEquals(int, int) or > assertEquals(Integer, Integer) Assuming you mean't intValue() above. > > Thus, if I write > > Integer i = ...; > Long l = ...; > assertEquals(i,(Integer)l.intValue); > > it does what I want, but seems a bit verbose with two "casts". intValue() isn't a cast. Either way, you're better off casting the i to a long (because there are longs that can't be represented as an integer, and your equals case might miss that) > > So just wondered, Is there a direct way to go from Long to Integer, > without the intermediate int-step ? If possible, use int and long to start with, rather than Integer and Long. Then you can do assertEquals((long)i, l); -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Lew on 9 Sep 2009 13:30 On Sep 9, 1:04 pm, Daniel Pitts <newsgroup.spamfil...(a)virtualinfinity.net> wrote: > grz01 wrote: > > Hi > > In JUnit I want to compare an Integer with a Long, as 2 Integers -- > > say like this: > > > Integer i = ...; > > Long l = ...; > > assertEquals(i,l); // assertEquals is ambigous, doesnt work > > This will always fail anyway, because they are different classes. > > > Now, if I try this instead: > > > Integer i = ...; > > Long l = ...; > > assertEquals(i,l.intValue); > > there is no such field called intValue. > > > assertEquals() still complains it doesnt know whether I mean > > assertEquals(int, int) or > > assertEquals(Integer, Integer) > > Assuming you mean't intValue() above. > > > Thus, if I write > > > Integer i = ...; > > Long l = ...; > > assertEquals(i,(Integer)l.intValue); > > > it does what I want, but seems a bit verbose with two "casts". > > intValue() isn't a cast. Either way, you're better off casting the i to > a long (because there are longs that can't be represented as an integer, > and your equals case might miss that) > > > So just wondered, Is there a direct way to go from Long to Integer, > > without the intermediate int-step ? > > If possible, use int and long to start with, rather than Integer and Long.. > > Then you can do > assertEquals((long)i, l); or assertEquals( i.longValue(), l.longValue() ); Why make things complicated? -- Lew
From: grz01 on 9 Sep 2009 13:31 Hi Daniel, Yes, of course, I meant the intValue() method. This was a simplified example I wrote only to demonstrate the problem, but in the actual code I have here, the objects and types come from third-party frameworks we are using, so I cant use your advice re. int/ long, etc. The Integer and Long types are what they are and cannot be changed. In my code it's actually something like assertEquals(good.grief(), (Integer)oh.dear().intValue()) which doesnt look very nice... So, I am still interested if somebody can rewrite that line of code in a nicer way, without the casting/conversion in two separate steps. Instead of: Long -> int -> Integer, I was hoping you could do it in one direct step: Long -> Integer. Anyone? / grz01
From: grz01 on 9 Sep 2009 13:34
> assertEquals( i.longValue(), l.longValue() ); > > Why make things complicated? > > -- > Lew Thanks Lew, but still 2 different conversions in the code :) I would like only one, if possible :) |