Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone using short and byte primitive types, in real apps?

I have been programming in Java since 2004, mostly enterprise and web applications. But I have never used short or byte, other than a toy program just to know how these types work. Even in a for loop of 100 times, we usually go with int. And I don't remember if I have ever came across any code which made use of byte or short, other than some public APIs and frameworks.

Yes I know, you can use a short or byte to save memory in large arrays, in situations where the memory savings actually matters. Does anyone care to practice that? Or its just something in the books.

[Edited]

Using byte arrays for network programming and socket communication is a quite common usage. Thanks, Darren, to point that out. Now how about short? Ryan, gave an excellent example. Thanks, Ryan.

like image 511
Adeel Ansari Avatar asked Dec 31 '08 03:12

Adeel Ansari


2 Answers

I use byte a lot. Usually in the form of byte arrays or ByteBuffer, for network communications of binary data.

I rarely use float or double, and I don't think I've ever used short.

like image 104
Darron Avatar answered Oct 23 '22 11:10

Darron


Keep in mind that Java is also used on mobile devices, where memory is much more limited.

like image 24
Ryan Fox Avatar answered Oct 23 '22 12:10

Ryan Fox