How to use power of two programming Languages?

Hey guys, what is going on? Hope so all fine, today i am back with this new article. I am sure you will enjoy today's article. This article is about Java and Python, but more about getting extra knowledge. So, If you are not familiar with Java and Python then also keep reading.
   I always have been curious to know that how Google and other big companies develop lots of softwares and services using multiple programming languages in one project. They use combination of two programming languages like Java-Python, Cplusplus-Python etc. Why do they combine two programming languages?. You already know that that all languages have their different pros and cons and this is the main reason why they combine languages. If you don't know how to do it? don't worry, you will have good idea about it after reading this article. You are going to get knowledge about one of the cool stuffs you can do with programming, So let's start.


Why do we need to do this?
  • To improve strength
  • To increase quality
  • To increase Utility
  • To improve performance
  • Involve advantages of each individual language
  • Less code
  • Time saving 

Where we can use this?

This is mostly used in Big projects because they have to provide lots of services and different languages have their own pros and cons. This makes codes stronger and even this is the best way to programming. If you know 2 language All right? And you are master in 1 language but you know also second language which can you use. Then you can implement that and combined language gives ultimate performance.

Methods

  • Jython (Java implementation of python)
  • Jpype (Allow python program to full access java class)
  • Pyjnius (Using java in python)
  • Py4j (Getting access of java objects in python)

These, all are very famous methods for integrating Java and Python. We will discuss one by one. I will show you in details so you can get it properly. So, let's start with our first method Jython.

1. Jython:-
Jython is pure implementation of python. Jython code can execute on JVM. Jython is dynamic scripting language which runs on JVM and supports both libraries java and python. Features of Jython is interactive interpreter which we basically used in python IDLE. Jython is using High level data structure. Jython is platform independent. You all know that python is very easy to learn because it is using very simple and easy syntax. Jython is a implementation of python so indirectly Jython is also lots of easier to learn and understand and using java's capabilities it becomes lot powerful then python.

All right now let's move on our small example.

>>>from java.util import Random
>>>r =Random()
>>>r.nextInt()

This will generate random number. This is just one example to show implementation of functionalities of Java into python.
I know one example is not enough, You want one more? All right let's look for new example

Jython>>>import javax.swing as swing
                  Win.swing.JFrame("Call of techies Frame")
                  Win.size=(400,600)
                   Win.show()
Now,this will create one frame, in this program we called java's swing functionalities for making frame. You can see we wrote program same as we write in python. This is called Jython.

2. Pyjnius:-
It is a library of python which helps to access java class into python. So, now let's directly move onto our example.

In this example i am calling StringJoiner class from java.
code:
from jnius import autoclass
Stringjoiner=autoclass('java.util.Stringjoiner')
stringjoinervar=StringJoiner()

stringjoinervar.add("Call")
stringjoinervar.add("Of")
stringjoinervar.add("Techies")
stringjoinervar.toString()

Output: 
CallOfTechies

This example is about how to implement java classes into python. We called StringJoiner class from java which joints string into one line. This is just example of how you can call. You can use other classes also as per your need.


3. JPype:-
JPype allows python programs to full access java classes. JPype is not a re-implementation like Jython. Let's look on very small example which shows sample program to demonstrate JPype.

Code:
from jpype import *
startJVM(getDefaultJVMPath(),"-ea")
java.lang.System.out.println("Call Of Techies")
shutdownJVM()

In the above code, we imported jpype and first we connected our system to JVM and then we printed out call of techies and then again shutdown our JVM.


4. Py4j:-
Py4j is used to access java objects from python. In py4j you can also call back python objects to java programs.

Code:-
>>>from py4j.java_gateway import JavaGateway
>>>gateway=Javagateway()
>>>cot1=random.nextInt(50)
>>>cot2=random.nextInt(50)
>>>print(cot1,cot2)

In the above code, we imported java gateway to give access python program to java objects and then we called random method to print out two random number which is named as cot1 and cot2.

So, guys these are methods to get advantages of both programming languages. If you want to know about other methods then tell me in comments, I will share my knowledge with you.

Wanna read: How To Learn Programming Smartly?

Post a comment

0 Comments