TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Java developers, are you using the primitives short or byte?

2 pointsby mettersover 4 years ago
I am a software developer with about three year experience (mostly JVM languages) and currently reading Code Complete 2.<p>Because of that I wondered why I never had a chance to use short or byte - or at least I never saw one. Of course I have learned about those, but during my professional time (circa 3.5 years and 4 projects) as a developer I believe to never have stumbled upon any usage.<p>I am just wondering whether they are or are not obsolete by now, because the performance gain seems to be not too extraordinary.

3 comments

wsc981over 4 years ago
I am not a Java developer by trade, but I do a lot of C# stuff and the languages are somewhat similar.<p>So with that in mind I imagine a byte type being useful for reading &#x2F; writing binary formats [0] (e.g. for sending small amounts of data over a network). A byte or short type can also be useful for using bitfields [1]. In some use cases you might want to use a small type to store the flags (e.g. if you don&#x27;t need 32 flags, it seems a waste to use a Int32 type).<p>---<p>[0]: <a href="https:&#x2F;&#x2F;www.codejava.net&#x2F;java-se&#x2F;file-io&#x2F;how-to-read-and-write-binary-files-in-java" rel="nofollow">https:&#x2F;&#x2F;www.codejava.net&#x2F;java-se&#x2F;file-io&#x2F;how-to-read-and-wri...</a><p>[1]: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bit_field" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bit_field</a>
评论 #24660494 未加载
jfengelover 4 years ago
I use byte in the context of Input&#x2F;OutputStreams, which operate on bytes. Ideally this will be wrapped in something that determines the encoding -- preferably one maintained by somebody other than me -- but I&#x27;m aware that serialization to disk or network ends up being a stream of bytes.<p>I&#x27;ve occasionally used short in similar contexts. I wrote a JVM library, and the JVM specification uses a bunch of short values -- though ironically, a lot of them are really unsigned values, so a short is inappropriate.<p>But for the most part, use of short or byte to save memory is very rare for me. I write Java knowing that it&#x27;s pretty flagrant with machine cycles and memory, and also knowing that computers have lots of memory and users care more about the difference between 10 vs 20 days to write a feature than about the difference between 10 vs 20 msec to reply to a click. I write Java because it lets me work faster, rather than for economy.
jartover 4 years ago
Primitive types in general are planned for deprecation in future releases of Java. The 8-bit &#x2F; 16-bit data structures have been obsolete for quite some time. If you haven&#x27;t used them in your career yet as a software developer it&#x27;s a good sign of progress that our industry is evolving beyond legacy technologies onto more enlightened thinking.
评论 #24660498 未加载