Monday, 17 June 2013

Eclipse loves you, but sometimes it's not easy love...

There are couple of things that are annoying in Eclipse, but overall it's great tool (and for JavaSE I prefer it even over IntelliJ), so it just needs to be fine tuned to be as awesome as Barney.


Firstly:
http://eclipse.dzone.com/articles/quickly-declare-and-initialise - I personally love templates, and I started to love this one just few seconds ago - removing one of the grates pain in the ass: 
(List<Sth> sths= new ArrayList<Sth>();) of my daily work is the main reason for writing this entry.

Secondly - decrease autocomplete delay:

Making autocomplete really work (the way it should, IMO)

This is also worth to mention (but I stayed with default behavior here):


PS: http://www.yourkit.com/ <- this is damn awesome profiler (WAY better than JvisualVM, even with some plugins), too bad it's so costly and trial is only 15 days long. Anyway, that tool is surely WORTH being tested - just take a look here: http://www.yourkit.com/features/index.jsp#objects_view It saved my ass last week.

Monday, 13 May 2013

Java memory 'n stuff

I have found not bad presentation about memory complexity in Java. Clean code still should be a priority, but it's good to know the results of taken implementation decisions.

http://www.cs.virginia.edu/kim/publicity/pldi09tutorials/memory-efficient-java-tutorial.pdf


Saturday, 2 February 2013

Java References vs OutOfMemoryError



3
2
1

Go go go, read it! ;)


I have found this article while searching something exactly like it on StackOverflow: SoftReferences vs Weakreferences / OutOfMemoryError so I'm just (re)sharing it.



All my interest about java references' is thanks to Marek Defeciński and his todays speech about them   (you can easy find him @ web - he's well known megrer on GitHub* ;) ) 










(* - Jacek Laskowski today talked about GitHub and it's social aspect that motivates people to constant growing their skills - great job!)

Tuesday, 22 January 2013

Comapring floating-points

Seems straight-forward... "Use delta!"


But today I tried explain it to my fiend, and... it wasn't that easy for me. So as title of my blog says "if you can't explain it simply you don't understand it well enough" - it was time to use google :)


And here it comes: http://randomascii.wordpress.com/2012/06/26/doubles-are-not-floats-so-dont-compare-them/ - I really recommend reading it, even as a Senior Developer you can be surprised by some things (I was).

Some sample to encourage you - yes, it's worth your damn precious time:

   float x = 1.1;
   if (x != 1.1)
      printf(“OMG! Floats suck!\n”);


On a fairly regular basis somebody will write code like this and then be shocked that the message is printed. Then somebody inevitably points them to my article and tells them to use an epsilon, and whenever that happens another angel loses their wings.

As mentioned in above article choosing the right epsilon the key. Here is nicely explained how to how to make  that choice.

And lastly, link provided by Tomek - a lot of reading ;)

Sunday, 2 December 2012

Java puzzle ;)

    Integer a = 200, b = 200;

    System.out.println(a < b || a == b || a > b);

    System.out.println(a <= b || a > b);



I'm not the type of guy who TYPICALLY likes stuff like that. IMO code used in many java-mind-fucks is just unreal to be found in application written by someone that is not out of his mind ;) But this one posted here I find to be just lovely :)


If you have your own favorites, post them as a comment :)


Ok, but lest focus on above code sample.

Here is short answer and explanation: FALSE and TRUE. True because of both `a` and `b` are damn numbers, so it has to be truth no matter what. And false, because: `a` is NOT lower than `b`; `a` is NOT grater then `b` and `a` and `b` are not pointing to the same Intereger object. Cache size is <-128, 127> - check the Integer javadocs if needed.

But that's not all, things can start being interesting from now on. You can actually make both of those sysouts TRUE! How?

Take a look here:



from java.lang.Integer:
    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }



It's like mindfuck inside mindfuck - no1 expects this :)

Anyway, empowered with that knowledge, lets hope it's not useless, but alike to the python paradox. Knowing that little trick does NOT make you a better programmer, but there is a big chance that you ARE already a good programmer if you know about it ;-)

Monday, 19 November 2012

Some simple Windows tools that will make you happy

Currently I'm migrating from one PC to the other, so there are couple of things that I HAVE to take there with me:
 

Map Any Key to Any Key on Windows 7 / XP / Vista - can be useful if you have page up and page down keys next to arrows, IMO home / end should be placed there.

Ditto - Clipboard Manager - remembers all CTR+C 's :)

Unlocker

Monday, 12 November 2012

Producer-Consumer sample - Listing files

Listing files is reeally slow operation.

That's why I decided that it will be a good use case to have some fun with java's concurrency.

Producer-Consumer pattern seemed to fit my needs.
  1. Producer is listing all files from given directory. 
  2. Consumer is filtering proper files and propagates this data to GUI in real-time. 
  3. Both are connected by BlockingQueue
BlockingQueue<List<File>> myQueue = new LinkedBlockingQueue<List<File>>();

Please find my sources @ https://bitbucket.org/pawelmichalski/repo/src.

... Actually I should write something about my code to justify some of my implementating decisions, but it's being late right now in Poland :) so consider this entry as asking for code review in some free time.


cheers.