The snippet library.
Small, practical examples. Search by task or narrow things down below.
Calculate a simple total
Java: Multiply a unit price by a quantity, then add a delivery fee.
Java✓ Tested
Call a library function
Java: Use a standard absolute-value function with a small negative number.
Java✓ Tested
Choose between two branches
Java: Use if and else to handle an available or empty stock count.
Java✓ Tested
Combine text with a name
Java: Construct a greeting from a fixed prefix and a stored name.
Java✓ Tested
Compare a value with a limit
Java: Turn a comparison into a readable yes or no result.
Java✓ Tested
Convert known numeric text to an integer
Java: Convert a trusted literal to an integer before adding. Validate external input before converting it.
Java✓ Tested
Count the items in a collection
Java: Read the collection’s length instead of hard-coding the loop limit.
Java✓ Tested
Divide using a fractional number
Java: Use a floating-point operand to preserve the fractional part of a division.
Java✓ Tested
Explain a calculation with a comment
Java: Keep the code executable while documenting why a fixed input is used.
Java✓ Tested
Find what remains after division
Java: Use the remainder operator to count items left after forming complete groups.
Java✓ Tested
Handle three possible outcomes
Java: Check high, normal and low values with an ordered conditional chain.
Java✓ Tested
Measure a short text value
Java: Read the length of an ASCII string. Non-ASCII text may use multiple code units or bytes depending on the language.
Java✓ Tested