Ben Chuanlong Du's Blog

It is never too late to learn.

Calling Rust from Java

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

https://github.com/drrb/java-rust-example

https://users.rust-lang.org/t/ffi-java-call-a-rust-dynamic-library/19924

https://docs.rs/jni/0.14.0/jni/

https://stackoverflow.com/questions/30258427/calling-rust-from-java

https://github.com/java-native-access/jna …

Java Native Access

Java Native Interface

  1. You can call native code (typically C, C++ or Fortran) in Java using the Java Native Interface (JNI). For the code implemented in native code, you must use keyword "native" to tell the compiler that it is implemented outside Java. Also, you should surround the Java code which load the compile native code in static{}

Implement Singleton in Java

An implementation of the singleton pattern must:

  • ensure that only one instance of the singleton class ever exists;
  • and provide global access to that instance.

Typically, this is done by:

  • declaring all constructors of the class to be private;
  • and providing a static method that returns a reference to the instance.

The instance is usually stored as a private static variable; the instance is created when the variable is initialized, at some point before the static method is first called. The following is a sample implementation written in Java.

String in Java

Comments

  1. String is a immutable class in Java. Extensive operations on strings (e.g., + in a big loop) is usually very slow before Java 7 (the + operator is optimized by the compiler automatically starting from Java 7). To avoid this problem (in older versions of Java), you can use the StringBuilder