Prev: JDK 1.5.0_21 released
Next: LWUIT and J2ME
From: Lew on 9 Sep 2009 13:35 grz01 wrote: > 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. > Sometimes when working with classes like Long and Integer, a visit to their respective Javadocs might find methods in both that could make them commensurate, like, oh, say, longValue(). The Javadocs are a very potent resource. -- Lew
From: Lew on 9 Sep 2009 13:37 Lew wrote: >> assertEquals( i.longValue(), l.longValue() ); > >> Why make things complicated? > grz01 <gr...(a)spray.se> wrote: > Thanks Lew, but still 2 different conversions in the code :) > > I would like only one, if possible :) ??? What do you imagine the difficulty to be with the two "conversions" that you imagine happening here? -- Lew
From: grz01 on 9 Sep 2009 14:02 > What do you imagine the difficulty to be with the two "conversions" > that you imagine happening here? > > -- > Lew Hi Lew, Just like with assertEquals(good.grief(), (Integer)oh.dear().intValue()) it's not a "difficulty", admittedly. That works, too. I just think it would be neater with a single step, esp. when this type of code repeats many hundred times in our test-cases. But if someone can tell me, with certainty "NO, IT'S NOT POSSIBLE IN JAVA!" (to convert Long -> Integer in one step) then I would be fine with that answer, too :) and stop looking... :) / grz01
From: Lew on 9 Sep 2009 14:07 Lew wrote: >> assertEquals( i.longValue(), l.longValue() ); > >> Why make things complicated? > grz01 wrote: > Thanks Lew, but still 2 different conversions in the code :) > > I would like only one, if possible :) > One of your "conversions" is the call to Long#longValue(), which does no conversion whatsoever. It extracts the underlying long value of the Long, something that equals() does anyway, so there's no escaping it. No additional cost there. The other "conversion" is actually two operations: the inescapable extraction of the underlying Integer int value, and the widening conversion to long. So that's two extractions, one widening conversion and a comparison with my suggestion. What you want is to convert, say, an Integer to a Long, not actually possible, followed by two extractions and a comparison. Even if conversion of Integer to Long were possible, no doubt it would be more expensive than the widening of an int to long. So do you prefer two extractions, an impossible class conversion and a comparison as you ask, or two extractions, a simple widening primitive conversion and a comparison, as I suggest? -- Lew
From: Lew on 9 Sep 2009 14:10
grz01 wrote: > But if someone can tell me, with certainty "NO, IT'S NOT POSSIBLE IN > JAVA!" > (to convert Long -> Integer in one step) > then I would be fine with that answer, too :) > and stop looking... :) The Javadocs tell you that, and that's the most authoritative "someone" there is for this question. Read the Javadocs. The Javadocs are a potent resource. Don't ignore the Javadocs. The answer to that question is yes, the conversion of Long to Integer, which, BTW, is the wrong direction to convert as you've already been told, as you can tell by reading the Javadocs. So I suggest that you read the Javadocs. -- Lew |