Beginning Java for Xojo programmers I

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 !


I’m not sure if this is the place to write some comments and how deep you plan to go into Java because I do not want to disrupt your tutorial series, so feel free to move my post somewhere else.

There are cases where they do get default values like in Xojo for some primitive datatypes, like when defined as public or static. e.g will work:

public boolean val1;
public double val2;
public float val3;
static int val4;
static long val5;
static String val6;

public void Test() {		
	System.out.println("Default values.....");
	System.out.println("Val1 = " + val1); // false
	System.out.println("Val2 = " + val2); // 0.0d
	System.out.println("Val3 = " + val3); // 0.0f
	System.out.println("Val4 = " + val4); // 0
	System.out.println("Val5 = " + val5); // 0L
	System.out.println("Val6 = " + val6); // null	

But as a general rule, you are probably always better of giving it a default value as you described.


In this moment I found out that I always wrote my code case sensitive and I was always initializing variables after declare when I used xojo. Seams it is so while I came from java side to xojo.

Indeed you are correct
There are many places where Java has special cases when it will initialize things
I dont try to remember all those little edge cases and often just set a default myself so I dont have to think about that.
That java DOES complain about uninitialized variables IS a good thing

wonder if a version of Morpho :butterfly: for Java would be a good investment of time?

For Geometry Solving I use Opencascade Technology in Java cause there’s a really nice Wrapper from Opencascade for their geometry solver and their 3D Surfacing and Modelling. So I was never thinking of using Morpho. That is not only a geometry Solver but 3 complete 2D, 3D, Hidden Line Generation industrial Level CAD kernel which allows all the optional operations morpho is doing for you as normal in their geometry Solver. Within the constraints Manager I was building many solutions.

you misunderstand what Morpho is… right now the only existing version converts Xojo/BASIC into Swift… I was wondering if it would be worth the effort to create a version that converts Xojo/BASIC into JAVA… to serve as a learning tool, for those who (like myself) don’t know much JAVA

And I thought you mean the morpho lib for geometry solving and constraint management. Sorry for that. A transpirier for xojo to java would open the world also for outside the Mac world

I am waiting with bated breath for the next on topic post…