Declaring and Initialising Variables
In Python, you can declare and initialise variables in the following formats:
- age = 5
- height = 1.75
- name = “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 = 5;
- double height = 1.75;
- String name = “john”;
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”;