Primitive data types of Java and PHP Part 2

Primitive data types of Java and PHP

This is the second part of the Introduction to Java and PHP series. Similarities and differences between Java and PHP variables will be discussed. Whenever possible, code examples will be made in a similar manner for both Java and PHP.

Java assigns memory to variables based on the data type chosen. PHP assigns memory to the data type when it is set. Java and PHP allow variables to be declared and assigned at the same time.

In this tutorial, Java and PHP will be used to create a program consisting of integers, decimals, characters and boolean 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 5: Variables

Java PHP
Declare using lowercase such as byte myByte; Declare using dollar sign $ such as $myByte;

Java requires the declaration to be lowercase for the data type.
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 6: Integers

Java PHP
byte myByte; // Integer, 8-bit, -128 to 127
short myShort; // Integer, 16-bit, -32,768 to 32,767
int myInt; // Integer 32-bit
long myLong; // Integer, 64-bit
$myInt; // Integer

Both Java and PHP use the same types of Integers. Java requires declaration of the specific type of integer.

Lesson 7: Decimals

Java PHP
float myFloat; // Floating Point, 32-bit
double myDouble; // Floating Point, 64-bit
$myFloat; // Floating Point

Java has two types of declarations for floating point variables while the PHP variable is based on the assigned value.

Lesson 8: Characters

Java PHP
char myChar; // Character, 16-bit, ‘single quotes’ $char; // Character can use ‘single’ or “double” quotes.

Declaring characters differs in both Java and PHP. Java uses a specific char declaration while PHP uses the string variable.

Lesson 9: Boolean Expressions

Java PHP
boolean myBoolean; // Boolean, 8-bit, case-sensitive, true or false $myBoolean; // Boolean, case-insensitive, True or False

Both Java and PHP use the same assigned values for boolean variables. The minor difference is that PHP is case-insensitive.

Lesson 10: Assigning Values to Variables

Java PHP
myByte = 1;
myShort = 11;
myInt = 1111;
myLong = 123456;
myFloat = 11.11;
myDouble = 123456.123456;
myChar = ‘a’;
myBoolean = true;
$myByte = 1;
$myShort = 11;
$myInt = 1111;
$myLong = 123456;
$myFloat = 11.11;
$myDouble = 123456.123456;
$myChar = ‘a’;
$myBoolean = true;
Declare and assign at the same time
byte myByte = 1;
short myShort = 11;
int myInt = 1111;
long myLong = 123456;
float myFloat = 11.11;
double myDouble = 123456.123456;
char myChar = ‘a’;
boolean myBoolean = true;
$myByte = 1;
$myShort = 11;
$myInt = 1111;
$myLong = 123456;
$myFloat = 11.11;
$myDouble = 123456.123456;
$myChar = ‘a’;
$myBoolean = true;

Both Java and PHP variables can be assigned the same way. It is possible to declare and assign variables at the same time with Java and PHP. There are two differences between Java and PHP when assigning values to variables. In Java, boolean values are lower case but case-insensitive in PHP. Characters in PHP are assigned using either single or double quotes, but must be single quotes in Java.

PrimitiveDataTypes.java file

/**
 * Ojambo.com Primitive Data Types Java Tutorial
 * PrimitiveDataTypes.java Copyright 2011 Edward
 * http://ojambo.com
 * 
 */
 
// This class displays Primitive Data Types
public class PrimitiveDataTypes { //This class is called PrimitiveDataTypes
    public static void main(String[] args) { // Main Method
        // Declare variables and assign values immediately 
        byte myByte = 1; // Integer, 8-bit
	    short myShort = 11; // Integer, 16-bit
	    int myInt = 1111; // Integer, 32-bit
	    long myLong = 123456; // Integer, 64-bit
	    float myFloat = 11.11; // Floating Point, 32-bit
	    double myDouble = 123456.123456; // Floating Point, 64-bit
	    char myChar = 'a'; // Character, 16-bit, 'single quotes'
	    boolean myBoolean = true; // Boolean, 8-bit, true or false
            
        // Print out the assigned variables;
        System.out.println("myByte is " + myByte); // Print the variable myByte
        System.out.println("myShort is " + myShort); // Print the variable myShort
        System.out.println("myInt is " + myInt); // Print the variable myLong
        System.out.println("myFloat is " + myFloat); // Print the variable myFloat
        System.out.println("myDouble is " + myDouble); // Print the variable myDouble
        System.out.println("myChar is " + myChar); // Print the variable myChar
        System.out.println("myBoolean is " + myBoolean); // Print the variable myBoolean
    }
}

The PrimitiveDataTypes.java file can be compiled as javac PrimitiveDataTypes.java and executed with java PrimitiveDataTypes.

PrimitiveDataTypes.php file

<?php
/*
 * Ojambo.com Primitive Data Types PHP Tutorial
 * PrimitiveDataTypes.php Copyright 2011 Edward
 * http://ojambo.com
 * 
 */
 
// Declare variables and assign values immediately 
    $myByte = 1; // Declared when assigned
    $myShort = 11; // Declared when assigned
    $myInt = 1111; // Integer declared when assigned
    $myLong = 123456; // Declared when assigned
    $myFloat = 11.11; // Floating point declared when assigned
    $myDouble = 123456.123456; // Declared when assigned
    $myChar = 'a'; // String declared when assigned
    $myBoolean = true; // Boolean declared when assigned

// These statements print the assigned variables
echo "myByte is".$myByte; // Print out the $myByte variable
echo "myShort is".$myShort; // Print out the $myShort variable
echo "myInt is".$myInt; // Print out the $myInt variable
echo "myLong is".$myLong; // Print out the $myLong variable
echo "myFloat is".$myFloat; // Print out the $myFloat variable
echo "myDouble is".$myDouble; // Print out the $myDouble variable
echo "myChar is".$myChar; // Print out the $myChar variable
echo "myBoolean is".$myBoolean; // Print out the $myBoolean variable

?>

The PrimitiveDataTypes.php can be compiled as php -l PrimitiveDataTypes.php and compiled with php PrimitiveDataTypes.php

Conclusion:

There are notable differences between Java and PHP. Java is case sensitive and variables must begin with a lowercase letter. In Java, every variable must be specifically declared while PHP only requires a dollar sign. Boolean expressions in Java are case sensitive while those in PHP are case-insensitive.

There are more similarities between Java and PHP than differences. The PHP variables were created to resemble Java variables for this tutorial. It is possible to declare and assign variables immediately with Java and PHP.

    Recommendations:

  1. This tutorial uses an optional lightweight programming editor with syntax-highlighting features.
  2. The concepts taught in the first ten lessons can be applied to other programming languages.
  3. When selecting a programming language to master, pay close attention to license restrictions.