Declaring and Initialising Variables
In Python, we can declare and initialize variables using the
following formats:
x = 5
y = 1.75
z = “john”
Python is smart enough to know that the data type for age is
int, height is float and name is str.
In Java, you declare and initialise variables in the
following formats:
int age = 8;
double height = 1.79;
String name = “sam”;
You have to state the data type of the variable as part of
variable declaration. (Take note of the semi-colon at the end of each
statement).
One Important point
to note:
For python, you can change the data type of a variable. For
example,the same variable a can be used to store the value 1 (an integer) and
the value “john” (a String).
This is not allowed in Java. If you have declared the
variable a as an integer.
int a = 1;
You will get an error if you attempt to do the following:
a = “john”;
No comments:
Post a Comment