Java for Python Developers 1 – Basics

The intention of this blog is to talk about the similarities and differences between Python and Java. This will be a series of blogposts, this being the first. Through this blog I plan to help myself and other Python developers learn Java easily by relating it to similar concepts in Python. The focus of this post will be on learning Basic concepts and writing some simple code in Java.

Why learn Java?

Java is a very popular and a widely used Programming language. It is used in building large systems due to it’s powerful JVM that leads to faster execution. When an Application needs to scale-up, Java is the most popular choice in the industry.

It supports Concurrency.

Java has a good cross platform support.

It has a very strong IDE support making the developer’s life easier.

It imposes certain best practices. There is most likely, a single way of doing stuff and hence a bad programmer can only harm so much. But in case of languages like Python, there are several ways of writing a piece of code and the onus is completely on the developer to use the most optimized one.

Classes and Objects:

The concept of Classes, Objects and Object Oriented programming is similar to Python. But here’s the catch:

Each .java file can only have a single public class that contains the main function. This is to know the exact point in the file from which the application is supposed to be launched. The name of this public class should be same as the name of the .java file.

Variables:

Again, the concept is very similar to Python including the naming conventions.

Java cares about Type. An Elephant cannot be put in a basket meant for vegetables. This is very much unlike Python, which decides if we need a Basket or a Cage based on whether the source is an Elephant or a vegetable that we are dealing with. 🙂 Moreover,

variables declared as final cannot be reassigned, once assigned to some value.

A variable of a particular type can be assigned to another of the same type. Example, a variable of the type Tiger (Tiger t1 = new Tiger()) can be assigned to any another variable of the type Tiger. (Tiger t2 = new Tiger(); t2 = t1;)

Java is PASS-BY-VALUE.

Global Variables in Python can be used as global x = 3. Java has no concept of global variables as such. However, a variable marked as public, static and final acts as a globally available constant.

Conditional Statement:

if(var1 == var2) { <do this>
}

Iteration:

WHILE Loop:

Use while loop when we do not know the exact number of times to loop, in advance. In other words, when we have to loop until a condition is satisfied.

Java Library or Java API

Let’s now writing some simple code in Java to achieve certain redundant tasks using the built-in Libraries.

ArrayList:

ArrayList can perform some frequently used operations on Lists which otherwise takes multiple lines of code and iterations to achieve the same. Similar to the built-in functions for a List in Python, elements can be added to the list, removed, check if an element is present in a list, get the size of the list, get the index of an element etc. as follows:

Let’s play Battleship

Here’s an example program, the battleship game written in Java. We will play this on command-line. The game randomly assigns 3 Battleships on a 7×7 board. It allows the user to guess the location. On successful identification of each ship, it says “kill”. When all the ships sink, games ends with a score.

There may be better ways of writing this code but at this stage, we are just trying to get a step closer to learning Java. Here’s an implementation of the battleship game.

Conclusion:

In this blogpost we learnt how to use Variables, write Classes, conditional statements, loops in Java. We also glanced at some basics APIs or built-in libraries in Java that help perform certain redundant operations easily. We also put these concepts in action by coding a game.