Difference between Java and PHP
Key difference: Java is a programming language,
which has been influenced by the C language. It derives much of its
syntax from C and C++, however it has fewer low-level facilities than
either. Java is a general-purpose programming language that is designed
to have fewer implementation dependencies as compared to previous
languages. PHP, on the other hand, is a server-side scripting language
that has its main implementation in web development. However, it can be
used as a general-purpose programming language.
Java is a programming language, which has been influenced by the C
language. It derives much of its syntax from C and C++, however it has
fewer low-level facilities than either. Java is a general-purpose
programming language that is designed to have fewer implementation
dependencies as compared to previous languages. It is concurrent,
class-based, and an object-oriented language.
Java was developed by James Gosling at Sun Microsystems and was released in 1995. It was originally released as a core component of Sun Microsystems' Java platform. Sun has since re-licensed most of its Java technologies under the GNU General Public License. This means that all the technology licensed under GNU General Public License is open source and generally available free of cost. Sun Microsystems eventually merged into Oracle Corporation.
Java is designed to allow the same code to run on various platforms.
i.e., developers are allowed to “write once, run anywhere” (WORA).
Hence, Java applications are typically compiled to bytecode. A class
file which contains a Java bytecode can then be run on any Java Virtual
Machine (JVM). This would be regardless of computer architecture.
As of 2012, Java has become one of the most popular programming
languages for use. This is especially true for client-server web
applications. Many other systems have developed alternative
implementations of Sun technologies, such as GNU Compiler for Java and
GNU Classpath.
PHP, on the other hand, is a server-side scripting language that has
its main implementation in web development. However, it can be used as a
general-purpose programming language. PHP was originally created by
Rasmus Lerdorf in 1995 and it is currently managed by The PHP Group. PHP
originally stood for Personal Home Page, however it was later renamed.
It now stands for PHP: Hypertext Preprocessor, a recursive acronym. PHP
is free software released under the PHP License, as is incompatible with
the GNU General Public License (GPL) due to restrictions on the usage
of the term PHP.
PHP is an open source, server-side, HTML embedded scripting language.
It can basically perform any task that other CGI programs can, but it
is mainly used to create dynamic Web pages. Its main advantage is that
it is compatible with many types of databases. Furthermore, PHP can talk
across networks using IMAP, SNMP, NNTP, POP3, or HTTP.
PHP includes a command-line interface capability and can be used in
standalone graphical applications. PHP commands can be embedded directly
into an HTML source document rather than calling an external file to
process data. In the HTML document, the PHP script is enclosed within
special PHP tags. Due to these tags, the programmer can alternate
between HTML and PHP instead of having to rely on heavy amounts of code
to output HTML. Also, as PHP is executed on the server, the client
cannot view the PHP code.
Some differences between Java and PHP:
-
Every variable in Java needs to be prepended with a data type. This
includes primitive types such as boolean, int, double and char, as well
as Object data-types, such as ArrayList, String, and own objects.
-
In Java, every variable can only hold a value of its type.
-
In Java, equality on objects checks for object identity.
-
In Java, arrays are the classic C arrays. They can only hold variables
of one particular type, and need to be created with a fixed length.
-
Java contains a series of collection Objects, one of which is named ArrayList. ArrayList will act more like PHP arrays.
-
In Java, one cannot get the array like syntax; all manipulation must be done through methods.
-
In Java, ArrayLists and other collections are implemented with generics.
-
Java has no pointers. However, all Objects are actually references, similar to PHP 5, and dissimilar to PHP 4.
-
All method parameters are passed by value in Java.
-
The main code entry point in Java is a method to a class, as opposed to PHP where it is not.
-
In Java, variable names do not usually start with a sigil ($).
-
Class names in Java are case sensitive.
-
Strings are not mutable in Java.
-
The Java Class library provides a mechanism to implement threads. PHP has no such mechanism.
-
PHP methods and functions allow for optional parameters. In Java, a
separate method must be defined for each possible list of parameters.
-
PHP requires an explicit $this be used when an object calls its own methods. Java does not.
-
Java programs tend to be built from a "program runs, stays running,
processes requests" kind of way, whereas PHP applications are built from
a "run, handle the request, stop running" kind of way.
-
Java is compiled to bytecode, PHP is interpreted.
-
Java is strong and statically typed, while PHP is rather weakly and dynamically typed.
-
PHP is mostly used to dynamically generate Webpages. Java can do that
too, as well as Applets, mobile phone software, Enterprise stuff,
desktop application with and without GUI, 3d games, Google Web Toolkit,
etc.
-
PHP offers OOP (object oriented programming) as an option that is ignored in most projects. In, Java, OOP is the default.
-
String constants in PHP are declared using single or double quotes.
Double quotes will evaluate variables embedded in the text. All Java
String constants use double quotes and have no such variable evaluation.
-
PHP object method calls use the -> operator. Java uses the . operator
-
Constructors in Java are named after the class name. In PHP they are called __construct()
-
Method overriding and overloading is quite natural in Java but a bit of a kludge in PHP
-
PHP code is embedded in what is otherwise largely an HTML document, much like how JSPs work
-
In PHP, there is no method overloading, but methods and functions can have optional parameters.