---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 11 Edition"
java_version: 11
chapter: "Chapter 4. The class File Format"
section: "4.10.1.9.swap"
heading: "swap"
parent: "4.10.1.9. Type Checking Instructions"
source: https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.10.1.9.swap
id: "4.10.1.9.swap"
url: https://jvm.obfuscated.software/jvms/se11/4.10.1.9.swap.html
raw: https://jvm.obfuscated.software/jvms/se11/4.10.1.9.swap.md
---

# swap

A *swap* instruction is type safe iff one can validly replace two category 1 types, `Type1` and `Type2`, on the incoming operand stack with the types `Type2` and `Type1` yielding the outgoing type state.

```
instructionIsTypeSafe(swap, _Environment, _Offset, StackFrame,
                      NextStackFrame, ExceptionStackFrame) :-
    StackFrame = frame(_Locals, [Type1, Type2 | Rest], _Flags),
    popCategory1([Type1 | Rest], Type1, Rest),
    popCategory1([Type2 | Rest], Type2, Rest),
    NextStackFrame = frame(_Locals, [Type2, Type1 | Rest], _Flags),
    exceptionStackFrame(StackFrame, ExceptionStackFrame).
```
