Code below makes foo value as -1149239296 i. e. integer value which is out of bounds: Seems like Java takes type of first parameter and tryes to return formula’s result with that type. Where in Java specification one can read that story? I made such suggestion cause actually returns sets long value to foo. Answer For this case, casting any
Tag: casting
(int) Math.sqrt(n) much slower than (int) Math.floor(Math.sqrt(n))
I was looking at my code, hoping to improve its performance and then i saw this: Oh, ok, i don’t really need the call to Math.floor, as casting the double returned from Math.sqrt(n) will be effectively flooring the number too (as sqrt will never return a negative number). So i went and dropped the call to Math.floor: sat back and
Casting LinkedHashMap to Complex Object
I’ve got an application that stores some data in DynamoDB using Jackson to marshall my complex object into a JSON. For example the object I’m marshalling might look like this: Where SomeObject might look like this: and SomeOtherObject might look like this: This is fine an the object gets marshalled no problem and stored in the DB as a JSON
Dealing with an ArrayStoreException
throws while doesn’t. Is there any other way to deal with that exception without creating a temporary String[] array? Answer In Java an array is also an object. You can put an object of a subtype into a variable of a supertype. For example you can put a String object into an Object variable. Unfortunately, the array definition in Java
Cast Double to Integer in Java
Any way to cast java.lang.Double to java.lang.Integer? It throws an exception “java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer” Answer A Double is not an Integer, so the cast won’t work. Note the difference between the Double class and the double primitive. Also note that a Double is a Number, so it has the method intValue, which you can use to get the
Most efficient way to cast List to List
I have a List<SubClass> that I want to treat as a List<BaseClass>. It seems like it shouldn’t be a problem since casting a SubClass to a BaseClass is a snap, but my compiler complains that the cast is impossible. So, what’s the best way to get a reference to the same objects as a List<BaseClass>? Right now I’m just making
Java convert Arraylist to float[]
How I can do that? I have an arraylist, with float elements. (Arraylist <Float>) it is not working. cannot cast from Object[] to float[] Answer Loop over it yourself. The nullcheck is mandatory to avoid NullPointerException because a Float (an object) can be null while a float (a primitive) cannot be null at all. In case you’re on Java 8
Fastest way to cast Java Objects from memcache
We have a webapp which uses memcache to store objects temporally. We are adding background jobs to cleanup the cache (not based on time based expiration that comes with memcache). Since we have multiple instances of the webapp running, we want to make sure that only one instance is doing the cleanup at any point. So we decided to keep
Why doesn’t this generic cast fail?
I’d expect this code to throw a ClassCastException: But it doesn’t. Casting String to T doesn’t fail, until I use the returned object somehow, like: Background: I created a Class which uses JAXB to unmarshal an XML file. It looks like this: Depending on whether the root-Element is an anonymous type or not, either T or JAXBElement is being returned.
Convert boolean to int in Java
What is the most accepted way to convert a boolean to an int in Java? Answer ^^ PS : true = 1 and false = 0