Tuesday 7 August 2012

Java and Python

the following info was flicked from 

Py and Ja 

1) In Py

>>> a = 10
>>> a = "harish"
>>>a
harish

2) In Ja 
class h {
public static void main(String[] args){
String a = "harish";
a = 7;
System.out.println(a);
}

}
o/p: 
a = 7;
    ^
  required: String
  found:    int
1 error

This difference makes
Python being called as dynamically typed language and java being statically typed language 

The explanation is given in the blog mentioned above .
String a ;  => declaration binds a variable to a type 
a = "harish" ; => definitions binds it to an object . object here is "harish" , now the system checks the type of the object "harish" . if its is a string then , it okays , or else it flings an error .
whereas in py 
the variable is bound only to object and no type .
so it can be changed whenever wanted and wherever wanted 

I believe :
actually because java has something called compilation and then running  . There is a compulsion that the variables need to be type referenced so that during run time it gets the object . 
Where as in python , there is no such compilation , so only running , so only objects need to  be assigned to variables and and as there is not type which is associated with the variable(because there is no prechecking like compilation ), we can change the object the variable is bound to . 

this makes Python a dynamically typed language and java a statically typed language