从 PHP 到 Java。有什么建议吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2214122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
From PHP to Java. Any advice?
提问by Michael Ekoka
I've been doing web application development for the last 3 years in PHP. I'm now on the verge to give Java a go. My last use of the language was nearly 8 years ago and was mostly academic.
在过去的 3 年里,我一直在用 PHP 进行 Web 应用程序开发。我现在即将尝试使用 Java。我最后一次使用这门语言是在将近 8 年前,而且主要是学术性的。
I'm reasonably well acquainted with PHP's object model (version 5) and I have almost exclusively been coding in OO. I would now like to transport that experience and use it to develop in Java.
我相当熟悉 PHP 的对象模型(第 5 版),而且我几乎只用 OO 进行编码。我现在想传达这种经验并用它来用 Java 进行开发。
Where I'm coming from:
我来自哪里:
- linux as a desktop and server
- Vim/gVim + plugins as an editor
- MySql for db
- apache httpd
- experience with a bunch of PHP frameworks, Zend+Doctrine being the ones I use most
- linux 作为桌面和服务器
- Vim/gVim + 插件作为编辑器
- 用于数据库的 MySql
- 阿帕奇httpd
- 有一堆 PHP 框架的经验,Zend+Doctrine 是我最常用的
What I've garnered so far about a move to Java:
到目前为止,我所获得的关于转向 Java 的信息:
- I need an IDE: IntellijIDEA, NetBeans or Eclipse
- I need to pick a development framework. Some recurrent names: Spring MVC, stripes, wicket.
- 我需要一个 IDE:IntellijIDEA、NetBeans 或 Eclipse
- 我需要选择一个开发框架。一些经常出现的名称:Spring MVC、stripes、wicket。
Now I need some insight that could help make this transition smoother. But from the way people talk about it, Java seems to be an entirely new beast with its own ecosystem. It sounds as though moving to Ruby or Python would actually be easier, which is curious since, when I look at it, Java conceptually seems the closest to PHP, albeit stricter and precompiled.
现在我需要一些洞察力来帮助使这种过渡更加顺利。但从人们谈论它的方式来看,Java 似乎是一个全新的野兽,拥有自己的生态系统。听起来好像迁移到 Ruby 或 Python 实际上会更容易,这很奇怪,因为当我查看它时,Java 在概念上似乎最接近 PHP,尽管更严格和预编译。
As weird as this may sound, very few people have publicly documented their experience of such moves. I have searched google, amazon and stackoverflow for similar questions and the results leave to desire. I just can't believe that I would need to start the same as a newbie if I wanted to be productive as a web developer in Java fast.
这听起来可能很奇怪,但很少有人公开记录他们对此类举动的经历。我在 google、amazon 和 stackoverflow 上搜索过类似的问题,结果不尽人意。我简直不敢相信,如果我想以 Java 的 Web 开发人员的身份快速提高工作效率,我需要像新手一样开始。
Anybody is welcome to respond, but I somewhat think that people with some valuable experience in both languages would enrich this discussion the most.
欢迎任何人回应,但我有点认为在两种语言中都有一些宝贵经验的人最能丰富这次讨论。
- What helped you get going quickly in Java?
- What concepts are omnipresent in Java and absent from PHP and vice versa?
- Some gotchas for PHP developers going Java.
- How long before you felt the transition was complete?
- 是什么帮助您快速上手 Java?
- 哪些概念在 Java 中无处不在而在 PHP 中不存在,反之亦然?
- PHP 开发人员使用 Java 的一些问题。
- 多久之前你觉得过渡已经完成?
采纳答案by Michael Ekoka
I wouldn't try to learn an IDE at the same time as learning a language. An easier transition would be to stick to your shell and habitual text editor, and use the following shell-friendly tools:
我不会在学习语言的同时尝试学习 IDE。一个更简单的过渡是坚持使用你的 shell 和习惯性的文本编辑器,并使用以下对 shell 友好的工具:
- ant, for organising your project, running automated test suites, incremental compiles
- BeanShellfor interactive testing, trying things out quickly
- A quick trick:
javapfrom the commandline will give method signatures for any class in the standard library. Similar tophp -rbut gives more information since Java is typed.
- ant,用于组织项目、运行自动化测试套件、增量编译
- BeanShell用于交互式测试,快速尝试
- 一个快速的技巧:
javap从命令行将为标准库中的任何类提供方法签名。类似于php -r但提供了更多信息,因为 Java 是类型化的。
The online documentation for Java is precise, professional, and consistent in tone and format. Unlike in PHP where all of the functions are in one flat namespace, the standard libraries in Java are class hierarchies. You've got to know your way around that standard library, which means knowing hierarchies + responsibilities: for example you've got to know that java.util.Listis a subinterface of java.util.Collectionwith the addition of a concept of ordered entries. With that information in your head, a google search for java.util.List will take you to the Javadoc for the class, and the Javadoc will tell you exact method signatures and link you to a selection of concrete implementations.
Java 的在线文档在语气和格式上准确、专业且一致。与 PHP 中的所有函数都在一个平面命名空间中不同,Java 中的标准库是类层次结构。您必须了解该标准库的使用方式,这意味着了解层次结构 + 职责:例如,您必须知道这java.util.List是一个java.util.Collection添加了有序条目概念的子接口。有了这些信息,在谷歌搜索 java.util.List 将带您到该类的 Javadoc,Javadoc 将告诉您确切的方法签名并将您链接到具体实现的选择。
Some miscellaneous distinctions:
一些杂项区别:
- Strings are sequences of charactersrather than sequences of bytes. Absolutely the right way of doing it.
- Systems produce and consume streams(of bytes or characters) rather than byte buffers. For example, if you wanted to filter the output in PHP, a standard practice is to ask ob_get_contents for a byte buffer then to transform the whole buffer. In Java, you add a filter to your servlet that transforms the output a byte or a character at a time. It's a bit imposing to work with initially but it's simpler and more Lego-like when you get used to it - your stream processor doesn't have to know where things come from and where they go.
- Almost everything useful is an interface, and creating an instance of an interface can be tricky, non-standardised, and not always well-documented. In PHP, you can get up and running with XML with
new DOMDocument(). In Java,org.w3c.dom.Documentis an interface, so new() won't work. Javadoc is very precise about how interface instances behave once they come into existence, but it can be quite coy and prudish when you're trying to find out how an object is actually born. Very often, you'll have to look for tutorials and code examples and copy-paste a piece of boilerplate that gives you an instance of DOMDocument orjava.sql.Connectionor whatever. One reason frameworks like Spring are popular is that they separate out the ugly object-creation code and present you with a world where interface implementations are just magically there.
- 字符串是字符序列而不是字节序列。绝对正确的做法。
- 系统产生和使用流(字节或字符)而不是字节缓冲区。例如,如果您想在 PHP 中过滤输出,标准做法是向 ob_get_contents 请求一个字节缓冲区,然后转换整个缓冲区。在 Java 中,您可以向 servlet 添加一个过滤器,一次将输出转换为一个字节或一个字符。最初使用它有点令人印象深刻,但是当您习惯它时它更简单,更像乐高 - 您的流处理器不必知道事物的来源和去向。
- 几乎所有有用的东西都是一个接口,创建一个接口的实例可能很棘手、不标准化,而且并不总是有很好的文档记录。在 PHP 中,您可以使用 .xml 文件启动并运行 XML
new DOMDocument()。在 Java 中,org.w3c.dom.Document是一个接口,所以 new() 不起作用。Javadoc 非常精确地描述了接口实例在它们出现后的行为方式,但是当您试图找出一个对象实际上是如何诞生时,它可能会非常腼腆和谨慎。很多时候,您必须寻找教程和代码示例,然后复制粘贴一段样板,为您提供 DOMDocument 的实例java.sql.Connection或其他任何内容。像 Spring 这样的框架流行的一个原因是它们分离出丑陋的对象创建代码,并为您呈现一个接口实现神奇地存在的世界。
I actually switched in the opposite direction. I found that Java works very well in a large company where you might be working on a single component, handing it off to someone else who integrates that component into a larger system, which is then packaged and handed off to a separate operations team - that's where all this indirection and configurability (FactoryBuilderFactorytype abstractions, web.xml files, etc) makes sense and does something useful. In a small company where the programmers arethe ops personnel, Java is a lot more work. With Java, you'll have to get used to concepts like starting up the Java process, monitoring the Java process to make sure it stays up, monitoring the Java process to make sure that it doesn't go into a coma where it's alive but not responding, shutting down and restarting the Java process with minimal disruption when you're updating code, etc, etc. If you have separate ops personnel, that's fine, it's their job, they're very good at it. If you're a programmer, babysitting a Java process can be distracting and difficult to do well.
我实际上是从相反的方向切换的。我发现 Java 在一家大公司中运行良好,您可能正在处理单个组件,然后将其交给其他人,后者将该组件集成到一个更大的系统中,然后将其打包并交给一个单独的运营团队——那就是所有这些间接性和可配置性(FactoryBuilderFactory类型抽象、web.xml 文件等)都有意义并且做一些有用的事情。在一家小公司的程序员都运维人员,Java 的工作要多得多。使用 Java,您必须习惯诸如启动 Java 进程、监视 Java 进程以确保它保持正常运行、监视 Java 进程以确保它不会进入昏迷状态等概念当您更新代码等时,不响应、关闭并以最小的中断重新启动 Java 进程。如果您有单独的运维人员,那很好,这是他们的工作,他们非常擅长。如果您是一名程序员,照看 Java 进程可能会让人分心并且很难做好。
回答by Thorbj?rn Ravn Andersen
Start with the Java Tutorial
从 Java 教程开始
http://java.sun.com/docs/books/tutorial/getStarted/index.html
http://java.sun.com/docs/books/tutorial/getStarted/index.html
Then go buy Head First Java
然后去买 Head First Java
http://oreilly.com/catalog/9780596004651
http://oreilly.com/catalog/9780596004651
It will get you going pretty quickly in the language which is essential regardless of what you want to do.
无论您想做什么,它都会让您很快掌握这门必不可少的语言。
回答by Pointy
Strictly speaking, you don't needan IDE to work in Java. I've been coding heavily in Java for well over a decade (and heavily on other stuff for over 25 years), and I don't much like IDEs. Perhaps that implicit indicator of my age is part of the issue :-) It's a trade-off, like anything else.
严格来说,您不需要IDE 就可以在 Java 中工作。十多年来,我一直在大量使用 Java 进行编码(并且在 25 年多的时间里大量使用其他东西),而且我不太喜欢 IDE。也许我年龄的隐含指标是问题的一部分:-) 这是一种权衡,就像其他任何事情一样。
I'll plug Stripes as a nice simple framework, but mostly that's because it's relatively small and limited in scope. The large frameworks like Spring are "kitchen sink" ecosystems, and learning Java with one of those frameworks may smooth some of the difficult parts but leave other basic aspects mysterious. Again, it's a matter of personal preference.
我将把 Stripes 作为一个很好的简单框架插入,但主要是因为它相对较小且范围有限。像 Spring 这样的大型框架是“厨房水槽”生态系统,使用其中一个框架学习 Java 可能会解决一些困难的部分,但会使其他基本方面变得神秘。再次,这是个人喜好的问题。
It's good to have somebody around who knows the language. Oh, and to that point, make a friend of the Java API documentation. As platforms go, the Java API has its ups and downs but for the most part the documentation is at least pretty thorough and pretty accurate.
身边有懂这门语言的人是件好事。哦,在这一点上,请与 Java API 文档交朋友。随着平台的发展,Java API 有起有落,但在大多数情况下,文档至少非常全面和准确。
You'll also want to get really familiar with JSP and its relationship to Java and Java web service architectures, because that'll be what you'll relate to your PHP experience most directly (I'd think).
您还需要真正熟悉 JSP 及其与 Java 和 Java Web 服务体系结构的关系,因为这将是您与 PHP 体验最直接相关的内容(我认为)。
回答by rick
The best move I made was from Java to PHP.
我所做的最好的一步是从 Java 到 PHP。
Beware of complexity. Primarily, the key to great software is simplicity, and that is why PHP combined with a good framework kills Java.
小心复杂性。首先,优秀软件的关键是简单性,这就是 PHP 与好的框架相结合能杀死 Java 的原因。
With Java, you risk becoming a slave to your servlet container and framework. Choose the simplest most flexible framework. Controls and custom tags are the devil. You'll waste days learning things that were designed to make development quicker, but are ultimately too complex and inflexible. Java will slow development due to: complexity, compilation, and static types. You'll come to loathe null pointer exceptions.
使用 Java,您可能会成为 servlet 容器和框架的奴隶。选择最简单最灵活的框架。控件和自定义标签是魔鬼。您将浪费数天时间学习旨在加快开发速度但最终过于复杂和不灵活的东西。由于以下原因,Java 会减慢开发速度:复杂性、编译和静态类型。你会讨厌空指针异常。
Edit: Tools aside, Java and PHP are very similarly structured "C" style languages. Much more so than Python or Ruby. It is the static typing and complex tools that make Java so foreign.
编辑:撇开工具不谈,Java 和 PHP 是结构非常相似的“C”风格语言。远远超过 Python 或 Ruby。正是静态类型和复杂的工具让 Java 变得如此陌生。
回答by Woot4Moo
I recommend hitting up JavaBat at here
我建议在这里使用JavaBat
It will give you some good ideas. It took me 1 solid year in a professional setting to get a very firm grasp on Java and I have been able to move into other OO languages quite easily once I had the thoughts beaten into me.
它会给你一些好主意。我在专业环境中花了 1 年的时间才牢牢掌握 Java,一旦我有了这些想法,我就能够很容易地转向其他 OO 语言。
回答by Paul
This appears to be a bit of an old post, but hey...
这似乎是一个旧帖子,但是嘿...
I moved from C# to Java and I've just started a role and am moving from Java to PHP...
我从 C# 转移到 Java,我刚刚开始担任一个角色,正在从 Java 转移到 PHP...
First off: Java is awesome :)
首先:Java 很棒 :)
for your IDE get Eclipse, once you get used to it, you never have to leave the IDE (apart from SQL bits). It manages projects really well, you can download alsorts of plugins, such as SVN plugins. It allows you to run a Tomcat server within Eclipse and it will output errors straight to the Eclipse console.
for the framework, I used Struts and Tiles and Torque for the ORM, took me a while to get my head around them, but once we made friends, I can't imagine any other way of developing. Although I imagine for a small project it would be a lot of overhead!
agreed with an above post - get HEADFIRST JAVA, thats how I learnt and I used it for lots of other languages, they have a visual method of learning thats alot easier than pure textbooks - well for me anyway. I was up and running with in a day and confident within a couple of weeks I guess - but always learning :)
yip Java is strict, but you come to love it, for me moving to loose 'ol PHP is a bit wierd.
you will also need to download Tomcat to run your Java bits, its easy as to get running.
Java organises all your classes very nicely, none of this 'require_once(some_text_file.php)' rubbish, just 'import myClass' and off you go.
one of the things that annoys me is, there is no way of telling which JAR libraries you are NOT using, so after a couple of years working on a site, your lib folder can get a bit messy - specially if mulitple people are removing and adding functionality.
对于您的 IDE,请使用 Eclipse,一旦您习惯了它,您就不必离开 IDE(除了 SQL 位)。它管理项目非常好,您还可以下载插件,例如SVN插件。它允许您在 Eclipse 中运行 Tomcat 服务器,并将直接向 Eclipse 控制台输出错误。
对于框架,我将 Struts 和 Tiles 以及 Torque 用于 ORM,我花了一些时间来了解它们,但是一旦我们成为朋友,我就无法想象任何其他的开发方式。虽然我想对于一个小项目来说这将是很多开销!
同意上面的帖子 - 获得 HEADFIRST JAVA,这就是我学习的方式,我将它用于许多其他语言,他们有一种视觉学习方法,比纯教科书容易得多 - 无论如何对我来说都很好。我想我在一天之内就开始运行并在几周内充满信心 - 但我一直在学习:)
yip Java 是严格的,但你会喜欢它,对我来说,松散的 'ol PHP 有点奇怪。
您还需要下载 Tomcat 来运行您的 Java 位,它很容易运行。
Java 非常好地组织了所有类,这些“require_once(some_text_file.php)”都不是垃圾,只需“import myClass”就可以了。
令我烦恼的一件事是,无法确定您没有使用哪些 JAR 库,因此在网站上工作几年后,您的 lib 文件夹可能会变得有点乱 - 特别是如果多人正在删除和添加功能。
I could go on.
我可以继续。
Paul
保罗
回答by KJW
For my case, I only dealt with lot of procedural coding on the php, so jumping to Java, not only did I find everything to be more verbose and less forgiving than php (but now I know why it must be this way, and I love Java a lot) but learning to organizing my code in classes and also learning the eclipse IDE took about 1.5 years of just tinkering, trial & error, making stuff on my own (mainly swing applications).
就我而言,我只在 php 上处理了很多程序编码,所以跳到 Java,我不仅发现一切都比 php 更冗长,更不宽容(但现在我知道为什么必须这样了,而且我非常喜欢 Java)但是学习在类中组织我的代码以及学习 Eclipse IDE 花了大约 1.5 年的时间,只是修补、试错、自己制作东西(主要是摆动应用程序)。
I guess just creating stuff on your own, utilizing the vast amount of Java libraries out there to build your own stuff is fun is the fastest way. Also I think I could've saved more time by reading the java sun tutorials thoroughly. Even more time saving is making sure you've done a thorough search for libraries that prevent "reinventing the wheel".
我想只是自己创建东西,利用大量的 Java 库来构建自己的东西很有趣,这是最快的方法。此外,我认为我可以通过彻底阅读 java sun 教程来节省更多时间。更节省的时间是确保您已经对防止“重新发明轮子”的库进行了彻底的搜索。
Good luck!
祝你好运!

