Variables Python vs Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Variables Python vs Java

I've heard many times, but dont understand clearly, what are the cons and pros in Python variables can be reassigned to various types, and say in Java,- variables need to be predefined with dataypes? Any example or use case of benefit of one or another? Thank you.

17th May 2017, 3:30 AM
Dmitry Baltunou
Dmitry Baltunou - avatar
2 Answers
+ 8
Adding to @Rrestoring faith's answer, IMO one of the cons for Python when you don't need to specify datatypes during variable declaration, would be that it won't be able to distinguish between declaration and usage of a variable. This however, isn't the case for JS, due to the existence of 'var' keyword.
17th May 2017, 4:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Some Pros/Cons for not having to declare datatype, like in Python (Also called Dynamic Type Binding). Cons/ Difficulty to distinguish every variable type from another. Specially if you have lots of variables of different types. Higher cost to check the type. Each variable type must be determine by checking the type it was assigned. Pros/ Easier for generic code. Writing things that may require use of multiple different types of variables.
17th May 2017, 3:51 AM
Rrestoring faith
Rrestoring faith - avatar