Wednesday 6 July 2011

what is the major difference between java language and php?

The Java Class library provides a mechanism to implement threads. PHP has no such mechanism.

PHP methods (and functions) allow you have optional parameters. In java, you need to define a separate method for each possible list of parameters
public function inPHP($var1, $var2='foo'){}
public void function inJava($var1){
    $var2 = "foo";
    inJava($var1,$var2);
}
public void function inJava($var1,$var2){
}

PHP requires an explicit $this be used when an object calls its own methods methods. Java (as seen in the above example) does not.

Java programs tend to be built from a "program runs, stays running, processes requests" kind of way, where as PHP applications are built from a "run, handle the request, stop running" kind of way.
link|improve this answer


I don't understand what is unintuitive about Strings. In your example, "foo" != "bar" whether the semantics is by reference or by value. – Alan Jan 4 '09 at 22:20

As well you shouldn't have. Both vars strings should have been the same, I was dashing things off in too much of a hurry. Thanks for catching that. – Alan Storm Jan 4 '09 at 22:30
1  
In your first String equality example, v1==v2 will very often be true. When creating String objects using the Java syntactic sugar for strings (String v1 = "foo"), these strings are usually interned, so v1==v2 will refer to the same object. OTOH, if you have String v2 = new String("foo"), – dancavallaro Jan 4 '09 at 22:36
1  
Java can do collections with mixed types. All classes inherit from Object, so can create a list of objects (the default). Also, Java calls it Generics, not Templates. Templates is the C++ term (and perhaps others). – Mikezx6r Jan 6 '09 at 12:22
1  
FYI in my experience its been too unreliable to rely on if (string1 == string2), it quite often returns false. Its a lot better to use if (string1.equals(string2) instead – Click Upvote Oct 7 '09 at 17:38
show 4 more comments

I think these two languages (as well as their runtime systems) are too different to list all differences. Some really big ones that come to my head:
  • Java is compiled to bytecode, PHP is interpreted (as Alan Stirm pointed out, since PHP 4 its not, but it still behaves as if it was)
  • 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, but can do anything else as well (like Applets, mobile phone software, Enterprise stuff, desktop application with and without GUI, 3d games, Google Web Toolkit...)
  • add your favourite difference here
You will notice most differences when it's time to, but what's most important:
  • PHP offers OOP (object oriented programming) as an option that is ignored in most projects. Java requires you to program the OOP way, but when learning Java with a background in a not-so-OOP-language, it's really easy to mess things up and use OOP the wrong way (or you might call it the sub-optimum way or the inefficient way...).
link|improve this answer


please see edit and clarify if possible :) – Click Upvote Jan 4 '09 at 18:30

I have to admit that my PHP experience is too long ago to give a smart answer to that specific question. I guess someone else will leave an answer. – Brian Schimmel Jan 4 '09 at 18:38

Just a quick note that PHP isn't strictly "interpreted". Since PHP 4, on each request for a PHP page/script the parser will produce bytecode (often called optcode), which is then executed by the Zend Engine. – Alan Storm Jan 4 '09 at 20:01

  • Java is strongly-typed. PHP isn't;
  • PHP does a lot of implicit type conversion, which can actually be problematic and is why PHP5 has operators like === and !==. Java's implicit type conversion is primarily limited to auto-boxing of primitive types (PHP has no primitive types). This often comes up.
Consider:
$val = 'a';
if (strpos('abcdefghij', $val)) {
  // do stuff
}
which is incorrect and will have the block not executed because the return index of 0 is converted to false. The correct version is:
$val = 'a';
if (strpos('abcdefghij', $val) !== false) {
  // do stuff
}
Java conditional statements require an explicit boolean;
  • PHP variables and arrays are all prepended by $ and otherwise indistinguishable;
  • The equivalent of PHP associative arrays is PHP Maps (eg HashMap). Associative arrays are ordered on insertion order and can be used like ordinary arrays (on the values). Theres one Map implementation that maintains insertion order in Java but this is the exception rather than the norm;
  • $arr['foo'] = 'bar' insert or update an element in an associative array. Java must use Map.put() and Map.get();
  • PHP5 has the equivalent of function pointers and rudimentary closures (using create_function()). Java must use anonymous classes for both, which is somewhat more erbose;
  • Variable declaration is optional in PHP;
  • Use of global variables within functions requires explicit use of teh global keyword in PHP;
  • POST/GET parameters are, unless configured otherwise (register_globals()) automatically result in global variables of the same name. They can alternatively be accessed via the $_POST global variable (and $_SESSION for session variables) whereas support for these things comes from a JEE add-on called the servlets API via objects like HttpServletRequest and HttpSession;
  • Function declaration in PHP uses the function keyword whereas in Java you declare return types and parameter types;
  • PHP function names can't normally clash whereas Java allows method overloading as long as the different method signatures aren't ambiguous;
  • PHP has default values for function arguments. Java doesn't. In Java this is implemented using method overloading.
Compare:
function do_stuff($name = 'Foo') {
  // ...
}
to
void doStuff() {
  doStuff("Foo");
}
void doStuff(String what) {
  // ...
}
  • String constants in PHP are declared using single or double quotes, much like Perl. 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();
  • In Java objects, this is implicit and only used to be explicit about scope and in certain cases with inner classes. In PHP5, $this is explicit;
  • Static methods in Java can be called with either the . operator on an instance (although this is discouraged it is syntactically valid) but generally the class name is used instead.
These two are equivalent:
float f = 9.35f;
String s1 = String.valueOf(f);
String s2 = "My name is Earl".valueOf(f);
but the former is preferred. PHP uses the :: scope resolution operator for statics;
  • 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;
  • PHP uses the . operator to append strings. Java uses +;
  • Java 5+ methods must use the ellipsis (...) to declare variable length argument lists explicitly. All PHP functions are variable length;
  • Variable length argument lists are treated as arrays inside method bodies. In PHP you have to use func_get_args(), func_get_arg() and/or func_num_args();
  • and no doubt more but thats all that springs to mind for now.
link|improve this answer


Not agreeing or disagreeing, but I'm curious what you mean by "Method overriding and overloading is quite natural in Java but a bit of a kludge in PHP;". Unless I'm missing something, both languages allow child to declare new methods to override methods that exist in the parent class. – Alan Storm Jan 5 '09 at 0:38
1  
@Alan, yes, but in PHP if a class has got two functions of the same name or there are 2 procedural funcs of same name, you get a T_FUNCTION_EXISTS or something like that error – Click Upvote Jan 5 '09 at 3:51

  • you could use JavaDoc tool to autogenerate documentation on your software. But you need to write comments in specific way.
  • you can't run PHP on mobile phones :) There are a lot of run time environments and platforms. That means you need to think in advance which libraries there could be missing or which limitations there could be (screen size, memory limits, file path delimiter "/" or "\" e.g).

No comments:

Post a Comment

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com