Variable Declaration in Java & Python

This post is part of a series that will teach you to use Python if you come from Java. I highly recommend you start from here if you don’t know o only has a little bit of Python knowledge. Let’s begin.

We already review the Key differences between Java and Python. the file extension, file structure, the entry point, package and structure, compilation and interpretation, etc. Let’s review the Variable declaration in Java and Python.

First we need to remember that Java is explicitly typed and Python is dynamically inferred. So, there are rules for the variable declaration.

Variable declaration

In Java we need to specify the variable’s type, and the type can’t change. In Python we don’t need to specify the type, because it’s dynamically inferred and the type can change.

So, in Java we have the next variable declaration syntax:

type variable_Name = value;

For example, a declaration of a int variable.

int number = 15;

We use the type, the name of the variable, and then we assign a value using the equals sign “=”. Don’t forget the semicolon “;” in the end of the line.

Java has primitive types: byte, short, int, long, float, double, char, and boolean. And you only need to change the type for the required type.

In Python in other hand, you only provide the name and assign a value. Python will inferred the type.

number = 15;

String variable declaration

In Java String is an special object. Is the only object that don’t need to use the new operator and the object constructor.

The syntax is the same than the primitive ones, but if we assign a value, it’s must be surrounded by the double quotation mark ().

String name = "Aran";

Python follows the same syntax, variable name and assign the value, and the value must be surrounded by the double quotation mark () too.

name = "Aran"

Object variable declaration

Java is an OOP language, that means Java uses variables to store object references. The syntax is equals than the primitive variables, but we use the new operator and the object constructor when we create new objects.

type variable_Name = new type();

The variable type is a Java class or a Class defined by you. Let’s review a few examples:

Object obj = new Object(); 
Cat cat = new Cat();
Person person = new Person();

For Python, all the variables are objects, but in the case of the Python classes, we use the name of the class and constructor.

obj = Object()
cat = Cat()
person = Person()

Now, you now how to declare variables in Python. There still some variables o structure declaration like the array, maps, etc. But at least now you know how to declare and use variables.

Note: in Java, when we are talking about syntax we have some values in square brackets “[]”. Those values means that they are optional and is not necessary to add them at least that you need it.

[access_modifier] [static_modifier] [final_modifier] tipo variable_Name = valor;

Summary Table

Summary table that acts as cheat sheet for the variable declaration between Java and Python.

FeatureJavaPython
Variable DeclarationRequires specifying the type explicitly.No need to specify the type, dynamically inferred.
Example of Integerint number = 10;number = 10
Example of StringString name = "Alice";name = "Alice"
Primitive vs. Object TypesDifferentiates between primitive and object types.Everything is an object, including primitive types.
Type InferenceStatic, types must be declared and can’t change.Dynamic, types are inferred at runtime and can change.
Type SafetyEnforces type safety at compile time.Type is flexible, but runtime errors may occur.
ReassignmentCan’t change type once declared (int stays int).Can change the type of a variable after assignment.

Happy Learning!!!

Leave a comment