Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compare long

Posted on 2004-08-03
29
Medium Priority
?
492 Views
Last Modified: 2012-08-13
Hi!

seems like I can't do:

long a= 9876543435351;
long b =1232134343344;

if (a > b) {
 // do something
}

found that I had to use compareTo for long, but how?

regards
0
Comment
Question by:mamruoc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 6
  • +2
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703201
No - you have to use compareTo for Long, not long

if (a.longValue() > b.longValue()) {
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703217
(Where 'a' is defined as Long). The alternative is

if (a.compareTo(b) > 0) {
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703226
... and if they *are* defined as long, then your posted code will work
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mamruoc
ID: 11703227
Error                  Cannot invoke longValue() on the primitive type long

hm....
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11703231
a > b is correct for all simple number-carrying variables
compareTo( ) and equals( ) are for reference variables, when comparing (part of) the object's contents.
;JOOP!
0
 

Author Comment

by:mamruoc
ID: 11703238
I'm doing:

long time = 0 ;
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */

if((dateObject.getTime() < time)) {

}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703239
>>seems like I can't do:

>>long a= 9876543435351;
>>long b =1232134343344;

Right, you can't since both values are too large to fit in a long


0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703273
>>I 'm doing:

>>long time = 0 ;
>>Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */

>>if((dateObject.getTime() < time)) {

>>}

That's OK. But I think you mean

if((dateObject.getTime() > time))
0
 

Author Comment

by:mamruoc
ID: 11703275
I'm doing:

long time = 0 ;
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */

if((dateObject.getTime() < time)) {

}

I'm trying to make a timeout function using linuxtimestamp....

time will be fill with timestamp and added, let's say 2000, for 2 sec timeout....
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11703282
zzynx, you are confusing int and long: long can contain as much as 2^63
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703292
>>I'm doing:

That's fine

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703301
Please post the code that's failing
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703303
>> Error               Cannot invoke longValue() on the primitive type long
longValue() must be invoked on a Long (capital L) not on a long (small l)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703326
>> zzynx, you are confusing int and long: long can contain as much as 2^63
Did you try to compile

>>long a= 9876543435351;
>>long b =1232134343344;

It gives me
integer number too large: 9876543435351
integer number too large: 1232134343344
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11703328
>>I'm trying to make a timeout function using linuxtimestamp....

long x = 2000L;
long start = System.currentTimeMillis();

...

long duration =  System.currentTimeMillis() - start;
if (duration > x) {
      // time out
}


0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703336
I see: that should've been

long a= 9876543435351L;
long b =1232134343344L;

(remark the L at the end)
0
 

Author Comment

by:mamruoc
ID: 11703337
public void run() {
      if (DEBUG) System.out.println("UDP AcceptThread accepting messages...");
          try {
            acceptSocket = new DatagramSocket(UDP_IN_PORT);
            byte[] buffer = new byte[defaultBufferLength];
            int counter=0;
            while (true) {
                  incoming = new DatagramPacket(buffer, buffer.length);
                  Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
                  try {
                        if(recording || !playing || acceptingNewStream) { //Save to file, if system busy playback or sending.
                              if(time == 0) { // timeout == 0 for initiate a new stream
                                                      in_baos = new ByteArrayOutputStream();
                                                      time = 1;
                                                }
                                                if((dateObject.getTime() < time) || (time == 1)) {      //time < timeout || timeout == 1
                                                      acceptSocket.receive(incoming); /* Accepts incoming udp stream */
                                                      try {
                                                            in_baos.write(incoming.getData());
                                                            if(incoming.getLength() > 0) {
                                                                  if(time == 1) {
                                                                        System.out.print("Accepting new stream");
                                                                  }
                                                                  time = (dateObject.getTime() + (timeOut*1000));
                                                                  acceptingNewStream = true;
                                                                  System.out.print(".");
                                                            }
                                                      }
                                                      catch (IOException e) {
                                                            System.out.println( "Writing to stream: " + e.getMessage());
                                                      }
                                                }
                                                else {
                                                      System.out.println("Something went wrong: ");
                                                }
                                          }
                                          else { // System not busy
                                                ///TODO: Fix playing when not recording or playback.
                                          }
                                    }
                                    catch (IOException e) {
                                          System.err.println(e);
                                    }
                              } // end while
                        }  // end try
                        catch (SocketException se) {
                              System.err.println(se);
                        }  // end catch
                  } // end run

I got a ticker thread running beside too, to timeout the whole system 'cause incoming = new DatagramPacket(buffer, buffer.length); seems to hang if there is no new incoming....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703339
>>long a= 9876543435351;

should be

long a= 9876543435351L;
0
 
LVL 35

Expert Comment

by:girionis
ID: 11703355
Try:

long a = 9876543435351l;
long b = 1232134343344l;

if (a > b) {
 // do something
}

(Notice the "l" at the end of the long numbers)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703363
Said that :°)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11703364
>>long a= 9876543435351;
>>long b =1232134343344;

>Right, you can't since both values are too large to fit in a long

Nope they are not. They can easily fit in a long.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703382
Yes, I know.
cf. previous comment.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11703432
As the title suggests, this thread is long.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703441
Shouldn't this

        time = (dateObject.getTime() + (timeOut*1000));

be

        time = ((new Date()).getTime() + (timeOut*1000));
0
 

Author Comment

by:mamruoc
ID: 11703447
Neede acutally long,

but the suggestion about timout function was just what I needed.. :D

thanks alot everybody
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703456
Oh, no longer interested. OK.
0
 

Author Comment

by:mamruoc
ID: 11703482
Hi,

no hard feelings.. It's still intresting....

But CEHJ gave me a solution I needed....

:S
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703485
8-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11703508
>>no hard feelings
No offence meant.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question