Wednesday, March 20, 2013

Behavior of primitive type int with 0


We all use int as a primitive type in java. Like

int abc = 7; // which is perfectly fine

Lets say we declare,

int abc =007; // this is also fine

but if we declare

int abc = 008; or int abc = 009; ...--- Compilation error. Why ?

Because when we append zero before integer , compiler reads it as an Octal (base 8) literal.


What will be the output of following statement ?

1) System.out.print("Hi" + 007 ); 

2) System.out.print("Hi" + 008);

3) System.out.print("Hi" + 009 ); 


4) System.out.print("Hi" + 010);


Answers:


1) Hi7
2) Compilation error
3) Compilation error
4) Hi8







Friday, March 8, 2013

New features of Java


Java is the most commonly used enterprise langauge in the industry of software development, it's not because it is the best. There are many things missing in java or you can say features easily available in other languages but not in java. I would like to add few here for my friends who are working on java.

1) Generics - Lets talk about generics , Java introduced generics in version 5, where as this was already available in other languages like C# . Java generics are completely compile time , Generics in Java follows the principle of Type erasing, which basically means that when the java file is converted into byte code , it doesn't contains the information of generics. If you try to decompile the file and you will get a non generic java code. The reason provided by Java guys is that to make it backward compatible, but what about performance. And why do we even need backward compatibility in the versions of Java. Those who want to move to a newer version should use better and latest features.

Lets look by an example :

List<String> list = new ArrayList<>(); (This feature introduced in java 7 release)

This line of code when converted to byte code is equal to

List list = new ArrayList();

So what about reflection and performance. If you try to fetch java methods using reflection , it wont show you the generic types.

This is not the end, even the wildcards used in java are pretty confusing. I have very rare seen a Java developer who likes to use wildcards in collections.

2) Interfaces : Interface is a term that is introduced in java  : That it defines the design declaration, and who-ever implements it implements it fully. Interface actually forces to implement all its methods even though if not required.
Let say ,  an Interface with 5 methods, what about a class that wants to implement only 3 but it forces it to keep other 2 also in the class or else declare the class abstract and doesn't allow to create objects.

3) PassbyValue : Yes, we know java is passbyvalue , but when we pass a primitive type and pass an object , it got very confusing. It says, Primitives are pass by value and Objects are not passed by reference but object references are passed by value. So , you need to clearly understand that its the object reference that are passed by value.

4) Closures : There is a long debate on inclusion of Closures in java but I hope they will be part of Java 8.

5) Weak Java API : Java API itself is very weak that sometimes we have to take support of many open APIs and choosing among them itself becomes a tough job.
 
6) Java 7 new feature : Multiple Exception blocks, Autoclosables in try statement, and strings in switch are some of the new features of Java 7 , which are trying to make java compete with other languages.

There are many more things I could say but that could make me a critic.
I am not against Java but I feel all of us working on java should have some idea about the weak points of the language we are in.