Skip to content

Tag: java

Comparing Numbers in Java

In Java, all numeric types extend from java.lang.Number. Would it be a good idea to have a method like the following: I’m concerned about cases where a double 2.00000 does not equal an int 2. Are these handled by the built-in equals? If not, is there any way to write a simple number compare function in …

Java : convert List of Bytes to array of bytes

Trying to solve what should be a simple problem. Got a list of Bytes, want to convert it at the end of a function to an array of bytes. compiler doesn’t like syntax on my toArray. How to fix this? Answer The compiler doesn’t like it, because byte[] isn’t Byte[]. What you can do is use common…

Reverse bits in number

For example, I have the binary number 1011 which is equal to decimal 11. I want the reverse bit’s location such that it become 1101, which is decimal 13. Here is code: But when I enter x 11 then it prints 26. What is the mistake? Answer You are shifting b one time too many. Do the shift first (so

Handling nested elements in JAXB

I am wondering if it is possible to have JAXB not to create Java object for XML elements that serve as wrappers. For example, for XML of the following structure I do not want an object for <wrapper> to be created at all. So for a class like the <entity> element should be unmarshalled directly into…