Skip to contents

.jequals function can be used to determine whether two objects are equal. In addition, it allows mixed comparison of non-Java object for convenience, unless strict comparison is desired.

The binary operators == and != are mapped to (non-strict) call to .jequals for convenience.

.jcompare compares two objects in the sense of the java.lang.Comparable interface.

The binary operators <, >, <=, >= are mapped to calls to .jcompare for convenience

Usage

.jequals(a, b, strict = FALSE)
.jcompare( a, b )

Arguments

a

first object

b

second object

strict

when set to TRUE then non-references save for NULL are always treated as different, see details.

Value

.jequals returns TRUE if both object are considered equal, FALSE otherwise.

.jcompare returns the result of the compareTo java method of the object a applied to b

Methods

!=

signature(e1 = "ANY", e2 = "jobjRef"): ...

!=

signature(e1 = "jobjRef", e2 = "jobjRef"): ...

!=

signature(e1 = "jobjRef", e2 = "ANY"): ...

==

signature(e1 = "ANY", e2 = "jobjRef"): ...

==

signature(e1 = "jobjRef", e2 = "jobjRef"): ...

==

signature(e1 = "jobjRef", e2 = "ANY"): ...

<

signature(e1 = "ANY", e2 = "jobjRef"): ...

<

signature(e1 = "jobjRef", e2 = "jobjRef"): ...

<

signature(e1 = "jobjRef", e2 = "ANY"): ...

>

signature(e1 = "ANY", e2 = "jobjRef"): ...

>

signature(e1 = "jobjRef", e2 = "jobjRef"): ...

>

signature(e1 = "jobjRef", e2 = "ANY"): ...

>=

signature(e1 = "ANY", e2 = "jobjRef"): ...

>=

signature(e1 = "jobjRef", e2 = "jobjRef"): ...

>=

signature(e1 = "jobjRef", e2 = "ANY"): ...

<=

signature(e1 = "ANY", e2 = "jobjRef"): ...

<=

signature(e1 = "jobjRef", e2 = "jobjRef"): ...

<=

signature(e1 = "jobjRef", e2 = "ANY"): ...

Details

.jequals compares two Java objects by calling equals method of one of the objects and passing the other object as its argument. This allows Java objects to define the `equality' in object-dependent way.

In addition, .jequals allows the comparison of Java object to other scalar R objects. This is done by creating a temporary Java object that corresponds to the R object and using it for a call to the equals method. If such conversion is not possible a warning is produced and the result it FALSE. The automatic conversion will be avoided if strict parameter is set to TRUE.

NULL values in a or b are replaced by Java null-references and thus .jequals(NULL,NULL) is TRUE.

If neither a and b are Java objects (with the exception of both being NULL) then the result is identical to that of all.equal(a,b).

Neither comparison operators nor .jequals supports vectors and returns FALSE in that case. A warning is also issued unless strict comparison was requested.

Note

Don't use x == NULL to check for null-references, because x could be NULL and thus the result would be an empty vector. Use is.jnull instead. (In theory is.jnull and x == .jnull() are the the same, but is.jnull is more efficient.)

See also

Examples

s <- .jnew("java/lang/String", "foo")
.jequals(s, "foo") # TRUE
#> [1] TRUE
.jequals(s, "foo", strict=TRUE) # FALSE - "foo" is not a Java object
#> [1] FALSE
t <- s
.jequals(s, t, strict=TRUE) # TRUE
#> [1] TRUE

s=="foo" # TRUE
#> [1] TRUE


Double <- J("java.lang.Double")
d1 <- new( Double, 0.0 ) 
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "newInstance", class,     .jarray(p, "java/lang/Object", dispatch = FALSE), .jarray(pc,         "java/lang/Class", dispatch = FALSE), evalString = FALSE,     evalArray = FALSE, use.true.class = TRUE): RcallMethod: cannot determine object class
d2 <- new( Double, 1.0 )
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "newInstance", class,     .jarray(p, "java/lang/Object", dispatch = FALSE), .jarray(pc,         "java/lang/Class", dispatch = FALSE), evalString = FALSE,     evalArray = FALSE, use.true.class = TRUE): RcallMethod: cannot determine object class
d3 <- new( Double, 0.0 )
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "newInstance", class,     .jarray(p, "java/lang/Object", dispatch = FALSE), .jarray(pc,         "java/lang/Class", dispatch = FALSE), evalString = FALSE,     evalArray = FALSE, use.true.class = TRUE): RcallMethod: cannot determine object class

d1 < d2
#> Error: object 'd1' not found
d1 <= d3
#> Error: object 'd1' not found
d1 >= d3
#> Error: object 'd1' not found
d1 > d2
#> Error: object 'd1' not found

# cannot compare a Double and a String
try( d1 < "foo" )
#> Error in eval(expr, envir) : object 'd1' not found

# but can compare a Double and an Integer
d1 < 10L
#> Error: object 'd1' not found

#> Error: object 'd1' not found