Java SE

Java vs 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 your own objects.

// Java int foo = 36; char bar = 'b'; String baz = 'abc'; MyWidget widget = new MyWidget(foo, bar); // Php $foo = 36; $bar = 'b'; $baz = 'abc'; $widget = new MyWidget(foo, bar);
Every variable can only hold a value of its type. Using the above declarations, the following is not valid.
// Java foo = bar; // error // Php $foo = $str; // valid
Methods need to have their return type specified, and bad things will happen if an method returns something it's not supposed to.
// Java public int fooBarBax(int v1){} // Php public fooBarBax($v1){}
You main code entry point in Java will be a method to a class, as opposed to PHPs main global entry point . PHP methods (and functions) allow you have optional parameters. In java, you need to define a separate method for each possible list of parameters.
// Php public function inPHP($var1, $var2='foo'){} // Java 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. Use 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, etc). OOP 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. Compiler Java is compiled to bytecode, PHP is interpreted (since PHP 4 its not, but it still behaves as if it was). 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. Conditional PHP does a lot of implicit type conversion, which can actually be problematic.
// the block will not be executed because the return index of 0 is converted to false $val = 'a'; if (strpos('abc', $val)) { ... } // correct version $val = 'a'; if (strpos('abc', $val) !== false) { ... }
Java conditional statements require an explicit boolean.