Sunday, September 6, 2009

A tour around Java in a Fortnight-Day 1-Why Java?

What Java does not have?

No functions in Java (Only Methods)
Function vs. Method
1. C++ functions could be present inside and outside the class.
2. But java methods could be written inside classes only.

No pointers in Java
1. Pointers lead to confusion.
2. Pointers crash program easily. (Addition of 2 pointers, Runtime memory leakage-dangling pointers)
3. Pointers break security.

No operator overloading in Java
1. Operators are immutable in java.
2. Operator’s meaning cannot be changed in java.

No preprocessor directives in Java

No multiple inheritance in Java

No static memory in Java

No destructors in Java

No goto statement in Java

Well, there seems to be more features available in C and C++ than in java. Why learn Java then? Doesn’t your brain ask this question? My brain did ask.
And I began searching for the answer.
First we have to compare C and Java to understand how the languages work.

Here is a small comparison between C and Java
C vs. Java




C





Java

So Java is powerful mainly because, of the byte code. It could be interpreted by the Java Virtual Machine. So based on the system, different JVMs are used. But the class file remains the same unlike C's exe file. It’s quite an interesting fact that JVM was written using C.

There is a big table with all the differences between C and Java mentioned in it. I would suggest you to skip it if you feel it to be too big.

Click here to view that table.

C uses a compiler or an interpreter (only one at a time).
Java uses both compiler and interpreter together.

In addition to this it uses a pretty interesting JIT. Now what does this JIT do?
Wikipedia gives the following definition:

In computing, just-in-time compilation (JIT), also known as dynamic translation, is a technique for improving the runtime performance of a computer program. JIT builds upon two earlier ideas in run-time environments: byte code compilation and dynamic compilation. It converts code at runtime prior to executing it natively, for example byte code into native machine code. The performance improvement over interpreters originates from caching the results of translating blocks of code, and not simply reevaluating each line or operand each time it is met. It also has advantages over statically compiling the code at development time, as it can recompile the code if this is found to be advantageous, and may be able to enforce security guarantees. Thus JIT can combine some of the advantages of interpretation and static (ahead-of-time) compilation.

The paragraph does not make complete sense to you unless you already know about JIT ;-).

Now let me try to put it simple.

Consider the following code:

for(i=1;i<=10;i++)
print i

It is a program to print from one to ten.

Normally, a compiler will convert the print i code ten times (say it takes ten
nanoseconds).

JIT Compiler will load print i at a place in the memory with an increment in it
and call it ten times (it takes just 2 nanoseconds).

Convert print I --------> 1 nanosecond.
Load it in memory -----> 1 nanosecond.

Coming back to our comparison, C vs. Java, I think I have told you enough for today. And as for C++ vs. Java, that’s covered in what Java does not have itself.

I have told you what Java does not have?
Why Java does not have those features is the question you have to think upon. Keep meditating till the next post.

No comments:

Post a Comment

Thanks for commenting in my blog...