Wednesday, May 23, 2007

Weird Integer

Take a look at this simple class :

class Werid
{
public static void main(String args[])
{
Integer j1 = 127;
Integer j2 = 127;
System.out.println(j1==j2); // true

Integer k1 = 128;
Integer k2 = 128;
System.out.println(k1==k2); // false
}
}

How do you think, is it all OK here ? :) There is _something_ in this class. I've noticed that for the numbers less than 128 operator == returns true.
Why ?..

Lets see... When we run our Werid class with -verbose option we'll get sometting like this :

C:\>java -verbose Werid
.......
.......
[Loaded Werid from file:/C:/]
[Loaded java.lang.Integer$IntegerCache from shared objects file]
true
true
false
false
.......
.......
OK. What we have at java.lang.Integer$IntegerCache ? Cache of Integers is something interesting already. Lets go to sources :
private static class IntegerCache {
private IntegerCache(){}
static final Integer cache[] =
new
Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}
Great, it really cache. It is used only in one method:
/**
* Returns a Integer instance representing the
specified
int value.
* If a new Integer instance is not required,
this method
* should generally be used in preference to
the constructor

* {@link #Integer(int)}, as this method is likely
to yield

* significantly better space and time performance
by caching

* frequently requested values.
*
* @param i an int value.
* @return a Integer instance representing
i.
* @since 1.5
*/
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
Ahaa :) if (i >= -128 && i <= 127).
Things become more clear now.

Tuesday, May 22, 2007

New atricle about JSF at Theserverside

Here it is. I'm agree with author, 100%. Lack of cool third party components is not good at all. I think to myself "Is there some commercial JSF stuff ?". Opensource is even better :)

Dear readers, if you know some interesting products, please give me a sign. I saw a few in ASP.NET (AJAX data grids and so on), it will be very interesting to compare.

Friday, May 11, 2007

Wikipedia internals

I've noticed that my post about eBay internals was very popular. So, here is another one post about big and complex system - Wikipedia. It is presentation on MySQL Conference & Expo. LAMP environment under heavy load... They are using Apache Lucene for Search. But what they say about Java:

Due to licensing issues Wikipedia did not run Java Virtual Machine from Sun Microsystems (non-free software is not matching free content ideals), so alternatives were chosen - at first GCJ-based solution, afterwards .net Lucene port was used on top of Mono .NET framework.
Interesting point of view... *nix implementation of .NET framework (sponsored by... not Microsof but Novell) with port of well-known solution is better than using "native" Java with JVM.

Another hint is :
The major components for search are:
.....
....
Mono (or GCJ.. or JVM... depends on mood - we have support for all).
Depends on mood is great ! :) It is still not clear for me what actually they use. But is not a problem, I like wikipedia and using it all the time. If some solution works - let it be so.


Another question is why Lucene ? Why not Swish-e for example ? It is nice product, Apache folks using it at http://search.apache.org.

Saturday, May 05, 2007

Songs about Java EE 5 and Code Monkey

Googling for something not always bring expected results, but other results can appear not less interesting. I have decided to find a song fragment which sounds in Javaposse podcast. As a result I've come across 2 other songs. First one is about Java EE 5.

Another one is about code monkey... not so fun as first, more sad I should say :

Code Monkey get up get coffee.
Code Monkey go to job.
Code Monkey have boring meeting
with boring manager Rob.

Rob say Code Monkey very dilligent
but his output stink.
His code not functional or ellegant
what do Code Monkey think?

Code Monkey think maybe manager wanna write god damn login page himself.
Code Monkey not say it outloud.
Code Monkey not crazy, just proud.
It is from here.
MP3 , Lyrics

Enjoy :)