Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Integer / Long comparison

Reply
Thread Tools

Integer / Long comparison

 
 
grz01
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      09-09-2009
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/>
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-09-2009
On Sep 9, 1:04*pm, Daniel Pitts
<(E-Mail Removed)> 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
 
Reply With Quote
 
grz01
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
grz01
Guest
Posts: n/a
 
      09-09-2009
> * 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-09-2009
Lew wrote:
>> * assertEquals( i.longValue(), l.longValue() );

>
>> Why make things complicated?

>

grz01 <(E-Mail Removed)> 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
 
Reply With Quote
 
grz01
Guest
Posts: n/a
 
      09-09-2009
> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-09-2009
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

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Size of Integer , long Integer, Long double Suresh V C++ 5 07-05-2010 08:09 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
How to printf a long long data type integer? music4 C Programming 3 01-05-2004 03:29 AM
Re: long long integer and double precision number cyberdude C Programming 2 06-25-2003 07:54 PM
Re: long long integer and double precision number Dan Pop C Programming 0 06-24-2003 12:11 PM



Advertisments