/tutorial/cecil/JVM/Example4.java

http://github.com/tybor/Liberty · Java · 51 lines · 26 code · 9 blank · 16 comment · 0 complexity · 2ace339b4cebf9f34ced76da3613f8a0 MD5 · raw file

  1. import root4.*;
  2. public class Example4 {
  3. public static void main(String[] args) {
  4. /* To run this example, procede like this :
  5. compile_to_jvm root4
  6. /bin/rm -f root4.class
  7. javac Example4.java
  8. java Example4
  9. */
  10. root4 root;
  11. string eiffel_string;
  12. /* To initialize the Eiffel runtime :
  13. */
  14. _any._initialize_eiffel_runtime(args);
  15. /* Creation of the root object :
  16. */
  17. root = new root4();
  18. eiffel_string = ( string )( root.get_string() );
  19. root.put_string( eiffel_string );
  20. // print the capacity and count of the string.
  21. System.out.println( "eiffel_string capacity: " +
  22. eiffel_string.capacity +
  23. ", count: " +
  24. eiffel_string.count );
  25. // Build a Java String from the Eiffel string, and print it.
  26. String eiffel_string_in_java = new String( eiffel_string.storage,
  27. 0,
  28. eiffel_string.count );
  29. System.out.println( "Java printing the converted eiffel: '" +
  30. eiffel_string_in_java + "'" );
  31. // Build and Eiffel string from a Java String, and print it from Eiffel.
  32. // Note that the byte buffer does not have to be null terminated.
  33. String js = "Hello from Java";
  34. string new_eiffel_string = new string();
  35. new_eiffel_string.storage = js.getBytes();
  36. new_eiffel_string.capacity = new_eiffel_string.storage.length;
  37. new_eiffel_string.count = new_eiffel_string.storage.length;
  38. root.put_string( new_eiffel_string );
  39. }
  40. }