Wednesday, August 26, 2009

[Java] Boxing, ==, and equals() tweak

It shouldn't be surprising that

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");

Produces the output:

different objects
meaningfully equal

How about this one:

Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");

This example produces the output:

same object
meaningfully equal

Yikes! The equals() method seems to be working, but what happened with == and != ? Why is != telling us that i1 and i2 are different objects, when == is saying that i3 and i4 are the same object? In order to save memory, two instances of the following wrapper objects (created through boxing), will always be == when their primitive values are the same:
■ Boolean
■ Byte
■ Character from \u0000 to \u007f (7f is 127 in decimal)
■ Short and Integer from -128 to 127


Source: Sierra & Bates SCJP 6 Study Guide , Chapter 3

Tuesday, August 18, 2009

How to instal Intellij IDEA on Ubuntu

1. get your copy from here http://www.jetbrains.com/idea/download/
2. Unpack the idea-8.1.3.tar.gz file using the following command: tar xfz idea-8.1.3.tar.gz
3.
[this is the tricky part] Edit idea.sh from the bin subdirectory by adding the jdk 6 home directory to the variable IDEA_JDK (i.e. IDEA_JDK=/usr/java/jdk1.6.0 , depends on where you've placed the sdk )

Jah bless

make GRUB menu reappear after installing UBUNTU (having a vista installation to start with)

I had Ubuntu 9.04 (Jaunty) installed on a 80 gigs partition on my pc beacuse i was starting to get sick of Vista but after my first reboot, the system booted vista automatically without prompting me with the boot (grub) meu at startup. I've researched this over the web and came up with the following solution:

1. Use ubuntu live cd to boot into the unix system
2. open a terminal via Accesories menu
3. type "sudo grub"
4. type "find /boot/grub/stage1" - this will return a result, in my case it was (hd0,4)
5. type "root (hd?,?)" - in my case i typed root (hd0,4)
6. type "setup (hd0)"
7. quit and reboot

have a nice day