createJavaObject: Create Java objects

Description Usage Arguments Value See Also Examples

View source: R/j4rFunctions.R

Description

This function creates one or many object of a particular class. If the parameters contain vectors, then a series of instances of this class can be created. Primitive type are converted on the fly, numeric to double, integer to int, logical to boolean and character to String. Factors are also converted to String.

Usage

1
2
3
4
5
6
7
createJavaObject(
  class,
  ...,
  isNullObject = FALSE,
  isArray = FALSE,
  affinity = 1
)

Arguments

class

the Java class of the object (e.g. java.util.ArrayList)

...

the parameters to be passed to the constructor of the object

isNullObject

a logical that indicates whether the instance should be null (by default it is set to FALSE)

isArray

a logical that indicates whether the instance is an array. By default, it is set to FALSE. When creating an array, the parameters must be integers that define the dimensions of the array

affinity

a parameter used by the mclapply.j4r function in case of multithreading.

Value

a java.object or java.list instance in the R environment

See Also

J4R webpage

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
### starting Java
connectToJava(memorySize = 200)

### creating an empty ArrayList object
createJavaObject("java.util.ArrayList")

### creating an ArrayList instance with initial capacity of 3
createJavaObject("java.util.ArrayList", as.integer(3))

### creating two ArrayList with different capacities
createJavaObject("java.util.ArrayList", c(as.integer(3), as.integer(4)))

### creating a 3x3 array of integers
myArray <- createJavaObject("int", 3, 3, isArray = TRUE)

### creating two arrays of integers with length 3
myArrays <- createJavaObject("int", c(3,3), isArray = TRUE)

### shutting down Java
shutdownJava()

J4R documentation built on July 23, 2020, 9:06 a.m.