I have my mind set that I'm learning Java as my second language (I am a Perl developer). But just from reading a little of the online tutorials, I really can't find any similarities between the two. What will I have an easy time understanding when learning Java?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
closed as too broad by Snowman, durron597, Dan Pichelman, GlenH7, Kilian Foth Aug 7 '15 at 13:27
There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.
Perl is a high-level, general-purpose, multi-paradigm, interpreted, dynamic programming language. Java is a high-level, general-purpose, mostly single-paradigm, statically typed programming language.
So, both are high-level:
In essense this means that everything you can do with Perl, you can also do with Java. And as @KyleHodgson mentions, both their syntaxes derive from C and C++ and the syntax for simple stuff like
But that's where the similarities stop. Perl is multi-paradigm, supporting a wide range of programming paradigms:
Perl does not encourage one single paradigm, they are essentially equal and you are free to choose whichever you think best suits whatever you're building, without of course limiting you to a single paradigm, you can mix and match. On the other hand, Java is mostly a class based object oriented language. There is support for generic programming, but as a beginner you should think of Java as strictly a class based object oriented language.
So Perl allows more than one ways to structure your code1, whereas Java just one. That's not a bad thing (or a good thing), it's just different. If you haven't written any object oriented Perl code, Java might seem a little bit alien at first. Don't be discouraged, object orientation is something that you will eventually have to learn, if you are considering a career in software development, and learning Java is a good way of learning the basic concepts of object orientation - not a perfect way, but definately a good way.
And as you know, Perl is interpreted, whereas Java is... well... a different beast entirely. In Java you write your code as you would in Perl, and then you compile it. The result is not an executable, but Java bytecode. This intermediatery format is executed (finally!) in the Java Virtual Machine, which is somewhat analogous to the Perl interpreter. A JVM must be installed beforehand for a Java program to run, simirarly on how you need to install a Perl interpreter to execute a Perl script2.
Coming from a Perl background, the most important thing to remember is the compilation to bytecode step: Every time you make a change to a Java source file you need to re-compile it. It might sound crazy at first, but compilation has a very nice consequence: Your code is checked for a variety of errors at this stage, and the compiler refuses to finish the process if there are any, and sometimes will help you pinpoint the errors with helpful messages (There are always messages, but only sometimes they are helpful).
Which brings us to the last major difference:
Perl is dynamic3:
and dynamically typed4:
and Java is statically typed:
Which, to put it as simply as possible, means that in Java you have to declare the type of your variables and methods before using them. There are other differences, but I wouldn't want to spoil the fun of discovering them by yourself :)
And, finally, there is one very important difference: Java is the sweetheart language of the academia5 and the corporate world, while you will rarely meet Perl in an academic setting anymore (where I first met her), and its career perspectives are shrinking (still quite a few jobs, but nowhere near as many as Java, .Net languages or PHP). I will not comment on the reasons, I'm just stating the (sad) facts. Since you are still very young, by learning Java you will be a little bit more prepared for a Computer Science degree, if you chose to follow that path.
Don't give up on Perl, of course, but do explore Java. The fact that they are more different than similar also means that you will learn quite different approaches and programming mentallities, it's a hard path but one that will ultimately make you a better programmer.
2 The Perl community is actively exploring the possibillity of a Perl virtual machine, via Parrot.
3 Dynamic doesn't always mean dynamically typed.
4 Perl is dynamically typed for user defined types, statically typed with respect to distinguishing arrays, hashes, scalars, and subroutines, and strongly typed via
5 To the point of abuse, as Joel Spolsky writes in The Perils of JavaSchools.
Well, they both use a C like syntax - semicolons, curly braces and the like. Personally, when I tried to make a switch from Perl to Java a number of years ago - the biggest thing that tripped me up was object oriented programming. I had developed some objects in Perl, but it hurt my head to try to imagine life in the Kingdom of Nouns.
Taking a stab at an alternative answer since the currently selected one seems to be focusing on differences between the two (truly there are not a lot of similarities between Perl and Java).
If your Perl code is very rigorous and orthogonal (well-tested, well-documented, uses Moose or strict object-orientation), then those concepts will carry very well into Java, since Java is extremely orthogonal and not as expressive.