Technical Tips, Tricks and Articles

Archive for the ‘java’ Category

Comparing Wrapper objects values in range –128 to 127

leave a comment »

While debugging through a bug that I encountered in one of my ongoing project. I came to know a strange behavior of Java. Consider the following code segment

1 Integer a = 10;
2 Integer b = 10;
3 
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

If you look closely at line no 4, then you would realize that the code is actually comparing “Java objects”. Since “a” and “b” are not wrapper classes not primitive types.  But even then the condition is being true. Ok, some how this makes sense (at least till now unless you read this code segment)

1 Integer a = 150;
2 Integer b = 150;
3 
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

Astonishingly, for the above code, the if statement would not execute true. Why?

Well, ever since Java has introduced “auto-boxing” feature, you’ll face this issue. Why is it so? Actually, compiler try to optimize things as much it can. For this, Java has a pool of values from –128 to 127. Whenever, there is some auto-boxing between these values, the reference object for the wrapper class will get the same memory location. In both cases, we are comparing “object references” but in first case, since the value was in the range for both objects hence, Java assigned the same reference location to both objects, making the condition “true” on comparing memory location for two objects.

Now, the question is why between –128 to 127?. Well, the smallest range of values for smallest wrapper class is between –128 to 127 i.e. Byte.

Written by ..alee

July 5, 2009 at 1:06 pm

Posted in code, interview, java, programming

Tagged with , ,

Error starting eclipse

with 4 comments

You might face an error while starting eclipse on a freshly installed system, or on the system which has undergone a fresh upgrade. The error message will be like

—————————
Eclipse
—————————
JVM terminated. Exit code=-1
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
-XX:MaxPermSize=256M
-Djava.class.path=D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
-os win32
-ws win32
-arch x86
-showsplash D:\eclipse-jee-ganymede-win32\eclipse\\plugins\org.eclipse.platform_3.3.100.v200806172000\splash.bmp
-launcher D:\eclipse-jee-ganymede-win32\eclipse\eclipse.exe
-name Eclipse
–launcher.library D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher.win32.win32.x86_1.0.100.v20080509-1800\eclipse_1114.dll
-startup D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
-framework plugins\org.eclipse.osgi_3.4.0.v20080605-1900.jar
-vm C:\Program Files\Java\jre1.6.0\bin\client\jvm.dll
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
-XX:MaxPermSize=256M
-Djava.class.path=D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
—————————
OK
—————————

Actually, eclipse is trying to get the version of java that is installed on system. you might have installed that latest one but there are possibilities that any upgradation of later softwares has replaced the java.exe files in system32 folder with older version. All you need to do is to run eclipse with clean parameters. Create a batch file in the directory where eclipse.exe is and write this line in it eclipse -clean -vmargs -Xmx256m

Written by ..alee

January 30, 2009 at 4:11 pm

Posted in java

OO Questions for .NET/Java

leave a comment »

  1. To which is protected class-level variable available to?
    Answer: It is available to any sub-class (a class inheriting this class).
  2. Are private class-levl variables inherited?
    Answer: Yes, but those are not accessible. Although those are not visible or accessible via the class interface, yet those are inherited.
  3. Describe the accessiblilty modifier "protected internal"?
    Answer: It is available to classes that are within the same assembly and derived from specific base class.

Written by ..alee

November 16, 2006 at 3:52 pm