programming:java:java
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programming:java:java [2023/03/05 20:29] – [install alternative JDK using SDKman] skipidar | programming:java:java [2023/11/02 14:33] (current) – [Records] skipidar | ||
---|---|---|---|
Line 28: | Line 28: | ||
+ | |||
+ | ===== Available JDKs ===== | ||
+ | https:// | ||
===== install alternative JDK using SDKman ===== | ===== install alternative JDK using SDKman ===== | ||
Line 245: | Line 248: | ||
< | < | ||
# java open GraalVM from liberica " | # java open GraalVM from liberica " | ||
- | sdk install java 22.2.r17-nik < /dev/null | + | sdk install java 22.3.r17-nik < /dev/null |
# java openjdk | # java openjdk | ||
Line 256: | Line 259: | ||
< | < | ||
sdk use java 19.0.2-open | sdk use java 19.0.2-open | ||
- | sdk use java 22.2.r17-nik | + | sdk use java 22.3.r17-nik |
</ | </ | ||
Line 1062: | Line 1065: | ||
+ | |||
+ | ===== JUnit 5 ===== | ||
+ | |||
+ | <sxh groovy> | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Hamcrest ===== | ||
+ | |||
+ | Has matches useful for unit tests. | ||
+ | |||
+ | Set the groovy dependencies | ||
+ | <sxh groovy> | ||
+ | dependencies { | ||
+ | testImplementation ' | ||
+ | testImplementation ' | ||
+ | |||
+ | // import here | ||
+ | testImplementation ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Now you can import " | ||
+ | |||
+ | <sxh java> | ||
+ | import static org.hamcrest.MatcherAssert.assertThat; | ||
+ | import static org.hamcrest.Matchers.*; | ||
+ | |||
+ | import org.junit.jupiter.api.Test; | ||
+ | |||
+ | public class MyTest { | ||
+ | |||
+ | @Test | ||
+ | void testExample() { | ||
+ | String value = " | ||
+ | | ||
+ | assertThat(value, | ||
+ | assertThat(value, | ||
+ | assertThat(value, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Text processing ===== | ||
+ | |||
+ | ==== Regular Expressions (java.util.regex) ==== | ||
+ | |||
+ | When using Regex - make sure you dont introduce too greedy patterns | ||
+ | |||
+ | https:// | ||
+ | < | ||
+ | Boundary matchers | ||
+ | ^ The beginning of a line | ||
+ | $ The end of a line | ||
+ | \b A word boundary | ||
+ | \B A non-word boundary | ||
+ | \A The beginning of the input | ||
+ | \G The end of the previous match | ||
+ | \Z The end of the input but for the final terminator, if any | ||
+ | \z The end of the input | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Tokenize strings ==== | ||
+ | |||
+ | Break into single words. | ||
+ | |||
+ | <sxh java> | ||
+ | |||
+ | String[] result = "this is a test" | ||
+ | for (int x=0; x< | ||
+ | System.out.println(result[x]); | ||
+ | |||
+ | /* this | ||
+ | is | ||
+ | a | ||
+ | test | ||
+ | */ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | <sxh java> | ||
+ | @Test | ||
+ | void patternMatch() { | ||
+ | mymatch(" | ||
+ | mymatch(" | ||
+ | } | ||
+ | |||
+ | public static long mymatch(String regex, String text) { | ||
+ | Pattern pattern = Pattern.compile(regex); | ||
+ | Matcher matcher = pattern.matcher(text); | ||
+ | |||
+ | List< | ||
+ | .results() | ||
+ | .toList(); | ||
+ | |||
+ | System.out.printf(" | ||
+ | return res.size(); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== ReplaceAll | ||
+ | |||
+ | <sxh java> | ||
+ | @Test | ||
+ | void stringReplaceAll() { | ||
+ | String s = "bob and alice like to text each other"; | ||
+ | String john = s.replaceAll(" | ||
+ | System.out.println(john); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Records ===== | ||
+ | First: Java 14 | ||
+ | |||
+ | |||
+ | |||
+ | <sxh java> | ||
+ | // some interface | ||
+ | interface MyBarkInterface{ | ||
+ | default void bark(){ | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | record MyPoint(int x, int y) implements MyBarkInterface{}; | ||
+ | |||
+ | // Cant inherit from Record - its final | ||
+ | //record MyPoint2(int x, int y) extends MyPoint{}; | ||
+ | |||
+ | |||
+ | @Test | ||
+ | void recordTest1() { | ||
+ | MyPoint p = new MyPoint(1, | ||
+ | |||
+ | System.out.println(p); | ||
+ | System.out.println(p.x()); | ||
+ | System.out.println(p.x()); | ||
+ | | ||
+ | p.bark(); // can access hiearchy | ||
+ | } | ||
+ | |||
+ | </ |
programming/java/java.1678048140.txt.gz · Last modified: by skipidar