Java 25 Cheat Sheet
Program and output
void main () { // compact, Java 25
IO . println ( "Hello" );
var name = IO . readln ( "Name? " );
}
public class Program { // classic
public static void main ( String [] args ) {
System . out . println ( "Hello" );
}
}
java Program.java javac Program.java java Program jshell
Types
int i = 42 ; long l = 42L ; double d = 3 . 14 ; float f = 3 . 14f ;
boolean b = true ; char c = 'A' ; String s = "text" ; var x = 42 ;
final double VAT = 0 . 19 ;
Strings
s . length () s . isEmpty () s . isBlank () s . strip () s . repeat ( 3 )
s . toUpperCase () s . substring ( 2 , 5 ) s . charAt ( 0 ) s . indexOf ( "a" )
s . contains ( "a" ) s . startsWith ( "a" ) s . replace ( "a" , "b" ) s . split ( "," )
s . equals ( t ) s . equalsIgnoreCase ( t ) s . compareTo ( t )
"%s is %d" . formatted ( name , age ) String . join ( ", " , list )
var block = """
multiline
""" ;
Control flow
if ( a > b ) { … } else if ( a == b ) { … } else { … }
var t = cond ? "yes" : "no" ;
var text = switch ( value ) {
case 1 , 2 -> "small" ;
case Integer i when i > 100 -> "big" ;
case String str -> "text: " + str ;
case null -> "nothing" ;
default -> { yield "other" ; }
};
if ( o instanceof String str && str . length () > 3 ) { … }
Loops
for ( var e : list ) { … }
for ( var i = 0 ; i < n ; i ++ ) { … }
while ( cond ) { … }
do { … } while ( cond );
break ; continue ;
Collections
var list = new ArrayList < String > ();
list . add ( "a" ); list . get ( 0 ); list . remove ( "a" ); list . size (); list . contains ( "a" );
var fixed = List . of ( "a" , "b" );
var set = new HashSet < String > ();
var map = new HashMap < String , Integer > ();
map . put ( "a" , 1 ); map . get ( "a" ); map . getOrDefault ( "b" , 0 );
map . merge ( "a" , 1 , Integer :: sum );
map . computeIfAbsent ( "k" , k -> new ArrayList <> ());
map . forEach (( k , v ) -> IO . println ( k + "=" + v ));
int [] arr = { 1 , 2 , 3 }; arr . length ; Arrays . sort ( arr );
Methods
int add ( int a , int b ) { return a + b ; }
void doIt () { … }
int sum ( int ... numbers ) { … }
static int help ( int x ) { … }
Types you define
record Person ( String name , int age ) {
Person { if ( age < 0 ) throw new IllegalArgumentException (); }
String initial () { return name . substring ( 0 , 1 ); }
}
public class Account {
private long balance ;
public Account ( long start ) { this . balance = start ; }
public long balance () { return balance ; }
}
interface Payable {
void pay ( long cents );
default String info () { return "payment" ; }
}
sealed interface Shape permits Circle , Square {}
record Circle ( double r ) implements Shape {}
record Square ( double a ) implements Shape {}
enum Status { OPEN , DONE }
Exceptions
try {
risky ();
} catch ( IOException | NumberFormatException e ) {
IO . println ( e . getMessage ());
} finally {
cleanUp ();
}
try ( var reader = Files . newBufferedReader ( path )) { … }
throw new IllegalArgumentException ( "message" );
void m () throws IOException { … }
Lambdas and streams
Predicate < String > p = s -> s . length () > 3 ;
Function < String , Integer > f = String :: length ;
list . stream ()
. filter ( x -> x > 0 )
. map ( String :: valueOf )
. sorted ()
. distinct ()
. limit ( 10 )
. toList ();
list . stream (). count (); list . stream (). anyMatch ( p );
list . stream (). findFirst (); // Optional
list . stream (). mapToInt ( Integer :: intValue ). sum ();
list . stream (). collect ( groupingBy ( Person :: city ));
list . stream (). map ( Person :: name ). collect ( joining ( ", " ));
opt . orElse ( "default" ); opt . orElseThrow (); opt . ifPresent ( IO :: println );
Files
var p = Path . of ( "file.txt" );
Files . readString ( p ); Files . writeString ( p , "content" );
Files . readAllLines ( p ); Files . lines ( p ); // stream, close it
Files . exists ( p ); Files . size ( p ); Files . createDirectories ( p . getParent ());
Date and time
LocalDate . now () LocalDate . of ( 2026 , 8 , 12 )
LocalDateTime . now () LocalTime . of ( 14 , 30 )
date . plusDays ( 7 ) date . minusMonths ( 1 )
date . format ( DateTimeFormatter . ofPattern ( "MM/dd/yyyy" ))
Period . between ( a , b ) Duration . ofMinutes ( 90 )
Concurrency
Thread . startVirtualThread (() -> … );
try ( var ex = Executors . newVirtualThreadPerTaskExecutor ()) {
var f = ex . submit (() -> compute ());
var result = f . get ();
}
var counter = new AtomicInteger ();
var map = new ConcurrentHashMap < String , Integer > ();
New in Java 25
Feature
What it means
compact source files + instance main
void main() without a class
java.lang.IO
IO.println, IO.readln, no import
module imports
import module java.base;
flexible constructor bodies
code before super(...)
scoped values
context instead of ThreadLocal
structured concurrency (preview)
StructuredTaskScope
primitive patterns (preview)
case int i in switch
Commands
java --version jshell
javadoc File.java jar --create --file x.jar -C out .
mvn test mvn package
./gradlew build ./gradlew run
java --enable-preview --source 25 File.java