Byte primitive in Java - java

I have been working on a project with images and I read on internet that working in byte for images solves alot of problems and improve efficiency. I tried to google it but didn't find any definition for it. So I came here to ask
What is byte in Java? What is it use?

You can read the Java tutorials.
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

byte is one of the primitive types of java. Is an abstraction of a byte (8 bit integer with sign).

Related

Why byte datatype is used for file IO java? [duplicate]

This question already has answers here:
Is a byte always 8 bits?
(3 answers)
Closed 3 years ago.
I understand byte is the underlying data type of Java IO but why byte is used to read and write as it has maximum value range -128 to 127. This range is integer, how can integers be used for reading and writing different symbols characters or binary data ?
Expectation is to understand why byte data type is used for Java IO.
Java was designed after C/C++ with some discussed topics:
Java text String (Reader/Writer) contains Unicode so text in mixed scripts may be combined. Internally String was an array of UTF-16 char; a .class file uses UTF-8 string constants. Hence byte[] (InputStream/OutputStream) is only for binary data. Between text and binary data there is always a conversion using the binary data's encoding/charset.
Numerical primitive types exist only in the signed version. (Except char that one could consider non-numeric.) The idea was to root out signed/unsigned "problems" of C++. So also byte is signed from -128 to 127. However overflow is irrelevant in java too,
so one can do:
byte b = (byte) 255; // 0xFF or -1
The primitive types byte/short/int/long have a fixed size of bytes, where C was notorious cross-platform, and things like C uint32 a bit ugly (32).
Having experienced tricky C bugs with signed/unsigned myself (before >10 years), I think this decision was okay.
It is easier to calculate in a signed mind set, and then at the end consider values as unsigned, than have throughout the expressions signed and unsigned parts.
Nowadays there is support in java for calculations respecting an unsigned interpretation of values, like Integer.parseUnsignedInt.

What is byte data type in java and Why it is used?

I am little bit confused in a data type of java named byte data type and why they are used?
I don't have any idea about byte data type. Can anybody help me in resolving my problem ?
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
via: Java Docs

Is a Java array consisting of bytes/shorts instead of integers more memory efficient?

On many posts on Stackoverflow people said that using bytes or shorts instead of integers did not reduce the memory usage or CPU utilization.
However, if I have an array of bytes or shorts, would that array use less memory and/or be faster to iterate over than a similar array of integers.
To be specific: I'm asking about the primitive type.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
As mentioned in Java documentation byte/short can be used for saving memory in large arrays.
In case of byte, the variable declared as byte can also serve as a form of a documentation since its value is limited by the range. ex of byte (-128 to 127).
Also take a look at the excellent answers in the following post,
In java, is it more efficient to use byte or short instead of int and float instead of double?

I want to store a number between 0 and 100 in a variable - should I use an INT or a BYTE and why?

I know this is a n00b question but I want to define a variable with a value between 0 and 100 in JAVA. I can use INT or BYTE as data types - but which one would be the best to use. And why?(benefits?)
Either int or byte would work for storing a number in that range.
Which is best depends on what you are aiming to do.
If you need to store lots of these numbers in an array, then a byte[] will take less space than an int[] with the same length.
(Surprisingly) a byte variable takes the same amount of space as a int ... due to the way that the JVM does the stack frame / object frame layout.
If you are doing lots of arithmetic, etc using these values, then int is more convenient than byte. In Java, arithmetic and bitwise operations automatically promote the operands to int, so when you need to assign back to a byte you need to use a (byte) type-cast.
Use byte datatype to store value between -128 to 127
I think since you have used 0.00 with decimals, this means you want to allow decimals.
The best way to do this is by using a floating point type: double or float.
However, if you don't need decimals, and the number is always an integer, you can use either int or byte. Byte can hold anything up to +127 or down to -128, and takes up much less space than an int.
with 32-bit ints on your system, your byte would take up 1/4 of the space!
The disadvantages are that you have to be careful when doing arithmetic on bytes - they can overflow, and cause unexpected results if they go out of range.
If you are using basic IO functions, like creating sounds or reading old file formats, byte is invaluable. But watch out for the sign convention: negative has the high-order bit set. However when doing normal numerical calculations, I always use int, since it allows easy extension to larger values if needed, and 32-bit cpus there is minimal speed cost really. Only thing is if you are storing a large quantity of them.
Citing Primitive Data Types:
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
Well it depends on what you're doing... out of the blue like that I would say use int because it's a standard way to declare an integer. But, if you do something specific that require memory optimization than maybe byte could be better in that case.
Technically, neither one is appropriate--if you need to store the decimal part, you want a float.
I'd personally argue that unless you're actually getting into resource-constrained issues (embedded system, big data, etc.) your code will be slightly clearer if you go with int. Good style is about being legible to people, so indicating that you're working with integers might make more sense than prematurely optimizing and thus forcing future maintainers of your code (including yourself) to speculate as to the true nature of that variable.

what data structure should I use to store binary codes in java?

I have binary codes as 10111 , 100011, 11101111 etc. now what data structure should I use to store these codes so that minimum size is required to store them?
I can't use string array as size required will be more as compared than storing the decimal equivalent of above binary codes.
java.util.BitSet is designed for that if the length is not fixed.
Depending on the length of the codes, simply use int or long.
If they'll be short, use byte, int, long (depending on how short).
If they'll be a bit longer, use an array of bytes, ints, or longs. For instance, if you need to store a 256-bit code, you can do that in a long[4].
If the length of the codes you need to store varies widely, you might consider either a class with a length member giving the number of bits and a byte, int, long, byte[], int[], or long[] member for storing them (depending on sizes and what kind of granularity you want). Or if you're really trying to pack as much in as you can, you can set aside some of the bits from your storage area to hold the number of bits in the code.
Each binary code could be split in portions by 8 bits (a byte). It would be necessary to define how to handle the tail if it is less than 8 bits. Byte arrays should work here nicely to store each portion.

Categories

Resources