views:

666

answers:

12

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.

+4  A: 

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

Ryan Fox
+3  A: 

I used 'byte' a lot, in C/C++ code implementing functionality like image compression (i.e. running a compression algorithm over each byte of a black-and-white bitmap), and processing binary network messages (by interpreting the bytes in the message).

However I have virtually never used 'float' or 'double'.

ChrisW
Ahan.. May be because of the domain you are working in. Similar might be true for me.
Adeel Ansari
+8  A: 

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.

Darron
A: 

I have used bytes when saving State while doing model checking. In that application the space savings are worth the extra work. Otherwise I never use them.

Milhous
+2  A: 

The primary usage I've seen for them is while processing data with an unknown structure or even no real structure. Network programming is an example of the former (whoever is sending the data knows what it means but you might not), something like image compression of 256-color (or grayscale) images is an example of the latter.

Off the top of my head grep comes to mind as another use, as does any sort of file copy. (Sure, the OS will do it--but sometimes that's not good enough.)

Loren Pechtel
A: 

I found I was using byte variables when doing some low-level image processing. The .Net GDI+ draw routines were really slow so I hand-rolled my own.

Most times, though, I stick with signed integers unless I am forced to use something larger, given the problem constraints. Any sort of physics modeling I do usually requires floats or doubles, even if I don't need the precision.

Charlie Salts
+2  A: 

The Java language itself makes it unreasonably difficult to use the byte or short types. Whenever you perform any operation on a byte or short value, Java promotes it to an int first, and the result of the operation is returned as an int. Also, they're signed, and there are no unsigned equivalents, which is another frequent source of frustration.

So you end up using byte a lot because it's still the basic building block of all things cyber, but the short type might as well not exist.

Alan Moore
Agree. int literals are easier to program with ( loops, counters etc ) however for specific functionality as mentioned by others those types are very handy.
OscarRyz
+1  A: 

Until today I haven't notice how seldom I use them.

I've use byte for network related stuff, but most of the times they were for my own tools/learning. In work projects these things are handled by frameworks ( JSP for instance )

Short? almost never.

Long? Neither.

My preferred integer literals are always int, for loops, counters, etc.

When data comes from another place ( a database for instance ) I use the proper type, but for literals I use always int.

OscarRyz
For databases, I usually go for wrappers. Yes, because of well-known null.
Adeel Ansari
mmhh I have a mixed feeling about wrappers. Fortunately with java 1.5 most of the time it is transparent after autoboximg
OscarRyz
A: 

Apache POI was using short quite a few times. Probably because of Excel's row/column number limitation.

A few months ago they changed to int replacing

createCell(short columnIndex)

with

createCell(int column).

asalamon74
+1  A: 

It's perhaps more interesting to look at the semantics of int. Are those arbitrary limits and silent truncation what you want? For application-level code really wants arbitrary sized integers, it's just that Java has no way of expressing those reasonably.

Tom Hawtin - tackline
+1  A: 

I use bytes in lots of different places, mostly involving low-level data processing. Unfortunately, the designers of the Java language made bytes signed. I can't think of any situation in which having negative byte values has been useful. Having a 0-255 range would have been much more helpful.

I don't think I've ever used shorts in any proper code. I also never use floats (if I need floating point values, I always use double).

I agree with Tom. Ideally, in high-level languages we shouldn't be concerned with the underlying machine representations. We should be able to define our own ranges or use arbitrary precision numbers.

Dan Dyer
+2  A: 

when we are programming for electronic devices like mobile phone we use byte and short.In this case we should take care on memory management.

BlackPanther