Operators of Java and PHP
This is the third part of the Introduction to Java and PHP series. Arithmetic operators of Java and PHP will be discussed. Whenever possible, code examples will be made in a similar manner for both Java and PHP.
Operators in Java and PHP are very similar. Variables in Java and PHP can declared and assigned using operators at the same time.
In this tutorial, Java and PHP will be used to create a program consisting of integers and decimals expressions. The program will be compiled and executed. In order to make the work-flow faster, please use a programming editor.
- Tools are required:
- Text editor for creating and modifying the Java and PHP files.
- Java.
- PHP.
Optional Download and install suitable text editor
Any text editor will work. A bare minimum should be syntax-highlighting for Java and PHP. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.
Lesson 11: Declare Arithmetic Variables
Java | PHP |
---|---|
int num1 = 9; // Integer int num2 = 3; // Integer |
$num1 = 9; // Integer $num2 = 3; // Integer |
Java also requires the variable’s first letter to be lowercase.
PHP only requires the variable to have a dollar sign at the beginning.
Lesson 12: Basic Math
Java | PHP |
---|---|
Addition | |
int sum = num1 + num2; // Addition + | $sum = $num1 + $num2; // Addition + |
Subtraction | |
int difference = num1 – num2; // Subtraction – | $difference = $num1 – $num2; // Subtraction – |
Multiplication | |
int product = num1 * num2; // Multiplication * | $product = $num1 * $num2; // Multiplication * |
Division | |
int quotient = num1 / num2; // Division / | $quotient = $num1 / $num2; // Division / |
Modulus | |
int remainder = num1 % num2; // Modulus % | $remainder = $num1 % $num2; // Modulus % |
The addition sign for both Java and PHP is the same “+”. The minus sign for both Java and PHP is the same “-“. The multiplication sign for both Java and PHP is the same asterisk “*”. The division sign for both Java and PH is the same forward slash “/”. The modulus sign for both Java and PHP is the same percent “%”.
Lesson 13: Increment
Java | PHP |
---|---|
Postfix Increment | |
num1 = num2 + 1; // Postfix Increment +1 num1 ++; // Postfix Increment ++ |
$num1 = $num2 + 1; // Postfix Increment +1 $num1 ++; // Postfix Increment ++ |
Prefix Increment | |
num1 = 1 + num2; // Prefix Increment 1+ ++ num1; // Prefix Increment ++ |
$num1 = 1 + $num2; // Prefix Increment 1+ ++ $num1; // Prefix Increment ++ |
Postfix increment adds one to the variable. Postfix decrement removes one from the variable. Both Java and PHP use the same postfix and prefix operators.
Lesson 14: Assignment Shortcuts
Java | PHP |
---|---|
Assignment By Sum | |
num1 = num1 + num2; // Assignment by sum num1 += num2; // Assignment by sum += |
$num1 = $num1 + $num2; // Assignment by sum $num1 += $num2; // Assignment by sum += |
Assignment By Difference | |
num1 = num1 – num2; // Assignment by difference num1 -= num2; // Assignment by difference -= |
$num1 = $num1 – $num2; // Assignment by difference $num1 -= $num2; // Assignment by difference -= |
Assignment By Product | |
num1 = num1 * num2; // Assignment by product num1 *= num2; // Assignment by product *= |
$num1 = $num1 * $num2; // Assignment by product $num1 *= $num2; // Assignment by product *= |
Assignment By Quotient | |
num1 = num1 / num2; // Assignment by quotient num1 /= num2; // Assignment by quotient /= |
$num1 = $num1 / $num2; // Assignment by quotient $num1 /= $num2; // Assignment by quotient /= |
Assignment By Modulus | |
num1 = num1 % num2; // Assignment by modulus num1 %= num2; // Assignment by modulus %= |
$num1 = $num1 % $num2; // Assignment by modulus $num1 %= $num2; // Assignment by modulus %= |
Java and PHP use the same assignment by sum, by difference, by product, by quotient and, by modulus.
ArithmeticOperators.java file
/** * Ojambo.com Arithmetic Operators Java Tutorial * ArithmeticOperators.java Copyright 2011 Edward * http://www.ojambo.com * */ // This class displays Arithmetic Operators public class ArithmeticOperators { //This class is called ArithmeticOperators public static void main(String[] args) { // Main Method // Declare variables and assign values immediately int num1 = 9; // Integer, 32-bit, called num1 int num2 = 3; // Integer, 32-bit, called num2 // Basic Math Addition int sum = num1 + num2; // Addition +, num1 plus num2 // Basic Math Subtraction int difference = num1 - num2; // Subtraction -, num1 minus num2 // Basic Math Multiplication int product = num1 * num2; // Multiplication *, num1 times num2 // Basic Math Division int quotient = num1 / num2; // Division /, num1 divided by num2 // Basic Math Modulus int remainder = num1 % num2; // Modulus %, remainder of num1 by num2 // Postfix Increment num1 ++; // Postfix Increment ++, num1 plus one // Prefix Increment ++ num1; // Prefix Increment ++, one plus num1 // Assignment By Sum num1 += num2; // Assignment by sum +=, num1 plus num2 // Assignment By Difference num1 -= num2; // Assignment by difference -=, num1 minus num2 // Assignment By Product num1 *= num2; // Assignment by product *=, num1 times num2 // Assignment By Quotient num1 /= num2; // Assignment by quotient /=, num1 divided by num2 // Assignment By Modulus num1 %= num2; // Assignment by modulus %=, remainder of num1 by num2 // Print out the assigned variables; System.out.println("num1 is " + num1); // Print the variable num1 System.out.println("num2 is " + num2); // Print the variable num2 System.out.println("sum is " + sum); // Print the variable sum System.out.println("difference is " + difference); // Print the variable difference System.out.println("product is " + product); // Print the variable product System.out.println("quotient is " + quotient); // Print the variable quotient System.out.println("remainder is " + remainder); // Print the variable remainder } }
The ArithmeticOperators.java file can be compiled as javac ArithmeticOperators.java and executed with java ArithmeticOperators.
ArithmeticOperators.php file
<?php /* * Ojambo.com Arithmetic Operators PHP Tutorial * ArithmeticOperators.php Copyright 2011 Edward * http://www.ojambo.com * */ // Declare variables and assign values immediately $num1 = 9; // Declared when assigned, called $num1 $num2 = 3; // Declared when assigned, called $num2 // Basic Math Addition $sum = $num1 + $num2; // Addition +, $num1 plus $num2 // Basic Math Subtraction $difference = $num1 - $num2; // Subtraction -, $num1 minus $num2 // Basic Math Multiplication $product = $num1 * $num2; // Multiplication *, $num1 times $num2 // Basic Math Division $quotient = $num1 / $num2; // Division /, $num1 divided by $num2 // Basic Math Modulus $remainder = $num1 % $num2; // Modulus %, remainder of $num1 by $num2 // Postfix Increment $num1 ++; // Postfix Increment ++, $num1 plus one // Prefix Increment ++ $num1; // Prefix Increment ++, one plus $num1 // Assignment By Sum $num1 += $num2; // Assignment by sum +=, $num1 plus $num2 // Assignment By Difference $num1 -= $num2; // Assignment by difference -=, $num1 minus $num2 // Assignment By Product $num1 *= $num2; // Assignment by product *=, $num1 times $num2 // Assignment By Quotient $num1 /= $num2; // Assignment by quotient /=, $num1 divided by $num2 // Assignment By Modulus $num1 %= $num2; // Assignment by modulus %=, remainder of $num1 by $num2 // These statements print the assigned variables echo "num1 is".$num1; // Print out the $num1 variable echo "num2 is".$num2; // Print out the $num2 variable echo "sum is".$sum; // Print out the $sum variable echo "difference is".$difference; // Print out the $difference variable echo "product is".$product; // Print out the $product variable echo "quotient is".$quotient; // Print out the $quotient variable echo "remainder is".$remainder; // Print out the $remainder variable ?>
The ArithmeticOperators.php can be compiled as php -l ArithmeticOperators.php and compiled with php ArithmeticOperators.php
Conclusion:
Java variables must begin with a lowercase letter. In Java, every variable must be specifically declared while PHP only requires a dollar sign. Both Java and PHP share the same arithmetic operators.
- Recommendations:
- This tutorial uses an optional lightweight programming editor with syntax-highlighting features.
- The concepts taught in the first fourteen lessons can be applied to other programming languages.
- When selecting a programming language to master, pay close attention to license restrictions.
- Java and PHP Series:
- Ojambo.com Lightweight Programming Editors.
- Introduction to Java and PHP Part 1.
- Primitive data types for Java and PHP Part 2.
- Arithmetic Operators for Java and PHP Part 3.
- String Objects for Java and PHP Part 4.
- Control Structures for Java and PHP Part 5.
- Iteration Loops for Java and PHP Part 6.
- Array Types for Java and PHP Part 7.