java.lang.* and java.util.* types implemented by the Picodroid JVM. See Java API overview for the full API index.
java.lang.String
The JVM provides built-in support for java.lang.String. All methods work on ASCII strings; multi-byte UTF-8 sequences are passed through unchanged but byte-indexed (not character-indexed).
String.format supports the conversions %s %d %x %X %o %c %b %f %e %g %n %% with the flags
-0+(space),#, plus width and precision (e.g. %-8s, %08.2f).
StringBuilder interaction:+ string concatenation compiles to a compiler-generated StringBuilder that shares the JVM’s single internal buffer. If you build a StringBuilder manually and then log "prefix=" + sb.toString(), the compiler’s StringBuilder will clear the buffer before sb.toString() is evaluated. Capture the result first:
Stringresult=sb.toString(); // snapshot the buffer
Log.i(TAG, "prefix="+ result); // safe to concatenate now
java.lang.StringBuilder
StringBuildersb=newStringBuilder(); // empty
StringBuildersb=newStringBuilder("prefix="); // with initial content
sb.append("text"); // append String
sb.append(42); // append int
sb.append(3.14f); // append float (formats as "3.14")
sb.append(2.71828); // append double
sb.append(100L); // append long
sb.append(true); // append "true" or "false"
sb.append('x'); // append char
intlen=sb.length(); // current content length
charch= (char) sb.charAt(2); // byte at position 2
Strings=sb.toString(); // intern result as a String
Single shared buffer: all StringBuilder instances in the JVM share one underlying buffer. Creating a new StringBuilder (including the compiler-generated one for + concatenation) clears that buffer. Build one StringBuilder at a time and call toString() before starting another.
java.lang.Math
Standard math functions. All methods are static. Math.PI and Math.E are compile-time constants inlined by javac.
// Constants (inlined by the compiler — no runtime cost)
doublepi=Math.PI; // 3.141592653589793
doublee=Math.E; // 2.718281828459045
// abs — int, long, float, double
intai=Math.abs(-7); // 7
longal=Math.abs(-9000L); // 9000
floataf=Math.abs(-3.14f); // 3.14
doublead=Math.abs(-1.0); // 1.0
// min / max — int, long, float, double
intlo=Math.min(4, 9); // 4
doublehi=Math.max(1.5, 2.5); // 2.5
// Rounding
doublefl=Math.floor(2.9); // 2.0
doublece=Math.ceil(2.1); // 3.0
intri=Math.round(2.6f); // 3 (float → int)
longrl=Math.round(2.5); // 3 (double → long)
// Powers / roots
doublesq=Math.sqrt(2.0); // ≈ 1.4142135
doublepw=Math.pow(2.0, 10.0); // 1024.0
// Trigonometry (arguments in radians)
doubles=Math.sin(Math.PI/2.0); // ≈ 1.0
doublec=Math.cos(0.0); // 1.0
doublet=Math.tan(0.0); // 0.0
doublea2=Math.atan2(1.0, 1.0); // ≈ PI/4
// Angle conversion
doublerad=Math.toRadians(90.0); // ≈ PI/2
doubledeg=Math.toDegrees(Math.PI); // 180.0
// Logarithms / exponential
doubleln=Math.log(Math.E); // ≈ 1.0
doublelg=Math.log10(100.0); // ≈ 2.0
doubleex=Math.exp(1.0); // ≈ 2.71828
java.util.ArrayList
Dynamic list backed by a per-instance heap buffer.
importjava.util.ArrayList;
// Raw type (stores any Object — String, custom objects, null)
// Generic type with autoboxing (Integer, Boolean, Long, Float, Double)
ArrayList<Integer> nums=newArrayList<Integer>();
nums.add(10); // autoboxes int → Integer
nums.add(20);
intn=nums.get(0); // auto-unboxes Integer → int (10)
booleanhas=nums.contains(20); // true — value equality for wrappers
Autoboxing:ArrayList<Integer> works as expected — add(42) and contains(42) both box via Integer.valueOf. For raw ArrayList, store and retrieve Object references (String, custom class instances); do not store bare primitives without explicit boxing (Integer.valueOf(42), etc.).
java.util.HashMap and java.util.HashSet
Hash-table-backed associative containers. Keys are compared by equals() / hashCode(); autoboxed primitives (Integer, Long, Boolean, String) all work as keys.
// Iterate keys / values / entries (keySet(), values() and entrySet() are Iterable)
for (Objectk:map.keySet()) { Log.i("TAG", (String) k); }
for (Objectval:map.values()) { Log.i("TAG", String.valueOf((Integer) val)); }
for (Map.Entry<String, Integer> e:map.entrySet()) { Log.i("TAG", e.getKey()+"="+e.getValue()); }
HashSetset=newHashSet();
set.add("a");
set.add("b");
booleaninSet=set.contains("a"); // true
java.util.Iterator and the enhanced for loop
ArrayList, HashMap (via keySet(), values(), entrySet()), HashSet, and any class of your own that implements Iterable work with the enhanced for loop and an explicit Iterator. LinkedHashMap/LinkedHashSet are accepted as aliases of HashMap/HashSet (no insertion order — see the compatibility matrix).
importjava.util.ArrayList;
importjava.util.Iterator;
ArrayListitems=newArrayList();
items.add("a"); items.add("b"); items.add("c");
// Enhanced for-each
for (Objecto: items) {
Log.i("TAG", (String) o);
}
// Explicit iterator
Iteratorit=items.iterator();
while (it.hasNext()) {
Log.i("TAG", (String) it.next());
}
java.util.Arrays and java.util.Collections
Stable mergesort and a small set of list utilities. Mirrors the most-used subset of the Java standard library.
importjava.lang.Comparable;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
// Object[] sort — element type must implement Comparable
// Collections — operate on java.util.List (ArrayList implements it)
ArrayList<Integer> nums=newArrayList<Integer>();
nums.add(3); nums.add(1); nums.add(2);
Collections.sort(nums); // [1, 2, 3]
Collections.reverse(nums); // [3, 2, 1]
Method
Description
Arrays.sort(Object[] a)
In-place stable mergesort. Elements must implement Comparable.
Arrays.sort(int[] a)
In-place sort of a primitive array (long/double/float/short/byte/char overloads too).
Arrays.fill(a, value)
Fill every element with value (primitive overloads).
Arrays.copyOf(a, newLength)
Copy, truncating or zero-padding to newLength (primitive overloads).
Arrays.toString(Object[] a)
"[a, b, c]" rendering using each element’s toString.
Collections.sort(List)
Stable mergesort over a List. Elements must implement Comparable.
Collections.reverse(List)
Reverse the list in place.
java.lang.Comparable
publicclassScoreimplementsComparable<Score> {
intvalue;
publicintcompareTo(Scoreother) {
returnthis.value-other.value;
}
}
Used by Arrays.sort and Collections.sort. Boxed numerics (Integer, Long, Float, Double) and String already implement it.
Interface-typed collections
List<E>, Set<E>, Collection<E>, Map<K,V> and Iterable<E> all work as declared types, parameter types and return types — Map<String, String> m = new HashMap<>(); compiles and runs, as do interface-typed fields, instanceof, casts and the enhanced for loop:
These interfaces are built into the JVM rather than shipped as SDK source: your app compiles against the JDK’s own java.util declarations, and at run time the call dispatches on the receiver’s actual class (ArrayList, HashMap, HashSet, or a class of your own). ArrayList is the only concrete List in v1.
The catch: because the compiler sees the JDK’s full interfaces, it will also accept members picodroid does not implement (map.forEach, list.removeIf, Map.putAll, new TreeMap<>()), which fail at run time instead of at build time. Stick to the members listed in the compatibility matrix.
java.lang.Class
Class literals (MyType.class) and reflection-lite. Class<?> is the only reflective surface — there’s no Field or Method API in v1.
Class<?> c=String.class;
Stringname=c.getName(); // "java.lang.String"
booleansame= (s.getClass()==String.class); // true — Class instances are interned
// Each evaluation of `T.class` returns the same Class instance
Object.getClass() returns the runtime Class<?> of any reference. Useful for type-safe equality (.getClass() == Foo.class) and for log dispatch keyed by class identity.
java.lang.AutoCloseable and try-with-resources
publicinterfaceAutoCloseable {
voidclose()throwsException;
}
Any class that implements AutoCloseable works in try-with-resources — the compiler calls close() on exit (normal or exceptional). The picodroid.pio.* peripheral handles all implement it, so the idiomatic pattern is:
} // led.close() runs here — releases the pin back to the PeripheralManager.
Multiple resources in one try close in reverse-declaration order. See examples/trywithresourcesdemo/ for a worked example.
Enums
Java enum declarations are supported. Each enum constant is a singleton; values(), name(), ordinal(), and switch (myEnum) all work.
publicenum Direction { NORTH, EAST, SOUTH, WEST }
Directiond=Direction.NORTH;
Stringname=d.name(); // "NORTH"
intord=d.ordinal(); // 0
for (Directiondir:Direction.values()) {
Log.i("TAG", dir.name());
}
switch (d) {
case NORTH:Log.i("TAG", "up"); break;
case SOUTH:Log.i("TAG", "down"); break;
default:Log.i("TAG", "side"); break;
}
Boxed primitives (wrapper classes)
Integer, Long, Float, Double, Boolean, and Character are available as object wrappers.
Each supports valueOf(primitive), the matching unboxing accessor, and toString():
You rarely call these directly: ArrayList<Integer> and HashMap keys/values autobox through
valueOf and auto-unbox through the *Value() accessors.
Exceptions
java.lang.Throwable, Exception, and RuntimeException are supported, each with the standard
no-arg and (String message) constructors. Standard subclasses such as IllegalArgumentException
and IllegalStateException are also throwable with a message. Define your own by extending
Exception (or RuntimeException), then throw / catch as usual — catch matches subclasses
of the declared type:
The message passed to a constructor is captured by the runtime and shown when a throw goes
uncaught. v1 caveats: there is no getMessage() / getCause() accessor and no stack-trace API
yet — the message is for runtime diagnostics, not programmatic inspection. See
examples/exceptiondemo/.
java.util.Random
A seedable pseudo-random generator. A fixed seed gives a reproducible stream.
importjava.util.Random;
Randomr=newRandom(); // or new Random(42L) for a fixed seed
System.currentTimeMillis() returns wall-clock milliseconds (see
System & concurrency for the full timing surface, including
SystemClock). java.lang.Runnable is the standard void run() interface — used by
Thread, Executors, and (historically) view callbacks: