Forums Register Login

Beginner Java frustrations from a Perl Programmer

+Pie Number of slices to send: Send
Hello all! First time poster, and essentially first time visitor to the site

I've been using perl for development for around 8 years now, and was doing a fair amount of VB / ASP programming before that. I took a few (2, and one was really a SQL class, not a java class) classes in Java during my undergraduate degree, but never really got into it. Now I'm heading back to school for my Masters (since my employer will pay for the school, and pay me more if I get the masters) but I must say, I'm finding the obligated intro to java classes very frustrating as it seems most things I want to do that would be one line in perl are awkword in Java.

So, lets start with the questions, shall we?

First -- is there a Java resource like perl's cpan.org out there? When I'm looking to do something new, all I'm generally interested in is a short code snippet to do what I want to do, and the oracle docs are extremely verbose and not a ""3 second solution".

Second -- array declaration and management. Is there a java equavilent to something like this:
@temp = split("\s+",$line);
I'm running into issues dynamically creating and assigning values to arrays in java.

Third -- Hashes. There are times where I want a dynamically named variable to contain a value, in this case, I would use a hash in perl, like this:
foreach $line(<FILE>) {
@temp = split("\s",$line);
foreach $word (@temp) {
$wordCount{"$word"}++;
}
}

The above example even covers some file handling that seems oddly difficult in Java as compared to perl, but I think I'm doing it right.

Any discussion is appreciated, and I'm sure I'll have more as I keep working on java. Thanks for reading this far!
+Pie Number of slices to send: Send
I am not sure if you are right in comparing Perl and Java - be it in syntax wise or other areas. I havent done much of perl coding except the Selenium tests that I wrote (copied?).
I dont know the equivalent of CPAN.
But agree that Java is verbose and lot of them are complaining that. Hence there are lot of alternate languages on the JVM - Scala, Groovy, Clojure, JRuby and so on. These make the coding a lot simpler and not a nightmare for the developer.
Even the upcoming versions of Java- Java 8 (to be released late 2012) is trying to reduce this verbosity.

2. There is a String.split("separator") method in Java String API - This splits the string based on the separator passed (the separator can be a regex) and it returns an array of tokens.

3. You can use HashMap for storing the word count- Works similar to Hashes but you would have to use HashMap.put(key,value).

4. File Reading- Again there are a lot of APIs available in java.io package that would help you to read the contents from the file- FileReader, BufferedReader, Scanner (from java.util package)

And Welcome to JavaRanch
1
+Pie Number of slices to send: Send
It rather looks to me like, since you were a Perl programmer, you only did things which were Perl-ish. Your example appears to be some kind of text processing. And like Mohamed says, you can do those things in Java, only you do them differently.

However I've been programming in Java for about 10 years and I've almost never had to do anything like your example. The Java "ecosystem", as some call it, is a huge environment where all kinds of different things are done. If you're going to learn Java, then, you should do it properly. So you don't say "I know how to do that in Perl so how do I do that Perl code in Java?" Forget Perl entirely, and just learn Java. Otherwise you'll end up speaking bad Java with a heavy Perl accent.
+Pie Number of slices to send: Send
 

Tim Spengler wrote:Now I'm heading back to school for my Masters (since my employer will pay for the school, and pay me more if I get the masters) but I must say, I'm finding the obligated intro to java classes very frustrating as it seems most things I want to do that would be one line in perl are awkword in Java.

Second -- array declaration and management. Is there a java equavilent to something like this:
@temp = split("\s+",$line);
I'm running into issues dynamically creating and assigning values to arrays in java.


First: if you're going for your Master's, you should understand that you're in the big boys league now. Perl, for all its wonderful plusses, started out life as a script (and I use the term advisedly, though you'll rarely hear it now) for processing line-oriented text files; and in order to understand what I'm about to say, you'll need a bit of history.

Scripts - and you'll often hear things like bash and Perl touted as "languages", but they aren't (at least, not yet) - were generally designed to provide command line techies (usually administrators) with a way of interacting with the system they were working on. In a sense you could look on them as an "administrators language": no more need to write tedious and error-prone assembler code or direct system calls any more. And they were all (well, almost all) written in C.

The one unifying factor of all of these new "languages" was that they had to be interpreted: type a command and the shell (ie, the interpreter) would run it. In the old days, this meant quite a performance hit; but now, modern JIT compilers (probably fuelled by Java) are very sophisticated and much faster, so the distinction is less obvious.

The main difference between Java and Perl (or "language" and "script") is the development process: Java was designed from the ground up as a language. Perl (and bash; not so sure about Python) is a basic syntax with an agglomeration of 'libraries' of "useful things to do".

Now don't get me wrong, I love scripts (old sysadmin myself), but you cannot expect a compiled language like Java to be as dynamic as an interpretive "language" like Perl.
What you can expect (because there are standards to back it up) is that when you write a Java program, it will behave consistently, in ALL cases (unless otherwise specified), on all platforms that support a JVM. I don't think Perl even makes that claim, because that's never what it was designed to do.

Java's success lies not in it's "dynamism", but in its versatility:
  • Write me a fast scientific calculator in Perl; especially one that can take byte streams or binary input with equal ease to strings or console typing.
  • Write me a database-housed Perl Inventory application.
  • Write me a moderately complex, but secure, web application in Perl.


  • Now I've no doubt that all the above are possible, but can Perl guarantee the safety of the code (and I don't mean freedom from hacking; I mean does it do what I expect, on any machine that I might run it on) - I doubt it, unless there's a new P.L.S./PVM that I haven't heard about.

    Until then...welcome to your new static language: a world where you design, rather than program; and where most good work is done in your head rather than existentially. It's a change, no doubt, but I think you'll be better for it.

    Winston

    Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1602 times.
    Similar Threads
    1. Am I too old? 2. Will Masters degree help? 3. Best technical school
    Dynamically Creating Variables
    Coloring Table Rows Based on Array Value
    Where are all the SCEAs?
    Can MS in Computer Science fetch jobs?
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 18:12:43.