Home Java Scanner Methods radix() Java Scanner radix() Method Definition In Java, sc.radix() returns the int base (radix) currently configured on the scanner for integer parsing.
Every new scanner starts with a default radix of 10 (decimal). Calling useRadix(r) updates what radix() returns.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.radix(...).
Syntax Inspect the scanner's active default base with sc.radix().
Scanner sc = new Scanner("ff 10");
int def = sc.radix(); // 10
sc.useRadix(16);
int hex = sc.radix(); // 16 Call What it does sc.radix()Returns the default base (2..36, initially 10). sc.useRadix(16)Changes the default radix returned by radix().
Calling a one-off overload such as sc.nextInt(2) uses base 2 for that single call only and leaves sc.radix() unchanged.
Library declarations // java.util.Scanner
public int radix() Parameters Parameter Description None radix() takes no parameters.
Return Value Return Value Description intThe active default radix, always between 2 and 36.
Important
radix() only governs integer methods (nextByte, nextShort, nextInt, nextLong, nextBigInteger, and their hasNext* counterparts) when called with no arguments. Floating-point methods like nextDouble() ignore radix() and always parse base 10.
Compare with Similar Methods Checking and updating the number base:
Method How it differs useRadix(r)Updates the scanner's default radix. nextInt(r)Overrides the base for one call without changing radix(). reset()Resets radix() back to 10.
Java Examples 1. Default and hex 2. One-off override keeps radix 3. Save and restore radix Example 1 — Check Default Radix 10 and Switch to Hex Base 16 Verify that a fresh scanner begins at base 10, update it to 16, and read hex tokens.
import java.util.Scanner;
public class ScannerRadixShow {
public static void main(String[] args) {
try (Scanner sc = new Scanner("ff 10")) {
System.out.println("Default radix: " + sc.radix());
sc.useRadix(16);
System.out.println("Updated radix: " + sc.radix());
System.out.println("ff -> " + sc.nextInt());
System.out.println("10 -> " + sc.nextInt());
}
}
} Default radix: 10
Updated radix: 16
ff -> 255
10 -> 16 How It Works Initial base is 10. Printing sc.radix() on a new scanner shows 10.Configured base persists. After useRadix(16), sc.radix() stays 16 for every subsequent integer read.Both tokens parsed as hex. "ff" evaluates to 255 and "10" evaluates to 16.Example 2 — See How nextInt(2) Leaves sc.radix() Unchanged Compare passing a radix argument to nextInt(2) versus calling useRadix(2).
import java.util.Scanner;
public class ScannerRadixOverride {
public static void main(String[] args) {
try (Scanner sc = new Scanner("1010 25")) {
int bin = sc.nextInt(2);
System.out.println("One-off binary: " + bin + ", radix still: " + sc.radix());
int dec = sc.nextInt();
System.out.println("Next decimal: " + dec);
}
}
} One-off binary: 10, radix still: 10
Next decimal: 25 How It Works Argument overrides one read only. nextInt(2) parses "1010" in binary without mutating the scanner state.Default base remains intact. sc.radix() still reports 10 immediately afterwards.Plain nextInt() uses base 10. The second call reads "25" as decimal twenty-five.Example 3 — Save the Current Radix, Read Binary Flags, and Restore Save sc.radix() before switching to binary temporarily, then restore it cleanly.
import java.util.Scanner;
public class ScannerRadixRestore {
public static void main(String[] args) {
try (Scanner sc = new Scanner("1111 42")) {
int saved = sc.radix();
sc.useRadix(2);
int mask = sc.nextInt();
sc.useRadix(saved);
int count = sc.nextInt();
System.out.println("Mask (from binary): " + mask + ", Count: " + count + ", Radix: " + sc.radix());
}
}
} Mask (from binary): 15, Count: 42, Radix: 10 How It Works Save current state. Capturing saved = sc.radix() preserves whatever base the caller had active.Read binary bitmask. Switching to base 2 turns "1111" into decimal 15.Restore seamlessly. Putting saved back lets "42" read as base 10.Frequently Asked Questions What does Scanner.radix() return initially? 10, representing standard base-10 decimal integers.
Does calling nextInt(16) change what sc.radix() returns? No. Passing a radix directly to nextInt(16) applies only to that call; sc.radix() stays unchanged.
How do I change the radix permanently for future calls? Call sc.useRadix(newRadix) with a value between 2 and 36.
Does reset() reset radix() back to 10? Yes. sc.reset() resets delimiter, locale, and radix to their defaults.
Does radix() affect nextDouble()? No. Floating-point scanning always uses base 10 regardless of radix().
Next: reset() Reset delimiter, locale, and radix to their defaults without rewinding input.
reset() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion