Tuesday, September 8, 2009

Send mail to all your friends in gmail

Recently, I had to send a mail to all my friends.
So I was searching for a method to do it.
That's when I found this pretty useful video in youtube!

Monday, September 7, 2009

A simpler way to find the sum of n digits

Today, I was speaking with my Internet Programming lecturer, Mr. Gupta. He was sharing his experience on teaching C to the first year mechanical class. He said, he couldn't explain the logic of adding sum of n digits to a student. He employed the usual strategy of leaving out the nines in the number. (The student didn't know that simple logic it seems!)

That's when this idea struck me. If you have a number, say x.
It shall be represented as (9*n)+y.

If we identify y then that's the sum of digits.

Say the number is 103

It's simple 99+4, (9*11)+4 hence 4.

y is nothing but (9+9+9+9....11 times) +4
So we skip all those nines and take the 4 alone.

In simpler words if x is the number then (x modulus 9) is the sum of the digits.
So in a programmer's perspective its just x%9.

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.

A tour around Java in a Fortnight-Introduction



I had been spending my last fortnight with Java. As I was studying, I understood its power and the flexibility it gives to a programmer. Though I have learnt Java in bits and pieces, I have not learnt it from the basic (ie., from the origin, history, architecture,..). In my college, we students have not been attending the classes for the past one week due to the fear of getting infected from swine flu in the campus! This has been like a blessing in disguise, I was able to peacefully explore Java. As I was exploring Java, I got an idea of making a small tutorial for people like me who have learnt Java in bits and pieces.

I will be posting my tutorial here in my blog once in a while! It is a tedious process so there is no fixed timeline on my tutorial posts. Continue your support.