PHP和Java有什么区别? [英] What are the differences between PHP and Java?

查看:104
本文介绍了PHP和Java有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP和Java之间的主要差异是哪些人精通PHP但学习Java应该知道什么?

What are the main differences between PHP and Java that someone proficient in PHP but learning Java should know about?

编辑:我的意思是语言语法的差异,即它们的数据类型,它们如何处理数组&引用变量,等等:)

I mean differences in the syntax of the languages, i.e their data types, how they handle arrays & reference variables, and so forth :)

推荐答案

不是一个详尽的列表,我是PHP开发人员,他参观了Java一段时间后,所以Caveat Emptor。

Not an exhaustive list, and I'm PHP developer who did a tour of Java a while back so Caveat Emptor.

Java中的每个变量都需要以数据类型为前缀。这包括基本类型,如boolean,int,double和char,以及Object数据类型,如ArrayList,String和您自己的对象

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

int  foo    = 36;
char bar    = 'b';
double baz  = 3.14;
String speech = "We hold these truths ...";
MyWidget widget = new MyWidget(foo,bar,baz,speech);






每个变量只能包含其类型的值。使用上述声明,以下内容无效


Every variable can only hold a value of its type. Using the above declarations, the following is not valid

foo = baz






对象上的等同性(不是基本类型)检查对象标识。所以以下不直观地打印错误。字符串有一个相等的方法来处理这个问题。


Equality on objects (not on primitive types) checks for object identity. So the following un-intuitively prints false. Strings have an equality method to handle this.

//see comments for more information on what happens 
//if you use this syntax to declare your strings
//String v1 = "foo";
//String v2 = "foo";

String v1 = new String("foo");
String v2 = new String("foo");

if(v1 == v2){
    pritnln("True");
}
else{
    println("False");
}






数组是你的经典C阵列。只能保存一种特定类型的变量,需要使用固定长度创建


Arrays are your classic C arrays. Can only hold variables of one particular type, need to be created with a fixed length

要解决这个问题,有一个系列集合对象,其中一个名为ArrayList,其行为更像PHP数组(尽管保持一种类型的业务仍然是正确的)。你没有得到类似语法的数组,所有的操作都是通过方法完成的。

To get around this, there's a series of collection Objects, one of which is named ArrayList that will act more like PHP arrays (although the holds one type business is still true). You don't get the array like syntax, all manipulation is done through methods

//creates an array list of strings
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("My First Item"); 

ArrayLists仍然有数字键。还有另一个名为HashMap的集合,它会为你提供一个字典(或关联数组,如果你在90年代上学),就像对象一样。

ArrayLists still have numeric keys. There's another collection called HashMap that will give you a dictionary (or associative array, if you went to school in the 90s) like object.

ArrayLists和其他集合使用称为泛型的东西(< String>)实现。我不是Java程序员,所以我对Generics的理解是他们描述了Object将要运行的东西的类型。还有更多的事情发生。

ArrayLists and other collections are implemented with something called generics (the <String>). I am not a Java programmer, so all I understand about Generics is they describe the type of thing an Object will operate on. There is much more going on there.

Java没有指针。但是,所有对象实际上都是引用,类似于PHP 5,与PHP 4不同。我不认为 Java具有(折旧的)PHP& reference&语法。

Java has no pointers. However, all Objects are actually references, similar to PHP 5, dissimilar to PHP 4. I don't think Java has the (depreciated) PHP &reference &syntax.

所有方法参数都是通过Java中的值传递的。但是,由于所有对象实际上都是引用,因此在传递对象时传递引用的值。这意味着如果您操纵传递给方法的对象,操作将会粘住。但是,如果你尝试这样的东西,你将得不到你期望的结果

All method parameters are passed by value in Java. However, since all Objects are actually references, you're passing the value of the reference when you pass an object. This means if you manipulate an object passed into a method, the manipulations will stick. However, if you try something like this, you won't get the result you expect

public void swapThatWontWork(String v1, String v2)
{
  String temp = var1;
  var1 = var2;
  var2 = temp;
}






这是一个很好的时间任何提及方法需要指定其返回类型,如果方法返回不应该的东西,将发生不好的事情。以下方法返回一个int


It's as good a time as any to mention that methods need to have their return type specified, and bad things will happen if an method returns something it's not supposed to. The following method returns an int

public int fooBarBax(int v1){
}






如果方法要抛出异常,则必须将其声明为,或编译器不会有任何与它


If a method is going to throw an exception, you have to declare it as such, or the compiler won't have anything to do with it.

public int fooBarBax(int v1) throws SomeException,AnotherException{
   ...
}

如果您正在使用这可能会变得棘手您没有在方法中写入可能引发异常的对象。

This can get tricky if you're using objects you haven't written in your method that might throw an exception.

Java中的主要代码入口点将是类的方法,而不是PHP的主要全局入口点

You main code entry point in Java will be a method to a class, as opposed to PHPs main global entry point

Java中的变量名不以sigil开头($),虽然我认为他们可以,如果你想要他们

Variable names in Java do not start with a sigil ($), although I think they can if you want them to

Java中的类名称区分大小写。

Class names in Java are case sensitive.

字符串在Java中是不可变的,因此串联可能是昂贵的操作配给。

Strings are not mutable in Java, so concatenation can be an expensive operation.

Java类库提供了一种实现线程的机制。 PHP没有这样的机制。

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

PHP方法(和函数)允许你有可选参数。在java中,你需要为每个可能的参数列表定义一个单独的方法

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需要使用显式$。 Java(如上例所示)没有。


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程序往往是通过程序运行,保持运行,处理请求的方式构建的,其中as PHP应用程序是从运行,处理请求,停止运行的方式构建的。

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.

这篇关于PHP和Java有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆