|
| 1 | +# Java |
| 2 | + |
| 3 | +Java is a high-level, general-purpose, memory-safe, object-oriented programming language. |
| 4 | + |
| 5 | +In LiveCodes, Java runs in the browser using [DoppioJVM](https://github.com/plasma-umass/doppio). |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Demo: |
| 10 | + |
| 11 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 12 | + |
| 13 | +export const javaConfig = { |
| 14 | + activeEditor: 'script', |
| 15 | + script: { |
| 16 | + language: 'java', |
| 17 | + content: `public class BinarySearchSnippet { |
| 18 | + /** |
| 19 | + * Search an item with binarySearch algorithm. |
| 20 | + * |
| 21 | + * @param arr sorted array to search |
| 22 | + * @param item an item to search |
| 23 | + * @return if item is found, return the index position of the array item otherwise return -1 |
| 24 | + */ |
| 25 | +
|
| 26 | + public static int binarySearch(int[] arr, int left, int right, int item) { |
| 27 | + if (right >= left) { |
| 28 | + int mid = left + (right - left) / 2; |
| 29 | + if (arr[mid] == item) { |
| 30 | + return mid; |
| 31 | + } |
| 32 | +
|
| 33 | + if (arr[mid] > item) { |
| 34 | + return binarySearch(arr, left, mid - 1, item); |
| 35 | + } |
| 36 | +
|
| 37 | + return binarySearch(arr, mid + 1, right, item); |
| 38 | + } |
| 39 | + return -1; |
| 40 | + } |
| 41 | +
|
| 42 | + public static void main(String[] args) { |
| 43 | + int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15}; |
| 44 | + int itemToSearch = 7; |
| 45 | +
|
| 46 | + int result = binarySearch(sortedArray, 0, sortedArray.length - 1, itemToSearch); |
| 47 | +
|
| 48 | + if (result == -1) { |
| 49 | + System.out.println("Result: Item not found in the array."); |
| 50 | + } else { |
| 51 | + System.out.println("Result: Item found at index -> " + result); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +`, |
| 56 | + }, |
| 57 | + mode: 'simple', |
| 58 | + editor: 'auto', |
| 59 | + tools: { |
| 60 | + status: 'full', |
| 61 | +}, |
| 62 | +}; |
| 63 | + |
| 64 | +<LiveCodes config={javaConfig}></LiveCodes> |
| 65 | + |
| 66 | +### Communication with JavaScript |
| 67 | + |
| 68 | +The Java code runs in the context of the [result page](../features/result.mdx). |
| 69 | +A few helper properties and methods are available in the browser global `livecodes.java` object: |
| 70 | + |
| 71 | +- `livecodes.java.input`: the initial standard input that is passed to the Java code. |
| 72 | +- `livecodes.java.loaded`: A promise that resolves when the Java environment is loaded. Any other helpers should be used after this promise resolves. |
| 73 | +- `livecodes.java.output`: the standard output. |
| 74 | +- `livecodes.java.error`: the standard error. |
| 75 | +- `livecodes.java.exitCode`: the exit code. |
| 76 | +- `livecodes.java.run`: a function that runs the Java code with new input. This function takes a string as input and returns a promise that resolves when the Java code is done running. The promise resolves with an object containing the `input`, `output`, `error`, and `exitCode` properties. |
| 77 | + |
| 78 | +Example: |
| 79 | + |
| 80 | +<LiveCodes template="java" params={{ activeEditor: 'markup' }} height="80vh"></LiveCodes> |
| 81 | + |
| 82 | +## Language Info |
| 83 | + |
| 84 | +### Name |
| 85 | + |
| 86 | +`java` |
| 87 | + |
| 88 | +### Extension |
| 89 | + |
| 90 | +`.java` |
| 91 | + |
| 92 | +### Editor |
| 93 | + |
| 94 | +`script` |
| 95 | + |
| 96 | +## Compiler |
| 97 | + |
| 98 | +[DoppioJVM](https://github.com/plasma-umass/doppio) |
| 99 | + |
| 100 | +### Version |
| 101 | + |
| 102 | +`DoppioJVM`: v0.5.0, which runs Java 8 JDK. |
| 103 | + |
| 104 | +## Code Formatting |
| 105 | + |
| 106 | +Using [Prettier](https://prettier.io) with the [Prettier Java plugin](https://github.com/jhipster/prettier-java). |
| 107 | + |
| 108 | +## Live Reload |
| 109 | + |
| 110 | +By default, new code changes are sent to the result page for re-evaluation without a full page reload, to avoid the need to reload the Java environment. |
| 111 | + |
| 112 | +This behavior can be disabled by adding the code comment `// __livecodes_reload__` to the code, which will force a full page reload. |
| 113 | +This comment can be added in the [`hiddenContent` property of the editor](../configuration/configuration-object.mdx#markup) for embedded playgrounds. |
| 114 | + |
| 115 | +## Starter Template |
| 116 | + |
| 117 | +https://livecodes.io/?template=java |
| 118 | + |
| 119 | +## Links |
| 120 | + |
| 121 | +- [Java](https://www.java.com/) |
| 122 | +- [DoppioJVM](https://github.com/plasma-umass/doppio) |
0 commit comments