Operations involving byte, short, and char types in Java first convert these values to the int type, then the int type value, and finally the result of the int type. Therefore, if you add two byte values, you will end up with a result of type int. If you need to obtain a byte type result, you must explicitly convert the result of this int type to a byte type. For example, the following code causes compilation to fail:
Class Badarithmetic
{
static byte Addoneandone ()
{
byte a = 1;
byte B = 1;
byte C = (A + b);
return C;
}
}
When encountering the above code, JAVAC will give the following hint:
Type.java:6: Possible loss of precision
Found:int
Required:byte
byte C = (A + b);
Error
In order to remedy this situation, the result of the int type obtained by a + B must be explicitly converted to the byte type.
Recently, because of the financial project, the operation of the byte in the more demanding, so here to organize a bit about the byte type in Java.
There is no byte type in the Java Virtual machine, uh ... How to say, personal feeling this is a bit of a bluff. Indeed, when the idea just appeared in my mind, I felt a bit of baloney, after all, how can I say that there is no byte type in the Java virtual machine?
Well, let me explain it a little bit. The basic types of Java Virtual machine operations are basically done on the stack (this is believable, because it's not what I said). We know that when Java is working on a statement, it first presses the operand to the stack, then pops it out of the stack, and then presses the result back into the stack. This is also true of any operation on byte. As a result, Java is pressed into the stack before it operates on a byte type. In fact, Java is not pushed into the stack is not a byte type, but a standard int type (32-bit, byte is 8-bit), that is, the Java Virtual machine will be our short lovely byte type into the stack before it will be elongated into an int type and then press the stack. However, in fact, before the stack is an int type. In this way, the byte type that we pop in the stack or from the stack is actually stored in the length of the Int. That's why I said that there is no byte type in the Java virtual machine. Because they are all turned into int.
Int? Or a byte? So in the Java Virtual machine processing to deal with the 32-bit long int, then what about byte? In other words, if we see a 32-bit int, should we call it int or byte? For this question, my personal answer is that you are the name of the shrimp. For example: Now the top of the stack is two ... Well... 32-bit long ... Well... You know what I mean. You want to add them to the calculation. At this point your role is obvious, when you say to the virtual machine and add them to me as an integer, then the Java Virtual opportunity pops up these two things and then adds the results to the stack with the int type. But if you say to the virtual machine: Add these two bytes to a byte or add them together into a byte, then the Java Virtual machine will still pop up the two things added, but the previous sentence will first convert them into a byte and then int, and then add, and the next sentence will be added directly. The final result of the two sentences is to convert the sum into a byte and then into an int press into the stack.
So, what about type conversions? This should always be a byte!
Unfortunately, I can only say that the type conversion process will appear in the real byte, but it does not live until the end is stretched. For example, take a look at the two meaningless codes I've found in the program that make sense:
int a = 1;
byte B = (byte) A;
Well, I admit that the code will be written so that the program doesn't make sense to go anywhere. But we're on the same. When I first saw this, I was very excited to think that the variable B above should always be byte. If you are the same as me, then congratulations on your one step away from genius. I can only say that the answer is negative. Not to fight against you, but indeed to deny. Yes, the second sentence did produce a byte when it was executed, but unfortunately it didn't live to the end. Eventually it was pulled into an int pressed into the stack and used as the value of byte variable B. Although it was pulled into a 32-bit int, after all, it is a byte, so the body still has the descent of byte. How to say, that is, it is the virtual machine with symbols extended out. This is a good understanding that byte itself is a 8-bit 0 or 1 combination, you are 8 bits on each bit 0 or 1 pull the longer, at best, a long long 0 or 1 of byte. So to become a 32-bit, you have to fill byte 24 in. So where do these 24 people come from? The Java Virtual machine is done from a byte sign bit (that is, the highest bit). This is called a signed extension. Take the above procedure for example, the 1 compression into byte in binary view is 00000001, this I think we are not unfamiliar. Next is the extension, our byte sign bit is positive, that is 0, then the Java Virtual machine will fill the remaining 24 bits with zeros, the result is 00000000000000000000000000000001. Count yourself to see if I missed out.
You may think that my example is a little too simple, okay, I'm a hard one. Let byte variable b equals-1. Of course, it is not simple to change from the int type of-1 to the byte type of 1, but instead to the int type of a positive integer to truncate the Java virtual machine to the byte type of-1. So what is this positive integer? To tell the truth, I took the advanced calculator for a day and finally found it on Google: 2147483647. As long as the value of a in the above statement is changed to this, the value of byte variable B will be-1. Very simple, I think there is no need to explain. I'm sorry, I've got a little bit of a bum. Let me explain this: the binary of the 2147483647 integer is this: 01111111111111111111111111111111, carefully, 32 bits. Now we're going to force it into byte, only 8 bits, so the Java virtual machine will cut off 24 bits for us without thinking, and the remaining 8 bits are 1:11,111,111, which of course is that-1. What the? You said not? Yes-127? No no no, don't forget, the Java virtual machine uses the complement to express, you see is the complement. Let's figure this out again,-1. Well, the next step is to expand the int type. A simple copy of the symbol bit 24 out of the good, the result is 11111111111111111111111111111111. How many are these? Count yourself.
Sum up, well, say so much, we also see that although the operands in a Java Virtual machine can be a byte, the final result is int, whether it be arithmetic or type conversion. As for the difference in the implementation process, it is up to the person who wrote the program himself. If you are ambiguous, don't expect Java virtual opportunities to understand what you mean.
public class Test {
public static void Main (string[] args) {
int a=35461;
System.out.println (Test.byte2int (Test.int2byte (a)));
}
/**
* Convert int to byte[]
*
* @param res integer to convert
* @return the corresponding byte[]
*/
public static byte[] Int2byte (int res) {
Byte[] targets = new BYTE[4];
Targets[0] = (byte) (res & 0xff);//Lowest bit
TARGETS[1] = (byte) ((res >> 8) & 0xff);//Sub-low
TARGETS[2] = (byte) ((res >>) & 0xff);//Secondary high
TARGETS[3] = (byte) (res >>> 24);//highest bit, unsigned right shift.
return targets;
}
/**
* Convert byte[] to int
* @param res to convert byte[]
* @return the corresponding integer
*/
public static int Byte2int (byte[] res) {
int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00) | ((res[2] <<) >>> 8) | (Res[3] << 24);
return targets;
}
}
The difference between int and byte