Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The comparison operator for numbers are the same as in Python. For built-in types, instead of == in Python, Java uses equals method.  Both languages have a type representing the logic values, true and false. Java calls the type boolean and uses the names (literals) true and false, whereas Python calls it Boolean and uses True and False. Note that in Python, the values 0 and "" will also be interpreted as false in a condition and all other values will be true, including True, of course.

Python's Boolean operators: and, or, and not are replaced by: and not have corresponding Java operators &&, || and and ! in Java.

Code Block
languagejava
titleLogical Operators in Java
boolean x = true;
boolean y = false;

// Output: x and y is false
System.out.println("x and y is " + (x && y));

// Output: x or y is true
System.out.println("x or y is " + (x || y));

// Output: not x is false
System.out.println("not x is " + !x);
Code Block
languagepy
titleLogical Operators in Python
x = True
y = False

# Output: x and y is False
print('x and y is',x and y)

# Output: x or y is True
print('x or y is',x or y)

# Output: not x is False
print('not x is',not x)

Negation is also formed differently between those two programming languages.

 

Code Block
languagejava
titleNegation Operators in Java
 ! (x > 0 && y > 0) || z > 0 ^ w > 0
Code Block
languagepy
titleNegation Operators in Python
not(x > is0 notand y > 0) or z > 0

In Java, most of the comparison operators, >, <, >=, and <=, can be applied only to numerical types, while in Python, the comparison operators can be applied to numbers, strings, and other types of objects, and compare values in some appropriate way (e.g. numeric order, lexical order) where possible. The equality operators, == and !=, can be applied to any object, but when applied to reference types they test for same (different) object rather than same (different) value.

Code Block
languagejava
titleStrings Negation in Java
int x = 10;
int myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) ...y = 12;

// Output: x > y is false
System.out.println("x > y  is " + (x>y));

// Output: x < y is true
System.out.println("x < y  is " + (x<y));

// Output: x == y is false
System.out.println("x == y is " + (x==y));

// Output: x != y is true
System.out.println("x != y is " + (x!=y));

// Output: x >= y is false
System.out.println("x >= y is " + (x>=y));

// Output: x <= y is true
System.out.println("x <= y is " + (x<=y));
myCounter = 0 myString = str(myCounter) if myString == "0": ...
Code Block
languagepy
titleStrings in Python
Comparison operators in Python
x = 10
y = 12

# Output: x > y is False
print('x > y  is',x>y)

# Output: x < y is True
print('x < y  is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)

 The comparison operators for numbers are the same as in Python. For class types, instead of == in Python, you should use the equals method in Java.