If you ever want to port some java code TO xojo, or go the other way, its helpful to know some Java.
The languages are similar enough that moving code in either direction can be done (I wont say simply as there are some nuances about each language that you do need to be aware of otherwise you WILL get bitten hard)
For starters, both languages support single inheritance and interfaces. This is a good thing as it means you dont have to worry about complex inheritance hierarchies like you might get with C++ which permits multiple inheritance (literally one class that is derived from 2 or more parent classes - which can have its own attendant issues)
One thing that WILL cause you grief is Java IS case sensitive. Xojo is not.
So in Java classes like
Class Foo
End Class
Class foo
End Class
is 2 different classes.
This holds true for variable and parameter declarations as well.
So if you have
Class Foo
End Class
Foo foo = new Foo() ;
this is all legal and now the local variable, foo, holds a reference to an instance of Foo.
So be VERY careful when you port ode as this upper lower case distinction happens all the time in Java code and if you’re not watching it will bite you.
In Xojo you declare a variable using code like either of the following
dim i as integer
var j as double
you use the keyword DIM or VAR to introduce variable declarations. Then you name one or more and follow that with the AS keyword and a data type name.
Java expects that in a different order. In Java for the same two declarations you would do something like
int i ;
double j ;
It is type name followed by the names of t the variables.
One thing to be aware of with even these two simple statements is that in Xojo a newly declared variable will be set to the default value for the type. This is zero for numerics, an empty string for strings. And the color &c00000000 for colors. And false for booleans.
Java does NOT do this !
However the Java compiler does complain about using uninitialized variables.
So if you tried to just print out the values of those two variables after defining them, like,
int i ;
double j ;
System.out.println("i " + i + " j " + j ) ;
you would get compilation errors and the program would not compile.
In Xojo of course you CAN inisitalize the variable on the same line as the declaration like
dim i as integer = 909
var j as double = 3.45
And, you can do that in Java as well
int i = 909 ;
double j = 3.45 ;
System.out.println("i " + i + " j " + j ) ;
Something you will notice in the Java code I have posted so far is
System.out.println("i " + i + " j " + j ) ;
This is a general purpose output mechanism that will output a message to the terminal if the java code is run from the command line. A rough equivalent is System.debuglog in Xojo. But notice that I do not have to tell the integer or doubel to convert themselves to strings to print. This is handy.
As we move on you’ll find there are several other mechanisms in Java for getting formatted output to a terminal or other outputs.
One thing that is definitely going to be required is a java environment to test code out.
IF you already have Java installed great !
if you dont well there are plenty of JDK’s you can grab.
I would suggest either the official JDK from Oracle or one of the many well support Open JDK’s like the one from Azul
As long as you have the correct platform specific one of these installed this exploration should just work. There are subtle differences when you get way down deep in the weeds but we’re not going there.
At least not from the outset.
Once you have installed a JDK you should be able to open a terminal window and in there type
java -version
and get a response from Java about what version you installed
Grab one install it and I’ll see you for installment # 2 soon !