سؤال

https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby indicates that the Java null object is coerced to Ruby's nil, but it doesn't mention if coercion occurs in the other direction (e.g. when an argument is passed to a Java method). Does it? If not, how can you create null from JRuby?

I did note that nil.to_java returns nil.

هل كانت مفيدة؟

المحلول

To clarify, the answer to your question is yes.

To see this for yourself, just try passing nil to any Java method and see what happens, it's pretty easy to do:

> java.lang.System.out.println(nil)
null
 => nil

Or

> java.lang.String.new(nil)
Java::JavaLang::NullPointerException:

نصائح أخرى

Try

  JavaUtil.primitive_to_java(nil)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top