Core concepts
Classes and objects#
A Java object stays in Java. Ring holds a handle (a number) in a Ring object, and the bridge keeps the Java object in a table. When the last Ring variable that holds the object is gone, the handle is released (see How it works). oObject.javaRelease() releases it at once.
using { "java.util" } loads the Ring classes of the package. Each Ring class has the name of the Java class (ArrayList) and extends the Ring class of its Java super class. A nested class has the names joined: java.util.Map.Entry is MapEntry. If two packages have a class of the same name, the first one of the manifest keeps the name and the other gets the package in front of it: java.util.List is List and java.awt.List is AwtList (see libraries/javalib/generated/classindex.txt for every name).
An object that comes back from Java gets the Ring class of its Java class, or of its nearest super class or interface that has one. The package of that class is imported automatically when it was not named in using.
Making objects#
oList = new ArrayList # the default object (the constructor without values)
oSB = new StringBuilder("text") # the constructor with the smallest number of values (one or more)
oRect = new Rectangle(1,2,30,40)
oFile = new File("a.txt")Ring runs init() only when the parentheses have values, and it needs the exact number of values. So a Ring class has one init that takes the values of the smallest constructor of one or more parameters, and new X without parentheses makes the default object (when the Java class has a constructor without values). new X() with empty parentheses works only for the classes that have no constructor with values (for example Math, Collections). For the other constructors:
oList = new ArrayList
oList.@New([ 100 ]) # any constructor, the arguments are a list
oThread = jNew("java.lang.Thread",[oRunnable,"worker"]) # the dynamic functionnew Integer (without parentheses), new Math() and new Arrays make static holders: an object with no Java object behind it, used to call static methods and read constants.
The classes of java.lang that are used this way are ready as global objects, so you write System.currentTimeMillis(), Math.max(3,9), Integer.MAX_VALUE, String.join("-",oList) or Thread.sleep(100) without new: System, Math, StrictMath, Integer, Long, Double, Float, Short, Byte, Boolean, Character, String, Thread and Runtime (made in libraries/javalib/statics.ring). They are Ring variables, so Ring's own system("dir") and string(5) still work, and new String("abc") still makes a Java string. For the classes of other packages make the holder yourself: Arrays = new Arrays, then Arrays.sort(aItems).
Methods#
? oSB.length()
oSB.append("x").append(5) # an answer that is an object can be used at onceThe values you pass are converted by the type of the parameter (see below). Ring has no overloading and needs the exact number of values, so, as in RingDotNet:
- a Java method name that has one number of parameters is one Ring method with these parameters:
oM.max(3,9),oList.get(0)isoList.@get(0); - a Java method name that has a form without parameters and forms with parameters is two Ring methods:
name()is the form without parameters, and@name(x)takes the forms with parameters as one argument:oText.toUpperCase()andoText.@toUpperCase(oLocale),oMatcher.find()andoMatcher.@find(5),oField.getText()andoField.@getText([0,5]),oRandom.nextInt()andoRandom.@nextInt(1000); - a Java method name that has several overloads with different numbers of parameters and none without parameters is one Ring method with one argument (
oText.substring(5),oText.substring([5,8]));
The one argument of @name(x) (and of the method of the last kind) is a hash list or a value, and it chooses the overload:
| You pass | What happens | Example |
|---|---|---|
a list of :name = value pairs (a hash list) | the version with these parameter names (see below) | oList.add([ :index = 0 , :element = "x" ]), oSb.@append([ :str = "x" , :start = 0 , :end = 1 ]) |
| a value | the version with one parameter | oText.substring(5), oRandom.@nextInt(1000) |
| a plain list, when no version takes the list as its one parameter | the values in order | oText.substring([5,8]), String.format(["%s-%s",["x","y"]]) |
The function index of each package (api-*.html) shows the two forms of every method.
| Ring | Java |
|---|---|
@get(0) | a word that Ring does not accept after a dot (get, put, next, end, new, load, class, import, call...) gets an @ at the start; when it has the two forms above, they are @get() (no parameters) and @@get(x) |
oText.toString() | every object has toString(); the forms with parameters are @toString(x): Integer.@toString(255) |
callMethod("name",[args]) of every object calls any version with the values in order: oList.callMethod("add",[0,"x"]). The bridge still picks the real overload from the types of the values. A list that is an item (add(E) takes any object) is passed as the item, so use the names to choose a version of such a method.
Named values: the parameters as a list of key/value pairs#
A list of key/value pairs is the one value of a method that has several overloads, as in RingDotNet. The names are the names of the Java parameters (add(int index, E element)), the order does not matter, and the bridge finds the overload that has these names:
oList.add([ :index = 0 , :element = "x" ])
oBig = new ArrayList([ :initialCapacity = 50 ]) # constructors: init() takes the list as its one value
oCopy = (new ArrayList).@New([ :c = oList ]) # any other constructor: @New(...)
jStatic("java.lang.Math","max",[ [ :a = 3 , :b = 9 ] ]) # the dynamic API: the list is the one item of aArgsThe constructors: init(...) takes the values of the main constructor (the smallest of one or more parameters), new X without parentheses makes the default object, and @New(NULL), @New([ :name = value ]) or @New([ value1, value2 ]) makes the object with any constructor.
A name that has only one number of parameters (Math.max(a,b)) stays positional. The names of the parameters come from the Java sources (bin/paramnames.txt, made by tools/javatools/DumpParams.java from the src.zip of the JDK) and for your own classes from the classes compiled with javac -parameters. jParamNames("java.util.ArrayList","add") shows the names of every version of a method. A name that matches no version is an error that lists the candidates.
Values#
| Ring | Java parameter |
|---|---|
| a text | String, CharSequence, char (one letter), byte[] (the bytes), an enum (by its name), Object |
| a whole number | int, long, short, byte, double, float, Integer..., Number, Object, boolean (0 or 1), BigInteger, BigDecimal |
| a number with a fraction | double, float, Double, Number, Object, BigDecimal |
| a Java object | any parameter that accepts it |
| a list | an array of any type (["a","b"] for String[]), List, Collection, Iterable, Set, a varargs parameter |
jNull() | any parameter that is not a primitive |
"name" or "code" (jCallback("name") is the explicit form) | an interface (a Runnable, a Comparator...), see Callbacks and threads |
jInt(5), jLong, jFloat, jChar("a"), jBool... | exactly that type, when the overload must be chosen by you (list.remove(jInt(0)) removes by index) |
The bridge compares the values with every overload that has this number of parameters and takes the cheapest match (an int parameter for a whole number, a String for a text, then wider types). An error that lists the candidates tells when nothing matches.
What comes back: String is a Ring text (UTF-8; Java works in UTF-16 and the conversion is automatic), the numbers are Ring numbers, boolean is 1 or 0, a char is a text of one letter, byte[] is a Ring text with the bytes, null is a null object (jIsNull(x), x.isNull()), anything else is an object. Use oObject.toList() for an array, a collection, an iterable, an iterator or a map (a list of [key, value]).
Static members#
oMath = new Math
? oMath.max(3,9)
? oMath.PI
? Integer.MAX_VALUE
System.out.@println("hello")or without Ring classes: jStatic(class, method, [args]), jGetStatic(class, field), jClass("java.lang.Math").
Fields#
A public field is a Ring attribute: oPoint.x, oPoint.x = 10, Math.PI. Fields that Java does not allow to change (final) are read only. A field is not generated when its name is also the name of a method of the class (getField / setField read and write any field by its name).
Errors#
A Java exception is a Ring error: try ... catch ... done and cCatchError have Java Exception: java.lang.NumberFormatException: For input string: "abc" and the first lines of the Java stack. jLastException() is the exception object.
try
? Integer.parseInt("abc")
catch
? cCatchError
done