The $ operator for jobjRef Java object references provides convenience access to object attributes and calling Java methods.

# S3 method for class 'jobjRef'
.DollarNames   (x, pattern = "" )
  # S3 method for class 'jarrayRef'
.DollarNames (x, pattern = "" )
  # S3 method for class 'jrectRef'
.DollarNames  (x, pattern = "" )
  # S3 method for class 'jclassName'
.DollarNames(x, pattern = "" )

Arguments

x

object to complete

pattern

pattern

Methods

$

signature(x = "jobjRef"): ...

$

signature(x = "jclassName"): ...

$<-

signature(x = "jobjRef"): ...

$<-

signature(x = "jclassName"): ...

names

signature(x = "jobjRef"): ...

names

signature(x = "jarrayRef"): ...

names

signature(x = "jrectRef"): ...

names

signature(x = "jclassName"): ...

Details

rJava provides two levels of API: low-level JNI-API in the form of .jcall function and high-level reflection API based on the $ operator. The former is very fast, but inflexible. The latter is a convenient way to use Java-like programming at the cost of performance. The reflection API is build around the $ operator on jobjRef-class objects that allows to access Java attributes and call object methods.

$ returns either the value of the attribute or calls a method, depending on which name matches first.

$<- assigns a value to the corresponding Java attribute.

names and .DollarNames returns all fields and methods associated with the object. Method names are followed by ( or () depending on arity. This use of names is mainly useful for code completion, it is not intended to be used programmatically.

This is just a convenience API. Internally all calls are mapped into .jcall calls, therefore the calling conventions and returning objects use the same rules. For time-critical Java calls .jcall should be used directly.

See also

Examples


v <- new(J("java.lang.String"), "Hello World!")
v$length()
#> [1] 12
v$indexOf("World")
#> [1] 6
names(v)
#>  [1] "CASE_INSENSITIVE_ORDER" "equals("                "length()"              
#>  [4] "toString()"             "hashCode()"             "getChars("             
#>  [7] "compareTo("             "compareTo("             "indexOf("              
#> [10] "indexOf("               "indexOf("               "indexOf("              
#> [13] "valueOf("               "valueOf("               "valueOf("              
#> [16] "valueOf("               "valueOf("               "valueOf("              
#> [19] "valueOf("               "valueOf("               "valueOf("              
#> [22] "codePoints()"           "isEmpty()"              "charAt("               
#> [25] "codePointAt("           "codePointBefore("       "codePointCount("       
#> [28] "offsetByCodePoints("    "getBytes("              "getBytes("             
#> [31] "getBytes("              "getBytes()"             "contentEquals("        
#> [34] "contentEquals("         "equalsIgnoreCase("      "compareToIgnoreCase("  
#> [37] "regionMatches("         "regionMatches("         "startsWith("           
#> [40] "startsWith("            "endsWith("              "lastIndexOf("          
#> [43] "lastIndexOf("           "lastIndexOf("           "lastIndexOf("          
#> [46] "substring("             "substring("             "subSequence("          
#> [49] "concat("                "replace("               "replace("              
#> [52] "matches("               "contains("              "replaceFirst("         
#> [55] "replaceAll("            "split("                 "split("                
#> [58] "join("                  "join("                  "toLowerCase()"         
#> [61] "toLowerCase("           "toUpperCase("           "toUpperCase()"         
#> [64] "trim()"                 "strip()"                "stripLeading()"        
#> [67] "stripTrailing()"        "isBlank()"              "lines()"               
#> [70] "chars()"                "toCharArray()"          "format("               
#> [73] "format("                "copyValueOf("           "copyValueOf("          
#> [76] "intern()"               "repeat("                "wait("                 
#> [79] "wait("                  "wait()"                 "getClass()"            
#> [82] "notify()"               "notifyAll()"           


J("java.lang.String")$valueOf(10)
#> [1] "10.0"

Double <- J("java.lang.Double")
# the class pseudo field - instance of Class for the associated class
# similar to java Double.class
Double$class
#> [1] "Java-Object{class java.lang.Double}"