BlogsDope image BlogsDope

A brief introduction to Java — For a Python programmer

Sept. 6, 2018 JAVA PYTHON 439
Download Our App.
BlogsDope App
Get it on Google Play

INTRODUCTION

Contrary to the zen of Python, people writing Java code do not agree that beautiful is better than ugly. Java is fully object oriented. That means, every piece of code is written using the concept of objects and classes.
Of course we use classes and objects in Python too, but that’s not always necessary. However, everything in Java is dealt in classes and objects.


We know that

print “Hello World”

prints “Hello World” in python. But can’t we make the same program look like this :—

class Hello:
  def printer(self):
    print("Hello World")


if __name__ == "__main__":
    Hello().printer()

Yes, we can. And in this program, there is a class called Hello that contains a method called printer.
Later on, we called printer() method on the Hello() class.
(You can also create an object of Hello class and then call the printer function to get the same result).

That’s the way Java works. Following is the “Hello World” program in Java:

class Hello{
public static void main(String[] args){
    System.out.println(“Hello World!”);
  }
}


Also, in Python, we have this built-in function called print. In Java, there is class called ‘System’, whose members are ‘in’, ‘out’, etc. We apply the ‘println’ method on the ‘out’. Guess what? We use the ‘in’ within the ‘System’ class for taking inputs.

BASIC SYNTAX

Constructor in Java does the same job as __init__() in Python. Constructor in Java is a method that:
• Must have the same name as the class
• Must not return anything.
Let us create an Animal class in Java.

class Animal{
String Name;
public Animal(String name){
    this.Name = name;
}
public static void main(String[ ] args){
    Animal dog = new Animal(“Tommy”);
    Animal cat = new Animal(“Jimmy”);
    System.out.println(dog.name);
    System.out.println(cat.name);
  }
}
‘this’ keyword is referencing to the objects of the Animal class, it pretty much does the same job as ‘self’ in Python.
A constructor is always created when you create an object of a class. Also, note that we use the ‘new’ keyword to create a new object of some class.

 

We need to declare the type of data before we use them in Java. Summing two numbers goes like:

int a = 5;
int b = 7;
int c = a + b;

When we write int var, some memory is allocated for that var. Other data types are byte, short, long, float, double, char with there obvious meanings. It must be noted that Integer, Byte, Short, Long, Float, Double, Character are classes AND NOT data types.
These classes have their special set of methods like any other class in Java.

We can create an object of the Integer class like this:

Integer x = new Integer(4);

Having an Integer or such classes in Java give you access to many nice methods you can use over their objects. For example, we can call a method called toString() on x.
Also, note that String is also a class, and not a data type. There is no primitive data type for strings. However, Java has given special importance to Strings, so the String keyword CAN be used like int, and other data types.

There are many more differences in the two languages. However, this was a brief introduction to the same.

You should go through the Java course for more. 


Liked the post?
Contributor
Editor's Picks
0 COMMENT

Please login to view or add comment(s).