What is the Size of ” null ” Operator in Java?

[ This Blog Was Moved To : http://www.diknows.com ]

Today, in the Java Developers Group in LinkedIn; I found this question posted by Sneha Kesri. She asked “What is the size of ” null ” Operator in Java ?”.

Actually, I never thought of it that way, but I continued reading.

Ramesh Babu Kotehal replied with the following :

from the heap memory perspective it will be 0, as it wont allocate any memory from heap. But from stack memory perspective the object has reference which is set to null. and the reference variable size depends your system.
for 32 bit system it will be 4 byte and for 64 bit system it will be 8 byte.

but as such null is not referring to any data from heap, so heap memory will be same.

And that was the only reply by then, which was correct, but Sneha Kesri asked another question:

Thanks Ramesh, I am having one more doubt… when we say..

String str1 = null;
String str2 ="";

Since str1& str2 are not equal.Does Str2 allocates memory in heap?
Does it take any value i.e garbage value for str2;

And is there any way to check memory size in Java?

So, I replied her with the following :

When you write

String st1 = null;

You create a reference “str1” to an object that still does not exist in the heap, but when you type :

String st2 = "" ;

You create a reference “str2” to a string object that is already allocated in the heap which preserves the string object size.

if you do that expression

str1 == str2 ;

It should evaluate to false, because you are comparing reference values, which will not be equal.

if you do that expression

if(str1.equals(str2) ){ }

It should throw a NullPointerException because you are trying to access the method equals on a null object ( str1 )

but if you try that expression

if(str2.equals(str1) ) {}

It should return false, because the value of ( str2 ) is an empty string not null object.

I wish It was helpful.



Simple Apache Ant Tutorial

[ This Blog Was Moved To : http://www.diknows.com ]


Ant is free and open source build tool, written in Java, helps in automating the entire build process of a Java development project.

  • Ant uses XML build files.
  • By default, Ant looks for a build file named build.xml.
  • The build file contains information about how to build a particular project.
  • Each project contains multiple targets like creating directory, compiling source codes.
  • Target can depend on other targets.
  • Targets contain tasks.
  • Behind each task is a Java class that performs the described work.


To install Ant follow the steps given below.

  • Download the latest Ant package.
  • Extract it (my location is /home/dino/installations/apache-ant-1.7.1)
  • Set the following system variables
  • ANT_HOME =/home/dino/installations/apache-ant-1.7.1
  • PATH = %ANT_HOME%/bin


  • If you have root access, you can use your preferred Linux package manager (I use Yast) to install Ant

To make sure the installation is done correctly, open a console window and execute the command “ant – version”, you will see the installed ant version.

dino@linux-vxrd:~> ant -version
Apache Ant version 1.7.1 compiled on October 20 2009


In the next example you will see how to compile a java program and compress it into a .jar file using Ant build file. The following listing shows the build.xml file.

<?xml version="1.0" ?>
<project name="Hello World" default="compress">

	<target name="compile" depends="init">
		<echo>Starting the compilation process</echo>
		<javac srcdir="src" destdir="build/classes" source="1.5" target="1.5"/>
		<echo>Compilation Completed Successfully !</echo>

	<move todir="build/classes/">
		<fileset dir="src">
			<include name="**/*.class"/>
			<exclude name="**/*.java"/>

	<target name="init">
		<mkdir dir="build/classes" />
		<mkdir dir="dist" />

	<target name="compress"  depends="compile">
		<jar destfile="dist/HelloWorld.jar" basedir="build/classes" >
				<attribute name="Created-By" value="Diaa Kasem"/>
				<attribute name="Main-Class" value="HelloWorld"/>
		<echo>Building .jar file completed successfully!</echo>

	<target name="execute" depends="compile">
		<java classname="dino.HelloWorld" classpath="build/classes" />

	<target name="clean">
		<delete dir="build" />
		<delete dir="dist" />


  • The <project> element is the root element in Ant build files. The name attribute of the project element indicates the project name. Each project element can contain multiple <target> elements.
  • A target represents a single stage in the build process. A build process can have multiple targets. Here we have several targets compile, compress, init, execute and clean.
  • The default attribute of project element indicates the default target to be executed. Here the default target is compress.
  • When you see the compress target, it in turn depends on the compile target, that is indicated by the depends attribute. So the compile target will be executed first and because compile target depends on init target, init should be executed first.
  • The compile target has two task elements <javac> and <echo>. The javac task is used to compile the java files. The attribute srcdir=”.” indicates all the java files in the current directory. Echo task is used to display message on the console.
  • The compress target also performs two tasks, first the <jar> element as the name indicates, is used to build the jar file. The attributes destfile=”dist/HelloWorld.jar” , basedir=”build/classes” and includes=”**/*.class” indicates all the .class files in all the sub-directories should be compressed into HelloWorld.jar file. Later the echo task is used to display the success message on the console.

For a easier maintenanace all the source file should be kept in the src directory, the compressed jar file should be kept in the dist directory and all the intermediate class files should be kept in the build/classes directory.

By imposing structure cleaning the project becomes easy, we can just delete the directory and recreate it.

Using the <mkdir> task we create build/classes and dist directory.

<target name="init">
    <mkdir dir="build/classes" />
    <mkdir dir="dist" />

During the compilation process all the java files in the src directory will be compiled and the generated class files will be placed in the build/classes directory.

Since we placed all the class files under the build/classes directory, creating jar file becomes easier, you can simply specify the basedir attribute as “build/classes” instead of specifying basedir=”.” and includes=”*.class”. After creating the jar file we place it in the dist directory (destfile=”dist/HelloWorld.jar”).

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build/classes" />
<target name="compress" depends="compile">
        <jar destfile="dist/HelloWorld.jar" basedir="build/classes" />

You can use the java task to execute a class file as shown below. The classname attribute refers to the java class to be executed and the classpath attribute refers to the directory in which the class is located.

<target name="execute" depends="compile">
    <java classname="dino.HelloWorld" classpath="build/classes" />

Since all the class files and jar files are isolated, we can easily clean the project by deleting the respective directories.

<target name="clean">
    <delete dir="build" />
    <delete dir="dist" />

When you run the “ant execute” command the execute target will be invoked. Execute target also depends on compile target which in turn depends on init target, but now the directories won’t be created again because it already exist. Since the java file is already compiled it won’t be compiled again. Ant checks the timestamp of the file and compiles only the updated files. The HelloWorld.class file will be execute and the “HelloWorld!” message will be displayed on the console.

dino@linux-vxrd:~/Documents/anttest/helloworld> ant execute
Buildfile: build.xml


[echo] Starting the compilation process
[echo] Compilation Completed Successfully !

[java] Hello ANT Wolrd !

Total time: 1 second

The java source file I used is :

package dino;

public class HelloWorld{
public static void main(String [] args){
System.out.println("Hello ANT Wolrd !");

And this is the directory structure:

Ant Project Directory Structure

Ant Project Directory Structure

I guess this is it for now.

Hope you like it and share your opinion below.


Posted in JAVA. Tags: , , , , . 1 Comment »

First Day As A Google Reader JAVA API Contributer

[ This Blog Was Moved To : http://www.diknows.com ]

Actually I am more of celibarating than announcing my first contribution to The Unofficial Google Reader APIs Java implementation. Today, I proudly commited some code to this great project that helped me out in my latest project.

I Added the following functionalities :

  • Ability to get all unread items in a label.
  • Ability to mark all unread items in a label as read.
  • Ability to get Google User ID.
  • Ability to edit a subscription to be marked to a specific label.

I hope someone benifits my additions, as I gained many benifits from this great project.
And Big thanks to Sebestian ( The project owner )