Java vs Python

Java vs python

  • Java is a statically typed whereas python is dynamically typed.
  • Java and Python both are high-level programming languages.
  • Python is an interpreted language whereas Java is a compiler interpreter language.
  • Java uses //  for a single line and /* */ for multiline comments, Python uses # for a  single line and “”” and ‘’’ for a multiline comment.
  • Java variables have to be declared before using a variable, You need not explicitly declare variables in python. We can directly assign a value and start using it.
  • Python used newline to end statements whereas java uses a semicolon to end the statement.
  • Java uses braces to denote blocks, Python uses indentation to denote blocks and uses a colon for the preceding line.
  • Java uses a single quote for a single character and double quotes for string constants. Python allows single or double quotes for string or character constants.
  • Both Java and Python use Boolean to represent true or false.

Operators

  • Both uses = to indicate assignment.
  • Java uses && and || and ! for logical operator. Python uses and  and or and not
  • Java uses a++, ++a, a=a+1, a+=1 to increment by 1.  Python uses a+=1 and a=a+1. We can’t uses a++ and ++a in Python
  • Java uses <, <=, >, >=, ==, != operators.  Python supports all these operatos and additionally python allows <> for !=.

Conditional statements

  • Java uses if, if else, else if ladder.
  • If (conditon)  { body }
  • If(conditon)  { body } else { body }
  • If (condition) { body } else if (conditon) {body}

      Python uses if, elif, else

          If   <condition>:

          <body intended>

          elif  <condition>:

        <body intended>

          else:

       <body intended>

 

Loops

    Both Java and python uses for and while loops but the syntax is different.

    Java

    For(declaration; condition; inc or dec)

    For(<Type> <var>; <sequence>) { body}

    While(cond) { body }

 

   Python

     For <variable> in sequence :

                <body indented >

     While <condition>:

                <body indented>

Arrays

Java arrays uses squarebrackets [ ] to denote array, and uses braces { } to display an array

In python arrays are called lists and are denoted by square brackets.

 

Get more details about Java and Python.