Using Java Strings and the Swing API in R
In the first article, "Getting Started with R Using Java," we started with installing R and installing the rJava package. We discussed initializing the JVM, setting the classpath, creating a Java object, and calling a Java method. In this tutorial, we shall discuss the rJava package to use Java Strings and the Swing Java API from R. This tutorial has the following sections:
Using Strings was introduced by creating a new object from the java.lang.String class and a supplied String as constructor arg.
> s <- .jnew("java/lang/String", "Hello World!")
The jstrVal function could be used to output the String value.
> .jstrVal(s)  "Hello World!" >
Any of the String class methods could be called by using the String object "s". As an example, call the instance method indexOf(String str) to find the index of a substring such as "Hello."
The return value is an int with a value of 0, indicating the index of "Hello" in "Hello String!"
Alternatively, the short form could be used to call the indexOf(String str) method as follows to output int 0.
> s$indexOf("Hello")  0
The R Console output from the preceding method calls is as follows, as shown in Figure 1.
Figure 1: R Console output from the preceding method calls
As an example, call the static method valueOf(int i) in the String class.
The int value as a String is returned.
 "10.0" >
The R Console output from the preceding command is shown (see Figure 2).
Figure 2: The R Console output from the preceding command
As another example, call the concat(String str) method in the java.lang.String class using the .jcall(obj, returnSig = "V", method, ...) function, supplying the object reference as the "s" String object used from the start in this tutorial, a return type signature for the String class, and the method to call as "concat" with the "s" object reference as the concat method arg.
The value output is the Strings concatenated.
 "Hello World!Hello World!" >
The output from the preceding function call is shown in R Console, as referenced in Figure 3.
Figure 3: The output from the preceding function call, shown in R Console
A new String class reference could be created by using the J high level API, as follows.
> String <- J("java.lang.String") >
Or, a new String object reference could be created by using the "new" keyword.
> s <- new(String)
Subsequently, call the concat method on the String object reference to concatenate the "Hello" String literal to the String object "s".
> s <-s$concat("Hello")
Print the String object "s".
A String, "Hello", should get output.
 "Hello" >
The .jequals(a, b, strict = FALSE) function may be used to find if two objects are equal. The .jequals allows a mixed comparison of non-Java objects is "strict" is FALSE, the default. For example, create a String object reference "s" as before but using String ""Hello" instead of "Hello World!"
> s <- .jnew("java/lang/String", "Hello")
Find if the "s" object reference is equal to the R object "Hello".
> .jequals(s, "Hello")
An output of TRUE indicates the objects are equal.
In the preceding object comparison, the scalar R object "Hello" is converted to a String object. But, if "strict" is set to TRUE, the result would be otherwise because the R scalar "Hello" is not converted to a String.
> .jequals(s, "Hello", strict=TRUE)  FALSE
If two String objects represent the same Java object, the output would be TRUE even with strict=TRUE. As an example, create another Java object reference called "t" from the object reference s".
> t <- s
Compare "t" and "s" with strict=TRUE.
> .jequals(s, t, strict=TRUE)
An output of TRUE is generated, indicating that the Java objects are equal.
The == binary comparison operator also could be used to compare two Strings. As an example, compare the String object "s" with R scalar "Hello".
A value of TRUE is returned. The == operator calls the non-strict version of the .jequals method.
 TRUE >
A Swing application could be created similarly by calling the .jnew an .jcall functions. For example, create a java.awt.Frame class object with a "Hello World" constructor arg and subsequently call the setVisible method on the Frame object with the boolean arg TRUE to show the Window object.
> f <- .jnew("java/awt/Frame", "Hello World") > .jcall(f,, "setVisible", TRUE)
The preceding function calls do not generate any output. This is shown in Figure 4.
Figure 4: No output is generated
But, a Window object with the "Hello World" title gets displayed (see Figure 5).
Figure 5: A Window object with the "Hello World" title is displayed
Similarly, other GUI components could be added to the Frame. As an example, add a Button object with the "OK" label to a Window object with the "Hello" title.
> f <- .jnew("java/awt/Frame", "Hello") > b <- .jnew("java/awt/Button", "OK") > .jcall(f, "Ljava/awt/Component;", "add", .jcast(b, "java/awt/Component"))
Subsequently, pack and show the Window object.
> .jcall(f,, "pack") > .jcall(f,, "setVisible", TRUE)
The .jcall function call to add the Button to the Frame returns a Java Object, as shown in Figure 6.
Figure 6: A Java Object is returned
Subsequently, a Window with a button is displayed (see Figure 7).
Figure 7: A Window with a button is displayed
The .jinstanceof function is used to find if a given Java object is an instance of a given Java class. With "o" as the Java object and "cl" as the Java class, the function could be used with either of the following notations.
o %instanceof% cl .jinstanceof( o, cl )
As an example, find if an object is an instance of class Double. First, create a Java class reference for the Double class using J, the high level API for Java. Subsequently, create a Double object reference from double "1.2".
> Double <- J("java.lang.Double") > d <- new( Double, "1.2" )
Find if the Double object reference "d" is an instance of the java.lang.Double class.
> d %instanceof% "java.lang.Double"
The output should be as follows.
Find if the "d" object reference is an instance of the java.lang.Number class. And, the output is again TRUE.
> d %instanceof% "java.lang.Number"  TRUE
The "d" object reference is definitely not an object of type java.lang.Integer, but to verify, use the .jinstanceof function.
> d %instanceof% "java.lang.Integer"
The output is FALSE.
The "Double" class reference also could be used instead of the fully qualified Java class java.lang.Double.
> d %instanceof% Double  TRUE
Output from the preceding function calls in R Console is shown in Figure 8.
Figure 8: Output from the preceding function calls in R Console
In the next article, "Using the rJava R Package to Do More," we shall explore some more features of Java with R, such as checking for Exceptions, using arrays and collections, casting a Java object, and comparing Java references.