Skip to content

Instantly share code, notes, and snippets.

@bananu7
Last active December 15, 2015 02:29
Show Gist options
  • Save bananu7/5187972 to your computer and use it in GitHub Desktop.
Save bananu7/5187972 to your computer and use it in GitHub Desktop.

C++ coder in a Java world, part 1.

Disclaimer

These are my personal opinions and feelings that arose from very limited exposure to Java. Whilst I am fairly experienced with C++, the Java world is new for me. Whilst it is very hard to keep an unbiased opinion in such case, I have tried my best. So here, as-is, hands-on, are written my thoughts.

Intro

I wouldn't even pick up Java if not for mandatory university classes. I was of course aware of its existence, but it always stayed for far-enough comfort zone I haven't even had to touch. No more running away, though, the situation got real and I have launched the IDE.

I have worked with fairly new NetBeans IDE. AFAIR, it is written in Java and for Java, so I expected at least nice integration (my experiences in coding Python or C++ in NetBeans were at most "Meh."). The project creation is fairly simple, and allows you to choose from variety of targets. I suspect they actually work, contrary to Visual Studio project targets - anything different than native C++ console or Win32 app is just shooting yourself in the foot.

The first assignment was to write a simple directory listing command, similar to ls linux command. OK, let's dive into code.

The code

[[[ Insert java main code here ]]]

Ooo-keeeyyy.... I've seen that in C# before, I don't really give a damn if the main is in class or not. Okey, the assignment requires us to create our own model of directory, just for kicks. So, new class. Surprise, the wizard actually works and produces usable snippet.

Okay, so the model will be like

public class MyTree {
    public enum FileType {
        FILE, DIRECTORY, OTHER
    }
    Date date;
    String name;
    FileType type;
    // this is only used when it's a directory
    java.util.TreeSet<MyTree> children;
}

So far, so good. Of course, I didn't know all these, but googling was fairly simple. Apart from explicit public before enum, looks pretty standard C++. And oh, we keep elements on set "by-value", but of course it hardly matters here (I guess?).

Apples to oranges

However, there's one problem: TreeSet needs a comparable type. That makes sense. In C++, I'd just define operator<, however Java isn't so smart, so we need... an interface.

Yeah, Java starts to terribly suck here. It can't just check if my class has some properties like templates in C++ - I have to tell it explicitly what to look for. So:

public class MyTree implements Comparable<MyTree> {

Okay, so we can compare it to itself. And the implementation:

@Override
public int compareTo(MyTree rhs) {
    return name.compareTo(rhs.name);
}

No operators, obviously, but since both String and Date inherit from Comparable, we can use compareTo method. Well, not so pretty, but works.

Reading

Okay, I want to read it from OS now. I've done recursive classes like that one at least a few times, so it shouldn't be hard, right?

public void read (File file) {
    date = new Date(file.lastModified());
    name = file.getName();
    if (file.isDirectory())
        type = FileType.DIRECTORY;
    else if (file.isFile())
        type = FileType.FILE;
    else
        type = FileType.OTHER;
    File[] listOfFiles = file.listFiles();
    if(listOfFiles != null) {
        children = new java.util.TreeSet<>();
        for (int i = 0; i < listOfFiles.length; i++) {
            MyTree temp = new MyTree();
            temp.read(listOfFiles[i]);
            children.add(temp);
        }
    }
}

Let's break it line-by-line. First weird thing, File file can mean both file and a directory, which makes it a perfect start point for our function. It supports stuff like .lastModified() and .getName() (well, a bit inconsistent here, but who cares - autocompletion saved me some googling). isDirectory() was also particularly helpful - actually that, and other classes have a lot of methods like this. I wouldn't like to ever implement it, but certainly does its job.

Moving on, OMG, array. Or list. Or whatever! I don't care, it works, so... The code is simple, and the for loop looks almost exactly alike C++, except length isn't a function. Wait, what?!.

Let's sum it up:

  • .isDirectory()
  • .lastModified()
  • .getName()
  • .length

Umm, yeah, that didn't really help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment