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?
closed as too broad by user22815, durron597, Dan Pichelman, GlenH7♦, Kilian Foth Aug 7 '15 at 13:27
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. 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:
A high-level programming language is a programming language with strong abstraction from the details of the computer.
In computer software a general-purpose programming language (GPL) is a programming language designed to be used for writing software in a wide variety of application domains.
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
for loops and
if statements is essentially the same - and as @DipanMehta notes, both are garbage collected. And of course both are extremely popular and have vibrant communities.
But that's where the similarities stop. Perl is multi-paradigm, supporting a wide range of programming paradigms:
- Functional programming,
- Imperative programming,
- Class based object-oriented programming,
- Reflective programming,
- Procedural programming, and
- Generic programming
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:
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution.
and dynamically typed4:
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time.
and Java is statically typed:
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time.
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
use strict, so essentially it's a variable type system language, but to keep some sense of sanity let's call it dynamically typed.
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).
- They are both C-like (they have loops and braces and semi-colons)
- They are principally imperative
- Neither produce compiled binaries
- They are both very mature
- They are both battle-tested in the enterprise (Perl more as ops glue and Java more as applications/services)
- They are both popular
- They both support object-orientation
- They both have a wide array of frameworks available
- They both have books published about them
- They are both higher-level than C/assembly
- They are both "slow" (for various, specific scenarios of slow in that neither are C/assembly)
- They both manage memory automatically ("garbage collection")
- Neither are hip/cool as of summer 2012
- They are both entrenched in the open source community
- They are both available for many platforms
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.