Created on 02/01/2025
Because of OOP courses, I have to practice Java. I coded with it a few years ago, so I do have a few bases, but I forgot most of the subtleties.
Here are some points that might interest you if you are forced want to learn Java and already learnt another programming language beforehand.
Scheme time!
JDK -- Compiler (javac)
|----- Tools (javadoc, javadb)
|----- JRE -- JVM (interpreter)
-------|----- Libraries
JDK enables to create Java programs, while JRE only executes them. In other words, JDK is for devs and JRE for consumers.
I thought that, like C++, default visibility was private
, but in reality it is like C#, internal
. Fields are said internal
when they can be used inside a class and are visible to other classes from the same package.
A constructor is said default when it doesn't require parameters. When you don't write explicit constructors for your class, Java's compiler creates one which automatically initializes primitives to zero (or false
for booleans) and null
for objects.
I expected Java to have default arguments, like most high-level languages do, but unfortunately it doesn't.
So instead of this:
class Complex {
private double m_real, m_imag;
public Complex(double real=0.0, double imag=0.0)
{
m_real = real;
m_imag = imag;
}
}
You have to write that:
class Complex {
private double m_real, m_imag;
public Complex() { /* zero-initialization go brrr */ }
public Complex(double real)
{
/*
* Calls the constructor with 2 arguments.
* This is equivalent to: `new Complex(real, 0)`,
* but it's clearer and more readable.
*/
this(real, 0);
}
public Complex(double real, double imag)
{
m_real = real;
m_imag = imag;
}
}
You can't. The only solution is to create a function which returns the desires result.
So instead of:
void inc(Integer i) {
i += 1;
}
You would write:
int inc(int i) {
return i + 1;
}
You cannot declare a function inside a function, at least regular ones. Having coded in C, I don't mind, but it's always good to know.
If you still want to have that behavior, you have to use functors.
Since Java 10 (sorry if your teacher or company forces you to use Java 8), you can use the var
keyword to automatically deduce the type of your variable. Although there are some edge cases which prevent you from using it (when using null
for example), it increases readability when used correctly.
public class TypeInference {
public static void main(String[] args)
{
var text = "I'm over Java 10";
assert text instanceof String;
}
}
The languages that I'm going to introduce have either similar (but simpler) syntax or interopability with Java, and correct some of its mistakes.
Haxe has one killer feature and does it well: it can be transpiled to
many programming languages, including Java. Strangely, you cannot declare a class inside a class (unless you're using anonymous structures), but nested functions are allowed.
class Main
{
static function main() {
trace("Hello World in Haxe!");
}
}
Fields are private by default.
It's Java, but with a much simpler syntax: no semi-colons, type inference, and no OOP forcing, phew. Note that in these three languages, variables cannot be null by default, which will save you from hours finding a subtle buf because of a NullPointerException
.
fun main() {
println("Hello world!")
}
It's an optional parameter, but if you need it, you can write your main function like this: fun main(args: Array<String>)
Based on Java but more functional. I haven't got enough time to try it yet.
// one-liner
println("Hello world!")
Main function is not necessary, but you can create one which allows the program to accept arbitrary number of arguments (not available when using online playground).
@main def createTmdFile(name: String, title: String, draft: Boolean=true) = {
println("Creating the file ${name}.tmd")
}
Hoping these languages interest you.
— pgmtx