Java Scanner radix() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Scanner

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().

Java
Scanner sc = new Scanner("ff 10");
int def = sc.radix();     // 10
sc.useRadix(16);
int hex = sc.radix();     // 16
CallWhat 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
// java.util.Scanner
public int radix()

Parameters

ParameterDescription
Noneradix() takes no parameters.

Return Value

Return ValueDescription
intThe active default radix, always between 2 and 36.

Compare with Similar Methods

Checking and updating the number base:

MethodHow 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

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.

Java
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());
        }
    }
}

How It Works

  1. Initial base is 10. Printing sc.radix() on a new scanner shows 10.
  2. Configured base persists. After useRadix(16), sc.radix() stays 16 for every subsequent integer read.
  3. 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).

Java
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);
        }
    }
}

How It Works

  1. Argument overrides one read only. nextInt(2) parses "1010" in binary without mutating the scanner state.
  2. Default base remains intact. sc.radix() still reports 10 immediately afterwards.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Save current state. Capturing saved = sc.radix() preserves whatever base the caller had active.
  2. Read binary bitmask. Switching to base 2 turns "1111" into decimal 15.
  3. Restore seamlessly. Putting saved back lets "42" read as base 10.

Frequently Asked Questions

10, representing standard base-10 decimal integers.
No. Passing a radix directly to nextInt(16) applies only to that call; sc.radix() stays unchanged.
Call sc.useRadix(newRadix) with a value between 2 and 36.
Yes. sc.reset() resets delimiter, locale, and radix to their defaults.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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