48

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.

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.

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

What I've garnered so far about a move to Java:

  • I need an IDE: IntellijIDEA, NetBeans or Eclipse
  • I need to pick a development framework. Some recurrent names: 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.

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.

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?
Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79
  • I used NetBeans briefly then ended up using Eclipse because this is what they used in school lecture. First know the basics by reading a book. Creating your own stuff in Java is fastest (reading java sun tutorial when required and asking on SO). It took me about 1.5 years (but I had no OOP experience prior, only minor scripting on php, LAMP websites). I guess Eclipse IDE also made the learning process smoother, learning the shortcut keys makes it even better. – KJW Mar 26 '12 at 10:10
  • @mike 7 years later, did that transition go well? Would you suggest it to someone else? – lephleg May 22 '17 at 19:06
  • 1
    @LePhleg 7 years later the answer would still be "it depends what you're after". It's my opinion that to shepherd a budding developer toward good practice languages such as Java or Python are better equiped, because of their more mature community and tooling. PHP has a low barrier to entry which translates into a wide selection of candidates for employers, and a wide selection of jobs for candidates. For me, honing my craft was more valuable than the short term prospect of easily accessible jobs. The PHP ecosystem might have completely changed nowadays, so my point may be moot. – Michael Ekoka May 23 '17 at 13:23
  • @mike First of all, thanks for the instant reply so long after this thread has been closed. I'm on the same page regarding strongly typed languages. Working as a PHP dev for the last 3 years, I had to mantain and extend lots of spaggeti code from earlier releases, which made me sick of it. I really like the flexibility of PHP for simple tasks but I think it's like a boomerang when the project needs to scale. I'm thinking of trying Java for a bit to see how this would work. Would you suggest a web framework? Maybe Spring with Spring Boot? – lephleg May 23 '17 at 21:37
  • @LePhleg I ended up using Python. I haven't done anything in Java for a long time. – Michael Ekoka May 24 '17 at 10:58

7 Answers7

23

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:

  • ant, for organising your project, running automated test suites, incremental compiles
  • BeanShell for interactive testing, trying things out quickly
  • A quick trick: javap from the commandline will give method signatures for any class in the standard library. Similar to php -r but gives more information since Java is typed.

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.List is a subinterface of java.util.Collection with 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.

Some miscellaneous distinctions:

  • Strings are sequences of characters rather 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.Document is 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 or java.sql.Connection or 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.

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 (FactoryBuilderFactory type abstractions, web.xml files, etc) makes sense and does something useful. In a small company where the programmers are the 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.

  • The java char is too small. 16-bits is insufficient for Unicode. Additionally there is this new "Code Point" thing which apparently is needed for doing it properly. In other words - String is not enough. – Thorbjørn Ravn Andersen Feb 06 '10 at 21:18
  • Yes, good point. Strictly, Java strings aren't sequences of characters, they're sequences of, what is it... UTF16 code units? Java programmers are stuck with character model from 1995, when 16 bits were thought sufficient. It's still better for productivity and data integrity than the 1965 character model PHP programmers currently work with. I haven't yet investigated the PHP6 character model, which is reported to be more Unicode-compatible. –  Feb 06 '10 at 21:55
8

Start with the Java Tutorial

http://java.sun.com/docs/books/tutorial/getStarted/index.html

Then go buy Head First Java

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
8

Strictly speaking, you don't need an 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.

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.

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.

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).

Pointy
  • 405,095
  • 59
  • 585
  • 614
5

The best move I made was from Java to PHP.

Beware of complexity. Primarily, the key to great software is simplicity, and that is why PHP combined with a good framework kills 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.

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.

rick
  • 1,539
  • 10
  • 12
  • 3
    *Chacun à son goût* of course, but much of what you describe as being reasons to flee Java are, to me, great strengths and "power tools." But I do agree that framework choice is very important, and that's a hard thing to advise because it's so subjective. – Pointy Feb 06 '10 at 18:52
  • 8
    Static typing is _the_ quality that makes it possible to build cathedrals in Java. In my opinion. – Thorbjørn Ravn Andersen Feb 06 '10 at 19:02
  • @rick, this doesn't really answer the question... – Pool Feb 06 '10 at 19:05
  • Sure, my answer is laced with personal opinion, but I did answer the most important point IMO: gotchas. Choosing the simplest framework is the point I wanted to make. The question would require an essay to answer completely. – rick Feb 06 '10 at 19:10
  • 1
    As for static vs dynamic typing, well that is a fun debate. Douglas Crockford (whom I idolize) now prefers dynamic typing over static. He makes the point that compile-time errors are usually obvious. Run-time errors are responsible for most bugs and compilation does nothing for those. So the flexibility and development speed of dynamic outweigh the disadvantages. I'd add that web applications are compelling for dynamic since they always deal with string inputs/outputs. It seems to me, that large backend applications where data is passed around a lot would benefit from static types. – rick Feb 06 '10 at 19:22
  • @rick, I think you should try doing some programming in Haskell, just to see what types _can_ do for you. – Thorbjørn Ravn Andersen Feb 06 '10 at 21:31
  • I guess rick is not completely wrong but he's not completely correct either. This view is strictly limited to the situation, what type of project you are trying to buid. php is really great to quickly build web applications, and I admit there's lot of bloat when you are trying to create the same web application on Java, but things have changed. Play framework and Grails sufficiently cuts through that bloat now. Comparing php to java is like apples and oranges. Java runs your atm machine, try convincing banks to use php instead. – KJW Mar 26 '12 at 09:57
  • 2
    For web development PHP has a killer pro argument: You don't have to compile your stuff for every little change. – Sliq Sep 26 '12 at 20:14
2

I recommend hitting up JavaBat at here

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.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
2

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...

First off: Java is awesome :)

  • 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.

I could go on.

Paul

Paul
  • 37
  • 1
0

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).

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".

Good luck!

KJW
  • 15,035
  • 47
  • 137
  • 243