ENTRY

[ESC]
1h712 words

Experience report in learning Java

Over the last few weeks, I've been working my way through a text-based Java course from the university of Helsinki, while having Claude Sonnet open in another tab.

I chose a text-based course because I prefer material I can work with at my own pace, where I can freely skip back and forth to the parts I need at any moment. This was especially important here because I wasn't a beginner at programming and already had some decent skill with Python and Go.

Whenever I had a question about something unclear in the course, about how things worked under the hood or about how Java compared to other languages, I would direct those at Claude, which had overall very positive results and helped me pick up a lot of bits and pieces beyond the utilitarian scope of the course.

I started out using NetBeans, because it reminded me of Eclipse which I had some memories of using nearly two decades ago, but switched to IntelliJ partway through. Between the two I think I prefer IntelliJ, though I don't have enough experience actually being productive to make an informed judgement.

Among the things I learned:

  • My first big surprise was that in Java, everything you write is a class. Even the main function is actually the main method on a projects core/nominal class.
    If you do want the closest equivalent to individual functions that aren't part of an object, then you'll be using 'Final' classes with 'static' methods. A static method is one that is associated with the class itself, rather than any object derived from it. In this manner, you can create a package-like namespace in the form of a Class.method(something).
  • Java has a good bit of similarity with python in the way that variables contain references to values/objects, rather than the values themselves. I also finally understood what people meant when they said "python variables are references".
    These references aren't intended to me manually interacted with in the way you'd deal with C pointers, or even with the much more limited Go pointers.
    A consequence of the reference approach is that much of the time, if you hand an object to a method as an argument and then change something on that argument inside the method, the original is equally changed because both references (Original variable and scoped argument variable) refer to the same underlying object.
  • The reason Java and Python don't have expressions like i++ and instead require you to use i = i + 1 is because the underlying integer is immutable, you can't change it. So instead of just adding one to the number, you have to replace the reference to the old number with a reference to a newly created number object which has the value of i + 1. Hence i = i + 1.
  • Apparently, other languages such as Kotlin also use the JVM (Java Virtual Machine), which is the intermediate abstraction built on top of a processors individual hardware and assembly language, and which permitted the "Write once, run anywhere" slogan.
  • The contrast in how Java and Go use interfaces was interesting. In Go, a struct (the object/class analogue) fulfills an interface automatically if it has the right methods. This is used as part of the type system to create generalizable code that e.g. can interact with any 'Reader', meaning any object with a .read() method which can be used to read out bytes.
    In Java, interfaces are used similarly as part of the Type system for polymorphism, but also to get around the limitation of singular inheritance, that each class can only inherit from one parent class, no more.
  • Whether a method throws an exception that must be handled (as enforced by the compiler) is defined in the method signature.
  • Streams are interesting, but a bit tricky. At first I thought them similar to python list comprehensions, and while they can be used in a similar way, they are many other step-sequences you could go down.

Overall, I don't think I'll be choosing Java for any of my own projects, but I'm glad to have learned a bit about it for what it taught me about the landscape of programming languages.

0 replies

Join the conversation