Question: i am new to java.
i wanted to know this.
what is the need to create the .class file in java ?
can't we just pass the source code to every machine so that each machine can compile it according to the OS and the hardware ?

Answer: 
I believe it's mostly for efficiency reasons. 

From wikipedia <http://en.wikipedia.org/wiki/Bytecode>:

> 
> Bytecode, also known as p-code (portable code), is a form of
>  instruction set designed for efficient execution by a software
>  interpreter. Unlike human-readable source code, bytecodes are compact
>  numeric codes, constants, and references (normally numeric addresses)
>  which encode the result of parsing and semantic analysis of things
>  like type, scope, and nesting depths of program objects. **They
>  therefore allow much better performance than direct interpretation of
>  source code.**
> 
> 
> 

(my emphasis)

And as others have mentioned possible weak [obfuscation](http://en.wikipedia.org/wiki/Obfuscation) of the source code.
The main reason for the compilation is that the Virtual Machines which are used to host java classes and run them only understands bytecode
And since compiling a class each time to the language the virtual machine understands is expensive. That's the only reason why the source code is compiled into bytecode. 
But we can also use some compilers which compiles source code directly into machine code.But that's a different story which I don't know about much.