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
Android VideoView not playing sample video on T-Mobile G2 (just audio)
What I’m trying to do is play the first video retrieved from the external SD card, which on my T-Mobile G2 turns out to be the sample video for the phone. Now I assumed that since it plays in the phones video player, that it’d have no problems playing in the VideoView in my test app. However, all I get
How do I iterate through the files in a directory and it’s sub-directories in Java?
I need to get a list of all the files in a directory, including files in all the sub-directories. What is the standard way to accomplish directory iteration with Java? Answer You can use File#isDirectory() to test if the given file (path) is a directory. If this is true, then you just call the same method again with its File#listFiles()
Persisting set of Enums in a many-to-many unidirectional mapping
I’m using Hibernate 3.5.2-FINAL with annotations to specify my persistence mappings. I’m struggling with modelling a relationship between an Application and a set of Platforms. Each application is available for a set of platforms. From all the reading and searching I’ve done, I think I need to have the platform enum class be persisted as an Entity, and to have
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 the entity field. Is
why use Heap Memory in Java
Why do we use Heap Memory, Can we use Stack memory for the same? EDITED One more question came in my mind after reading answers 1) is there any other kind of memory which we can think of alternative to Heap and Stack? Edited I came across the string pool, is that memory associated with the heap or Stack? Answer
Integer division: How do you produce a double?
For this code block: the value of d is 0.0. It can be forced to work by casting: But is there another way to get the correct double result? I don’t like casting primitives, who knows what may happen. Answer That avoids a cast. But you’ll find that the cast conversions are well-defined. You don’t have to guess, just check
Weird Integer boxing in Java
I just saw code similar to this: When ran, this block of code will print out: I understand why the first is false: because the two objects are separate objects, so the == compares the references. But I can’t figure out, why is the second statement returning true? Is there some strange autoboxing rule that kicks in when an Integer’s
Why does this Java/Groovy code cause heap memory exceptions?
This 3rd party script keeps causing heap memory exceptions: That part of the script, which receives messages through TCP/IP after receiving the message, causes the following exception: Exception in thread “Thread-2” org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.OutOfMemoryError: Java heap space at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234) at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:880) at groovy.lang.Closure.call(Closure.java:279) at groovy.lang.Closure.call(Closure.java:292) at org.codehaus.groovy.runtime.DefaultGroovyMethods$6.run(DefaultGroovyMethods.java:11563) at java.lang.Thread.run(Thread.java:636) Caused by: java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2746)
How do servlets work? Instantiation, sessions, shared variables and multithreading
Suppose, I have a webserver which holds numerous servlets. For information passing among those servlets I am setting session and instance variables. Now, if 2 or more users send request to this …