Difference Between Java and Python / JavaScript

Naveen AnamaneniUpdated · 5 min read

You may already know a little Python from school or college, or some JavaScript from building a web page. Comparing Java with a language you know makes Java easier to learn. It also helps you answer a common question in interviews and in your own career planning: "Why Java?"

The Same Program in Three Languages

Here is one small task in all three languages: add up the prices of items in a shopping cart. Switch between the tabs to compare them.

CartTotal.java▶ Try it
public class CartTotal {
    public static void main(String[] args) {
        int[] prices = {120, 80, 45};
        int total = 0;
        for (int price : prices) {
            total = total + price;
        }
        System.out.println("Cart total: " + total);
    }
}
Output
Cart total: 245

All three print the same result. Notice the differences in the Java version:

  1. All the code lives inside a class, CartTotal, and starts in the main method.
  2. Line 3 says the prices are an array of int, whole numbers. Line 4 says total is an int too. Python and JavaScript don't ask you to write types.
  3. Lines 5 to 7 add each price to the total, one by one. You'll learn this kind of loop in "Java for Loop".
  4. Every statement ends with a semicolon.

Try it yourself: in the Java version, add a fourth price, 200, to the list on line 3. What total should it print?

Types Are Checked Before the Program Runs

In Java, if you put the wrong kind of value into a variable, the program doesn't even compile. This code tries to store text in a number variable:

TypeCheck.java
public class TypeCheck {
    public static void main(String[] args) {
        int quantity = "five";
    }
}

The compiler stops you, and tells you the exact line:

Compiler output
TypeCheck.java:3: error: incompatible types: String cannot be converted to int
        int quantity = "five";
                       ^
1 error

In Python or JavaScript, a similar mistake is only found when that line runs, which may be in front of a user. In a project with hundreds of files and many developers, catching these errors early saves a lot of time.

When a type mistake is found

The Three Languages Compared

TopicJavaPythonJavaScript
TypingStatic: checked by the compilerDynamic: checked while runningDynamic: checked while running
How code runsCompiled to bytecode, run by the JVM with a JIT compilerUsually run by the CPython interpreterRun by a JavaScript engine in the browser or in Node.js
SpeedFast for long-running serversSlower for heavy calculations, but libraries written in C helpFast in modern engines
Amount of codeMore code to writeVery shortShort
Running tasks in parallelThreads and virtual threadsStandard CPython limits threads running Python code in parallelOne main thread, with an event loop and web workers
Typical usesEnterprise back ends, banking, Android, big dataData science, AI and ML, scripting, automationWeb pages, web apps, Node.js servers

Which One Should You Learn?

It depends on the work you want to do:

  • For back-end jobs in banks, product companies and IT services, Java is one of the most requested languages.
  • For data science and AI, start with Python.
  • For front-end web development, you need JavaScript.

Learning Java first has one big advantage: its strict rules teach you to think clearly about types and structure. Picking up Python or JavaScript afterwards is usually easy.

Common Mistakes

Thinking more code means a worse language. Java's extra words, such as types and classes, are information the compiler and other developers can use. In large projects, that clarity pays off.

Thinking Python is always slow and Java is always fast. Python's data libraries do their heavy work in fast compiled code, and a badly written Java program can be slow. Speed depends on the job and the code.

Treating Java and JavaScript as related. Apart from some syntax that both borrowed from C, they are separate languages with different designs.

Practice

  1. Easy: For each job, name the language you would pick first: an online exam website's server, a Python-style data analysis of exam marks, and the exam page's countdown timer in the browser.
  2. Harder: Change the Java CartTotal program to also print the number of items in the cart. Hint: an array has a length that you can print with prices.length.

Quick check: Java vs Python and JavaScript

4 questions
1What does "statically typed" mean?
2What happens when you compile int quantity = "five"; in Java?
3Which language is most used for front-end web pages?
4Which is a common use of Java?

Key Takeaways

Was this tutorial helpful?

Lesson 8 of 482