4.59. kink/javahost/JAVA

JAVA mod provides a way to operate on Java objects, values, classes and methods.

Host system integration provided by JAVA mod is seamfull. There is a clear boundary between Kink world and Java world. For example, a Java method cannot be called directly as a Kink fun like JRuby or Groovy. Instead, you have to use .call_method:

Java_obj.call_method('toString')

There are several reasons behind the design.

1) Kink is fundamentally different from Java, both in the object system and the execution semantics. Thus, seamless integration is hard to achieve.

2) Generally, nontrivial software can benefit from a boundary layer between distinct systems.

3) With a clear boundary, Kink can evolve independent of Java.

4.59.1. type java

A `java` val comprises a pair of (objectReference, staticType), where objectReference is the Java object reference, and staticType is the class object of the static type.

The static type is used to find instance methods and instance fields.

4.59.1.1. J.static_type

`static_type` returns a java val of the static type.

The result of `static_type` is a java val (st, java.lang.Class), where `st` is a class object which represents the static type of the java val.

Example:

:JAVA.require_from('kink/javahost/')
:Comparable_class <- JAVA.class('java.lang.Comparable')
:Static_type <- JAVA.string('foo').as(Comparable_class).static_type
stdout.print_line(Static_type.repr)
# Output:
#   (java interface java.lang.Comparable as java.lang.Class)

4.59.1.2. J.dynamic_type

`dynamic_type` returns getClass() of the Java object referenced by `J`.

Precondition: the Java object reference of `J` must not be null.

The result of `dynamic_type` is a java val (dt, java.lang.Class), where `dt` is a class object which is the runtime class of the Java object reference of `J`.

Example:

:JAVA.require_from('kink/javahost/')
:Comparable_class <- JAVA.class('java.lang.Comparable')
:Dynamic_type <- JAVA.string('foo').as(Comparable_class).dynamic_type
stdout.print_line(Dynamic_type.repr)
# Output:
#   (java class java.lang.String as java.lang.Class)

4.59.1.3. J.typable_as?(Klass)

`typable_as?` returns whether the reference of `J` is “typable as” `Klass`.

The reference is “typable as” `Klass` if and only if:

1) the reference is null and `Klass` is a reference type, or

2) the reference is an instance of the `Klass`, or

3) the reference is an instance of Boolean, Character, Byte, Short, Integer, Long, Float or Double, and `Klass` is the corresponding primitive type class.

Precondition: `Klass` must be a java val of which reference is an instance of java.lang.Class.

The result of `typable_as?` is Kink bool.

Example:

:JAVA.require_from('kink/javahost/')
:Integer_class <- JAVA.class('java.lang.Integer')
:Long_class <- JAVA.class('java.lang.Long')
:Object_class <- JAVA.class('java.lang.Object')

# null can be typed as any reference types
stdout.print_line(JAVA.null.typable_as?(Integer_class).repr)  # => true
stdout.print_line(JAVA.null.typable_as?(JAVA.int_class).repr) # => false

# int(42), the reference of which is Integer.valueOf(42),
# is typeable as int, Integer and Object, but not as long or Long.
:Int <- JAVA.int(42)
stdout.print_line(Int.typable_as?(Integer_class).repr)    # => true
stdout.print_line(Int.typable_as?(JAVA.int_class).repr)   # => true
stdout.print_line(Int.typable_as?(Long_class).repr)       # => false
stdout.print_line(Int.typable_as?(JAVA.long_class).repr)  # => false
stdout.print_line(Int.typable_as?(Object_class).repr)     # => true

4.59.1.4. J.as(Klass)

`as` makes a new java val changing the static type to `Klass`.

Precondition: `Klass` must be a java val (klass, any static type), where `klass` is an instance of java.lang.Class and `J` is “typeable as” `klass`.

The result of `as` is a java val (ref, klass), where ref is same as the object reference of `J`.

Example:

:JAVA.require_from('kink/javahost/')
:Integer_class <- JAVA.class('java.lang.Integer')

:Int <- JAVA.int(42)
stdout.print_line(Int.repr)                   # => (java class java.lang.String as java.lang.Class)
stdout.print_line(Int.as(Integer_class).repr) # => (java 42 as java.lang.Integer)

Note that you cannot perform widening or narrowing operations using this method. If you convert 42 to 42L for example, use constructor funs such as JAVA.long explicitly.

:JAVA.require_from('kink/javahost/')

:Int <- JAVA.int(42)
stdout.print_line(Int.repr)  # => (java 42 as int)

:Long <- JAVA.long(Int.to_kink_num)
stdout.print_line(Long.repr) # => (java 42 as long)

4.59.1.5. J.eq_eq?(Another)

`eq_eq?` returns whether the referenes of `J` and `Another` are identical in the meaning of Java == operator.

Example:

:JAVA.require_from('kink/javahost/')

# null is eq_eq? null, irrespective of their static types
:Null_string <- JAVA.null.as(JAVA.class('java.lang.String'))
:Null_integer <- JAVA.null.as(JAVA.class('java.lang.Integer'))
stdout.print_line(Null_string.eq_eq?(Null_integer).repr)  # => true

:Object_class <- JAVA.class('java.lang.Object')
:Str <- JAVA.string('foobar')
:Str_as_obj <- Str.as(Object_class)

# object is eq_eq? itself, irrespective their static types
stdout.print_line(Str.eq_eq?(Str_as_obj).repr)        # => true

# object is not eq_eq? another object
stdout.print_line(Str.eq_eq?(Object_class.new).repr)  # => false

4.59.1.6. J.null?

`null?` returns whehter the reference of `J` is null.

The result is Kink bool.

4.59.1.7. J.to_kink_str

`to_kink_str` makes a Kink str from a Java string.

The reference of `J` must be an instance of java.lang.String.

4.59.1.8. J.to_kink_bool

`to_kink_bool` makes a Kink bool from a Java boolean.

Precondition: the reference of `J` must be an instance of java.lang.Boolean.

4.59.1.9. J.to_kink_num

`to_kink_num` makes a Kink num from a Java number.

Precondtion:

The object reference of `J` must be an instance of Character, Byte, Short, Integer, Long, Float, Double, BigInteger or BigDecimal. If the reference is an instance of Float or Double, it must be finite.

Conversion details:

• If `J` is a Character, Byte, Short, Integer, Long or BigInteger, the scale of the result num is 0, and the mantissa of the result num is equal to the number itself.

• If `J` is a Float or Double, the scale of the result num is the minimum non-negative scale on which the number can be exactly expressed, and the mantissa of the result num is J * (10 ^ scale).

• If `J` is a BigDecimal, the scale of the result num is equal to the result of BigDecimal.scale(), and the mantissa is equal to the result of BigDecimal.unscaledValue().

Example:

:JAVA.require_from('kink/javahost/')

:Ans <- JAVA.byte(42).to_kink_num
stdout.print_line(Ans.repr)           # => 42
stdout.print_line(Ans.scale.repr)     # => 0
stdout.print_line(Ans.mantissa.repr)  # => 42

:Pi <- JAVA.float(3.14).to_kink_num
stdout.print_line(Pi.repr)          # => 3.1400001049041748046875
stdout.print_line(Pi.scale.repr)    # => 22
stdout.print_line(Pi.mantissa.repr) # => 31400001049041748046875

If the reference of J is a NaN or infinite numbers, J.to_kink_num raises an exception.

4.59.1.10. J.to_kink_bin

`to_kink_bin` makes a Kink bin from a Java byte array.

Precondition: the object reference of `J` must be an instance of byte[].

Contents of the byte array is copied to the bin val, thus modification to the byte array does not affect the result bin val.

Example:

:JAVA.require_from('kink/javahost/')

:Bytes <- JAVA.byte_class.array_of(JAVA.byte(1) JAVA.byte(2) JAVA.byte(3))
:Bin <- Bytes.to_kink_bin
stdout.print_line(Bin.repr) # => (bin 0x01 0x02 0x03)

Bytes.array_set(0 JAVA.byte(100))
stdout.print_line(Bin.repr) # => (bin 0x01 0x02 0x03)

4.59.1.11. J.to_kink_exception

`to_kink_exception` makes a Kink exception from a Java Throwable.

Preconditions:

• The object reference of `J` must be an instance of java.lang.Throwable.

Causes and suppressed exceptions of the throwable are chained to the result exception.

Example:

:JAVA.require_from('kink/javahost/')
:TRACE.require_from('kink/')
:LOCATION.require_from('kink/')

:Ste_class <- JAVA.class('java.lang.StackTraceElement')
:Rte <- JAVA.class('java.lang.RuntimeException').new(JAVA.string('wrong!'))
Rte.call_method('setStackTrace'
  Ste_class.array_of(
    Ste_class.new(JAVA.string('Foo') JAVA.string('foo') JAVA.string('Foo.java') JAVA.int(42))
    Ste_class.new(JAVA.string('Bar') JAVA.string('bar') JAVA.string('Bar.java') JAVA.int(24))
  )
)
:Exc <- Rte.to_kink_exception
stdout.print_line(Exc.message)
# => 'java.lang.RuntimeException: wrong!'
Exc.traces.each{(:T)
  stdout.print_line(T.repr)
}
# Output:
# (trace sym=Bar.bar(Bar.java:24) (location empty) tail?=false)
# (trace sym=Foo.foo(Foo.java:42) (location empty) tail?=false)

4.59.1.12. J.unwrap

`unwrap` returns the Kink val referred by `J` as an instance of org.kink_lang.kink.Val.

Precondition: the object reference of `J` must be an instance of org.kink_lang.kink.Val, and the runtime of the val must be same as the current runtime.

Example:

:JAVA.require_from('kink/javahost/')
:VAL.require_from('kink/')

:Str <- 'foo'
:Wrapped_str <- JAVA.wrap(Str)
stdout.print_line(Wrapped_str.repr) # => (java StrVal(foo) as org.kink_lang.kink.Val)

:Unwrapped_str <- Wrapped_str.unwrap
stdout.print_line(Unwrapped_str.repr) # => "foo"
stdout.print_line(VAL.same?(Unwrapped_str Str).repr) # => true

4.59.1.13. J.array_class

J.array_class returns a java val of the array class of the component class refferred by J.

Precondition:

• The object reference of `J` must be an instance of java.lang.Class.

• The class represented by `J` must not be void class.

• If the class represented by `J` is an array class, its dimension must be equal to or less than 254.

The result of `array_class` is a java val (arrayClass, java.lang.Class), where `arrayClass` is the array class object.

Example:

:JAVA.require_from('kink/javahost/')
:Array_class <- JAVA.int_class.array_class
stdout.print_line(Array_class.repr)  # => (java class [I as java.lang.Class)

4.59.1.14. J.array_new(Size)

`array_new` returns a java val of an array created with the `Size` and the component class `J`.

The elements are initialized as false if the component class is boolean, 0 if the component class is char, byte, short, int, long, float or double, null otherwise.

Preconditions:

• The object reference of `J` must be an instance of java.lang.Class.

• The class represented by `J` must not be void class.

• If the class represented by `J` is an array class, its dimension must be equal to or less than 254.

• `Size` must be a non-negative int num.

The result of `array_new` is a java val val (arrayObj, arrayClass), where `arrayObj` is the created array, and `arrayClass` is the array class.

Example:

:JAVA.require_from('kink/javahost/')
:Array <- JAVA.int_class.array_new(2)
stdout.print_line(Array.repr)               # => (java [I@1b6e1eff as [I)
stdout.print_line(Array.array_get(0).repr)  # => (java 0 as int)
stdout.print_line(Array.array_get(1).repr)  # => (java 0 as int)
stdout.print_line(Array.array_length.repr)  # => 2

4.59.1.15. J.array_of(E0 E1 ,,,)

`array_of` returns a java val of an array created with elements `E0`, `E1` ,,, and the component class `J`.

Preconditions:

• The object reference of `J` must be an instance of java.lang.Class.

• The class must not be void class.

• If the class represented by `J` is an array class, its dimension must be equal to or less than 254.

• `E0`, `E1`, ,,, must be a java val the references of which are typable as the `J`.

The result of `array_of` is a java val (arrayObj, arrayClass), where `arrayObj` is the created array, and `arrayClass` is the array class.

Example:

:JAVA.require_from('kink/javahost/')
:Array <- JAVA.int_class.array_of(JAVA.int(10) JAVA.int(20))
stdout.print_line(Array.repr)               # => (java [I@1b6e1eff as [I)
stdout.print_line(Array.array_get(0).repr)  # => (java 10 as int)
stdout.print_line(Array.array_get(1).repr)  # => (java 20 as int)
stdout.print_line(Array.array_length.repr)  # => 2

4.59.1.16. J.array_length

`array_length` returns the length of the Java array `J` as a Kink num.

The object reference of `J` must be an array.

4.59.1.17. J.array_get(Ind)

`array_get` returns an element of the array `J` at the index `Ind`.

Preconditions:

• The object reference of `J` must be an array.

• `Ind` must be a Kink int num, and must be in the range of [0, length-of-J).

Result:

• The result is a java val (elemObj, componentType), where `componentType` is the component type of the array.

• If `componentType` is a reference type, `elemObj` is the element of the array itself.

• If `componentType` is a primitive type, `elemObj` is an instance of the corresponding wrapper class.

4.59.1.18. J.array_set(Ind Elem)

`array_set` stores `Elem` to the array `J` at the index `Ind`.

Preconditions:

• The object reference of `J` must be an array.

• `Ind` must be a Kink int num, and must be in the range of [0, length-of-J).

• `Elem` must be a java val, the object reference of which is typable as the component type of the array.

4.59.1.19. J.get_field(Field_name)

`get_field` gets the content of the public instance field.

The field is looked up from the static type of `J`, using Class.getField(String).

Preconditions:

• The static type must have the public instance field specified by the `Field_name`.

Result:

• The result is a java val (contentObj, fieldType).

• `fieldType` is the declared type of the field.

• If the field type is a reference type, `contentObj` is the content of the field.

• If the field type is a primitive type, `contentObj` is an instance of the corresponding wrapper class.

Example:

:JAVA.require_from('kink/javahost/')
:Vm <- JAVA.wrap(new_val).get_field('vm')
stdout.print_line(Vm.repr)
# Output:
#   (java org.kink_lang.kink.Vm@1b6e1eff as org.kink_lang.kink.Vm)

4.59.1.20. J.set_field(Field_name Content)

`set_field` sets `Content` to the public instance field.

The field is looked up from the static type of `J`, using Class.getField(String).

Preconditions:

• The static type of `J` must have the public instance field specified by the `Field_name`.

• The field must not be final.

• `Content` must be a java val, the object reference of which is typable as the declared type of the field.

4.59.1.21. J.get_static(Field_name)

`get_static` gets the content of the public static field.

The field is looked up from the class object which is the object reference of the java val `J`.

Preconditions:

• The object reference of `J` must be an instance of java.lang.Class.

• The class represented by `J` must have a public static field specified by the `Field_name`.

Result:

• The result is a java val (contentObj, fieldType).

• `fieldType` is the declared type of the field.

• If the field type is a reference type, `contentObj` is the content of the field.

• If the field type is a primitive type, `contentObj` is an instance of the corresponding wrapper class.

Example:

:JAVA.require_from('kink/javahost/')
:Byte_class <- JAVA.class('java.lang.Byte')
:Byte_max <- Byte_class.get_static('MAX_VALUE')
stdout.print_line(Byte_max.repr)  # => (java 127 as byte)

4.59.1.22. J.set_static(Field_name Content)

`set_static` sets `Content` to the public static field.

The field is looked up from the class object which is the object reference of the java val `J`.

Preconditions:

• The reference of `J` must be an instance of java.lang.Class.

• The class represented by `J` must have a public static field specified by the `Field_name`.

• The field must not be final.

• `Content` must be a java val, the object reference of which is typable as the declared type of the field.

4.59.1.23. J.call_method(Method_name ...[A0 A1 ,,,] ...[$config])

`call_method` calls a public instance method.

The instance method is looked up from the static type of `J`, using Class.getMethod(Method_name, A0.static_type, A1.static_type, ,,,).

Preconditions:

• `Method_name` must be a str.

• `A0`, `A1` ,,, must be java vals.

• The static type of `J` must have the specified public instance method.

• $config, if given, must be a fun which takes a `java_call_config` val.

Result:

1) If the return type is a reference type, and the method terminates without throwing an exception, `call_method` tail-calls the success cont with a java val (resultObj, returnType), where `resultObj` is the return value of the method, and `returnType` is the declared return type of the method.

2) If the return type is char, boolean, byte, short, int, long, float or double, and the method terminates without throwing an exception, `call_method` tail-calls the success cont with a java val (resultObj, returnType), where `resultObj` is a wrapper instance of the return value, and `returnType` is the declared return type of the method.

3) If the return type is void, and the method terminates without throwing exception, `call_method` tail-calls the success cont with no arg.

4) If the method terminates throwing a Java exception, and the exception is an instance of one or more `Exception_class` types specified by invocations of C.catch, the error cont corresponding to the most specific exception type is tail-called with a java val (exceptionObj, Exception_class), where `exceptionObj` is the thrown exception, and `Exception_class` is the class which is specified by `catch`.

5) If the method terminates throwing a Java exception, and no `Exception_class` of C.catch matches the thrown exception, `call_method` raises an exception.

Example:

:JAVA.require_from('kink/javahost/')
:ArrayList_class <- JAVA.class('java.util.ArrayList')
:Object_class <- JAVA.class('java.lang.Object')
:Lst <- ArrayList_class.new

# .as(Object_class) is required,
# because the method is looked up using static types of the args
Lst.call_method('add' JAVA.string('foo').as(Object_class))
Lst.call_method('add' JAVA.int(42).as(Object_class))

stdout.print_line(Lst.repr) # => (java [foo, 42] as java.util.ArrayList)

# call ArrayList.remove(int)
:Removed <- Lst.call_method('remove' JAVA.int(0))
stdout.print_line(Removed.repr) # => (java foo as java.lang.Object)

# call ArrayList.remove(java.lang.Object)
:Success <- Lst.call_method('remove' JAVA.int(42).as(Object_class))
stdout.print_line(Success.repr) # => (java true as boolean)

By default, a Java exception results in a Kink exception.

:CONTROL.require_from('kink/')
:JAVA.require_from('kink/javahost/')
:ArrayList_class <- JAVA.class('java.util.ArrayList')
:Lst <- ArrayList_class.new

CONTROL.try(
  { Lst.call_method('get' JAVA.int(42)) }
  {(:R) raise('must not reach here') }
  {(:Exc)
    stdout.print_line(Exc.message)
  }
)
# => java exception: java.lang.IndexOutOfBoundsException: Index 42 out of bounds for length 0

The success/error conts can be configured by $config fun.

:JAVA.require_from('kink/javahost/')
:BigInteger_class <- JAVA.class('java.math.BigInteger')

:divide_10_by <- {(:Divisor)
  BigInteger_class.get_static('TEN').call_method('divide' Divisor){(:C)
    C.on_success{(:R)
      'result: {}'.format(R.repr)
    }
    C.catch(JAVA.class('java.lang.RuntimeException')){(:Exc)
      'thrown: {}'.format(Exc.repr)
    }
  }
}

stdout.print_line(divide_10_by(JAVA.big_integer(2)))
# => result: (java 5 as java.math.BigInteger)

stdout.print_line(divide_10_by(JAVA.big_integer(0)))
# => thrown: (java java.lang.ArithmeticException: BigInteger divide by zero as java.lang.RuntimeException)

If the return type of the Java method is void, the success cont is called with no args.

:JAVA.require_from('kink/javahost/')
:ArrayList_class <- JAVA.class('java.util.ArrayList')
:Object_class <- JAVA.class('java.lang.Object')

:Lst <- ArrayList_class.new
Lst.call_method('add' JAVA.string('foo').as(Object_class))
Lst.call_method('add' JAVA.string('bar').as(Object_class))

Lst.call_method('clear'){(:C)
  C.on_success{()
    stdout.print_line('cleared!')
  }
}
# => cleared!

4.59.1.24. J.call_static(Method_name ...[A0 A1 ,,,] ...[$config])

`call_static` calls a public static method.

The method is looked up from the class object which is the reference of the java val `J`, using Class.getMethod(Method_name, A0.static_type, A1.static_type, ,,,).

Preconditions:

• The object reference of `J` must be an instance of java.lang.Class.

• `A0`, `A1`,,, must be java vals.

• The class which is represented by the object reference of `J` must have the specified public static method.

• $config, if given, must be a fun which takes a `java_call_config` val.

See J.call_method for description of the result.

4.59.1.25. J.new(...[A0 A1 ,,,] ...[$config])

`new` makes a new Java instance calling a public constructor.

The constructor is looked up from the class object which is the reference of the java val `J`, using Class.getConstructor(A0.static_type, A1.static_type, ,,,).

Preconditions:

• `A0`, `A1`,,, must be java vals.

• The static type of `J` must have the specified public constructor.

• $config, if given, must be a fun which takes a `java_call_config` val.

Result:

1) If a new instance is created, `new` tail-calls the success cont with a java val (resultObj, returnType), where `resultObj` is the new instance, and `returnType` is the class object which is the reference of `J`.

2) If the constructor terminates throwing a Java exception, and the exception is an instance of one or more `Exception_class` types specified by invocations of C.catch, the error cont corresponding to the most specific exception type is tail-called with a java val (exceptionObj, Exception_class), where `exceptionObj` is the thrown exception, and `Exception_class` is the class which is specified by `catch`.

3) If the constructor terminates throwing a Java exception, and no `Exception_class` of C.catch matches the thrown exception, `new` raises an exception.

Example:

:CONTROL.require_from('kink/')
:JAVA.require_from('kink/javahost/')
:BigDecimal_class <- JAVA.class('java.math.BigDecimal')

:make_big_decimal <- {(:Str)
  BigDecimal_class.new(JAVA.string(Str))
}

stdout.print_line(make_big_decimal('12.3').repr)  # => (java 12.3 as java.math.BigDecimal)

CONTROL.try(
  { make_big_decimal('xxx') }
  {(:R) raise('must not reach here') }
  {(:Exc)
    stdout.print_line(Exc.message)
  }
)
# => java exception: java.lang.NumberFormatException: Character x is neither a decimal digit number, decimal point, nor "e" notation exponential mark.

4.59.2. type java_call_config

Conf val type of call methods: J.call_method, J.call_static, and J.new.

4.59.2.1. C.on_success($success_cont)

`on_success` specifies $success_cont as the success cont of the call method.

Precondition:

• $success_cont must be a fun.

If `on_success` is not called, the default success cont is determined as follows:

• For `new`, VAL.identity is used as the default success cont.

• For `call_method` and `call_static` for methods of which the return type is void, { nada } is used as the default success cont.

• For `call_method` and `call_static` for methods of which the return type is not void, VAL.identity is used as the default success cont.

4.59.2.2. C.catch(Exception_class $cont)

`catch` specifies $cont as the error cont which is tail-called when an exception of the specified type is thrown.

Preconditions:

• `Exception_class` must be a java val of a class object of a subclass of java.lang.Throwable.

• `Exception_class` must not be a subtype of the exception type specified by any previous invocation of `catch`.

• $cont must be a fun.

Example of multiple invocations of `catch`. Note that the static type of the java val of the caught exception is RuntimeException, which is specified to invocation of `catch`.

:JAVA.require_from('kink/javahost/')
:Charset_class <- JAVA.class('java.nio.charset.Charset')

Charset_class.call_static('forName' JAVA.string('<malformed name>')){(:C)
  C.catch(JAVA.class('java.lang.RuntimeException')){(:Rte)
    stdout.print_line('caught rte: {}'.format(Rte.repr))
  }
  C.catch(JAVA.class('java.lang.Exception')){(:Exc)
    stdout.print_line('caught exception: {}'.format(Exc.repr))
  }
}
# => caught rte: (java java.nio.charset.IllegalCharsetNameException: <malformed name> as java.lang.RuntimeException)

RuntimeException cannot be specified after Exception, because RuntimeException is a subtype of Exception. Example:

:JAVA.require_from('kink/javahost/')
:Charset_class <- JAVA.class('java.nio.charset.Charset')

Charset_class.call_static('forName' JAVA.string('<malformed name>')){(:C)
  C.catch(JAVA.class('java.lang.Exception')){ }
  C.catch(JAVA.class('java.lang.RuntimeException')){ }
}
# Output:
#   ...
#   C.catch(Exception_class $cont): catch block of java.lang.RuntimeException is not accessible, because it is preceded by java.lang.Exception

4.59.3. JAVA.is?(Val)

`is?` returns whether `Val` is a java val.

4.59.4. JAVA.string(Str)

`string` makes a Java string.

Preecondition:

• `Str` must be a Kink str.

The result is a java val (string, java.lang.String), where `string` is a java.lang.String instance which is copied from `Str`.

4.59.5. JAVA.bytes(Bin)

`bytes` makes a Java byte array from `Bin`.

Precondtion:

• `Bin` must be a bin.

The result is a java val (byteArray, byte[]), where `byteArray` is an array of bytes whose elements are copied from `Bin`.

4.59.6. JAVA.boolean(Bool)

`boolean` returns a Java boolean corresponding to `Bool`.

Precondition:

• `Bool` must be a Kink bool.

Result:

• If `Bool` is true, `boolean` returns a java val (Boolean.TRUE, boolean).

• If `Bool` is false, `boolean` returns a java val (Boolean.FALSE, boolean).

4.59.7. JAVA.char(Num)

`char` returns a Java char corresponding to `Num`.

Precondition:

• `Num` must be a Kink int num.

The result is a java val (ch, char), where `ch` is an instance of java.lang.Character which is copied from the least significant 16 bits.

4.59.8. JAVA.byte(Num)

`byte` returns a Java byte corresponding to Num.

Precondition:

• `Num` must be a Kink int num.

The result is a java val (bt, byte), where `bt` is an instance of java.lang.Byte which is copied from the least significant 8 bits.

4.59.9. JAVA.short(Num)

`short` returns a Java short corresponding to `Num`.

Precondition:

• `Num` must be a Kink int num.

The result is a java val (sh, short), where `sh` is an instance of java.lang.Short which is copied from the least significant 16 bits.

4.59.10. JAVA.int(Num)

`int` returns a Java int corresponding to `Num`.

Precondition:

• `Num` must be a Kink int num.

The result is a java val (n, int), where `n` is an instance of java.lang.Integer which is copied from the least significant 32 bits.

4.59.11. JAVA.long(Num)

`long` returns a Java long corresponding to `Num`.

Precondition:

• `Num` must be a Kink int num.

The result is a java val (ln, long), where `ln` is an instance of java.lang.Long which is copied from the least significant 64 bits.

4.59.12. JAVA.float(Num)

`float` returns a Java float numerically close to `Num`.

Precondition:

• `Num` must be a Kink num.

The result is a java val (fl, float), where `fl` is an instance of java.lang.Float, and `fl` is the result of conversion by BigDecimal.floatValue().

4.59.13. JAVA.double(Num)

`double` returns a Java double numerically close to `Num`.

Precondition:

• `Num` must be a Kink num.

The result is a java val (dbl, double), where `dbl` is an instance of java.lang.Double, and `dbl` is the result of conversion by BigDecimal.doubleValue().

4.59.14. JAVA.big_integer(Num)

`big_integer` returns a Java BigInteger corresponding to `Num`.

Precondition:

• `Num` must be a Kink num.

The result is a java val (bi, java.math.BigInteger), where `bi` is an instance of java.math.BigInteger, and the number represented by bi is equal to `Num`.

4.59.15. JAVA.big_decimal(Num)

`big_decimal` returns a Java BigDecimal corresponding to `Num`.

Precondition:

• `Num` must be a kink num.

The result is a java val (bd, java.math.BigDecimal), where `bd` is an instance of java.math.BigDecimal, the scale of `bd` is equal to the scale of `Num`, and the unscaledValue of `bd` is equal to the mantissa of `Num`.

4.59.16. JAVA.wrap(Val)

`wrap` make a java val which represents `Val`.

`wrap` return a java val (kv, org.kink_lang.kink.Val), where `kv` is an instance of org.kink_lang.kink.Val which represents the Val.

`Val` can be extracted from the result java val using J.unwrap method.

Example:

:JAVA.require_from('kink/javahost/')
:Str <- 'foo'
:Wrapper <- JAVA.wrap(Str)
stdout.print_line(Wrapper.repr)  # => (java StrVal(foo) as org.kink_lang.kink.Val)
stdout.print_line(Wrapper.unwrap.repr)  # => "foo"

4.59.17. JAVA.boolean_class

`boolean_class` returns a java val which represents boolean.class.

The result is a java val (boolean.class, java.lang.Class).

4.59.18. JAVA.char_class

`char_class` returns a java val which represents char.class.

The result is a java val (char.class, java.lang.Class).

4.59.19. JAVA.byte_class

`byte_class` returns a java val which represents byte.class.

The result is a java val (byte.class, java.lang.Class).

4.59.20. JAVA.short_class

`short_class` returns a java val which represents short.class.

The result is a java val (short.class, java.lang.Class).

4.59.21. JAVA.int_class

`int_class` returns a java val which represents int.class.

The result is a java val (int.class, java.lang.Class).

4.59.22. JAVA.long_class

`long_class` returns a java val which represents long.class.

The result is a java val (long.class, java.lang.Class).

4.59.23. JAVA.float_class

`float_class` returns a java val which represents float.class.

The result is a java val (float.class, java.lang.Class).

4.59.24. JAVA.double_class

`double_class` returns a java val which represents double.class.

The result is a java val (double.class, java.lang.Class).

4.59.25. JAVA.void_class

`void_class` returns a java val which represents void.class.

The result is a java val (void.class, java.lang.Class).

4.59.26. JAVA.null

`null` returns a java val which represents null reference.

The result is a java val (null, java.lang.Object).

4.59.27. JAVA.true

`true` returns a java val which represents Java true.

The result is a java val (true, boolean).

4.59.28. JAVA.false

`false` returns a java val which represents Java false.

The result is a java val (false, boolean).

4.59.29. JAVA.class(Class_name)

`class` tries to find a class, then returns the class.

Precondition:

• `Class_name` must be a str, which represents a binary name of the class.

`class` loads the specified class from the class loader which the Kink runtime belongs to.

If the class is found, `class` returns the `Klass`, where Klass is a java val (the loaded class object, java.lang.Class).

If the class is not found, `class` raises an exception.