Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, August 18, 2011

Java puzzler - Letter went missing

Recently I've implemented function that takes String and populates long so that it contains character values from the back of the string. However my unit test is failing because one Letter is missing. Are you able to put it to the right place so that unit tests will not fail without debugging the code?

import static org.junit.Assert.*;
import org.junit.Test;

public class StringToolsTest {

	static final char [] data = {0xca, 0xfe, 0xeb, 0xab, 0xee};
	static final String string5 = String.valueOf(data);
	static final String string0 = "";
	static final String string1 = "a";
	static final String string9 = "987654321";
	
	@Test
	public void testFillBackwards() {
		long l = 0;
		assertEquals(0xcafeebabeeL, StringTools.fillBackwards(string5, l));
		assertEquals(0L, StringTools.fillBackwards(string0, l));
		assertEquals(0x61L, StringTools.fillBackwards(string1, l));
		assertEquals(0x3837363534333231L, StringTools.fillBackwards(string9, l));
	}
}

public class StringTools {
	public static long fillBackwards(String s, long d)
	{
		long result = 0L;
		byte[] bytes = s.getBytes();
		for (int i=0; i<Math.min(8,bytes.length); i++){
			result |= ((bytes[bytes.length-i-1] & 0xff) << (8*i));
		}
		return result;
	}
}

Solution bellow (highlight to show):
The problem is on the line 29 in "& 0xFF". I should be doing &0xFFL instead. Why ? bytes[x]&0xff is by default int (both operands are widened to int), but then << is applied, result of which is type of left operand (java spec) and this is |= to long result. Solution is to make & operator to work on long values by adding L to 0xFF.
Puzzler on snipplr.

Saturday, August 13, 2011

Question Interview

One of my friends got this question on his interview: "Given is an arbitrary long string, revert order of words in the string". If the input is "Hello World!", function should modify string to: "World! Hello". Function must have this interface: "void revert(char * string)". It must work in linear time O(n) and have no special memory requirements M(1).

My solution (spoiler alert)

Thursday, July 21, 2011

Java puzzler - How many integers are in the box ?

This is most basis Java Puzzler. Question is, how will the output look like ?
  • Will it print out powers of 10 starting from 1 ending at 10000000.
  • Or will it print just 1.
  • Or will it print any other number.
  • Or will it loop forever.
  • Or anything else.
package sk.adino.puzzlers;

import static org.junit.Assert.*;
import org.junit.Test;

public class AutoboxingTest {

	@Test
	public void test() {
		int i=1;
		while(i<10000000) {
			System.out.println("value:" + i);
			Integer j=i;
			Integer k=i;
			if (j==k){
				i=i*10;
			}else break;
		}
		assertEquals(i,1000);
	}
}

Thursday, November 27, 2008

Java EE factsheets

Stefan Jäger recently posted bunch of Java EE fact-sheets on his blog. Stefan created fact-sheets for: SF and SL session beans, MDB's and JMS, Transactions and security, and finally JPA. Very nice, and available as PDF downloads.

Thursday, October 30, 2008

Puzzlers series

I guess everyone have been in situation when he/she has written code, which didn't behave as it should. Most of these times, problem was of PEBKAC (problem exists between keyboard and chair) type. Computers (nearly) always do, what they're told to do. Under influence of Java Puzzlers book by Neal Gafter and Joshua Bloch, I've decided to start series of posts to this blog, which would cover situations when program doesn't do what we think it should. At first posts will be mostly in C/C++, but i think more languages will be covered in the future. Posts will be formated, so that you'll see only problem with possible answers. Solution will have font color set to background color, or some close color, so you'd have to highlight block in order to see it. You're more than welcome to add your own puzzlers. Just contact me.

Thursday, October 16, 2008

Java Puzzlers

Oh my - oh my - oh my. Yet another funny presentation, this time by Joshua Bloch (Author of Effective Java) and Neal Gafter (Java Puzzlers: Traps, Pitfalls, and Corner Cases, closures proposal for Java7). Presentation is from JavaPolis 2007 and it's called Java Puzzlers/Overview. It's interesting even for non Java programmers.

Tuesday, September 23, 2008

Rules of API design

Joshua Bloch, author of Effective Java writes article about key principles of designing API's. However not only API developers should read this, because as first points states: "All programmers are API designers".
These principles are also mentioned in his original presentation:
How to Design a Good API & Why it Matters.

Monday, March 3, 2008

Eclipse and Java VM memory tuning

How to improve performance of eclipse instalation ? Well, I'm sure there are lot of posts about this on the internet, but it all narrows down to few steps:
  1. get rid of modules you don't use
  2. tune heap size of JavaVM
  3. tune perm size of JavaVM
Let's look closely at these steps.

1. get rid of unused modules
I like to test new features and modules for eclipse. But after some time I found out that these modules consume lot of memory and increase startup time. You're likely to accept slowdown due to some module that you realy use and it improves your coding. But modules, which you use only ocasionaly should go, or at least you should disable them.

2. tune JavaVM heap size
All objects instantiated during runtime live in heap. Therefore, the more heap you have, more likely all objects will fit there. JavaVM needs to have all of it's VM memory online in physical memory. You can specify maximum size of JavaVM memory with -Xmx<size>. Attempt to allocate more than -Xmx memory will end with OOM exceptions. On the other hand there is -Xms option to JavaVM. With this you specify how much of -Xmx should be directly allocated by JVM. Specifying this to same size as -Xmx means there will be longer startup, but application will run smother.

3. tune JavaVM perm size
The perm memory is the area of the VM, which is used to store data structures and class information. In case you have project with huge amount of classes, JVM may reach the limit of MaxPermSize. Simmiliar with -Xmx and -Xms there are two options:
-XX:PermSize=<size>
-XX:MaxPermSize=<size>

Use -vmargs as parameter to eclipse.exe together with these parameters to tune performance of your eclipse. My values are: -vmargs -Xms256m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=64m