0

Beginner here. Need some deeper insight. Four integer types: byte, short, int, and long. So, apart from their range, what should I know about their behavior.

Difference between int i = 1000 ; and long l = 1000 ;

By differences, I mean, the space allocated in the memory, speed when using them etc. Any thing, that I must keep in mind while designing an algorithm in real life.

In one line, why use int if long can do int and more than int.

Searched on internet but didn't find a precise answer.

Charan
  • 1,051
  • 3
  • 12
  • 32

1 Answers1

0

long takes double the size of int, at least in Java and most C++ platforms (in C++ you'd actually have to define the length of long which depending on platform might be 32 or 64 bit, that's why there is a long long in C++).

Besides memory usage in general this might also affect processing times because more data might have to be sent over the bus resp. you could send 2 ints in parallel on a 64-bit machine.

But most likely you're not going to have to take all that into account since most systems are not that tight on resources, so choose whatever you find appropriate.

Edit:

If you're operating on huge datasets it might save you some space to use int over long but in those cases it might actually be wiser to design the algorithm in a way that it operates on only the data which is immediately necessary and release it as fast as possible, i.e. don't keep everything in memory.

Thomas
  • 87,414
  • 12
  • 119
  • 157