layout: tutorial title: Mutable Bytes description: Mutable Bytes group: nav-right categories: [“bytes”] previous: manipulating-bytes.md next: own-bytes-class.md

By default, bytes objects are not mutable. You can use MutableBytes objects instead.

Creating MutableBytes objects

The methods described in the tutorial “Creating Bytes” all work in the same way for MutableBytes.

You can call the method mutableCopy() on any Bytes object to get a copy of the Bytes object as mutable.

Finally, you can create fresh objects with the create() method.

Fill, Clear

Fill a MutableBytes with the same byte the fill method:

{%highlight java%} MutableBytes bytes = MutableBytes.create(2); bytes.fill((byte) 34); assertEquals(Bytes.fromHexString(“0x2222”), bytes); {%endhighlight%}

You can clear the contents with the clear method:

{%highlight java%} MutableBytes bytes = MutableBytes.fromHexString(“0xdeadbeef”); bytes.clear(); {%endhighlight%}

Setting values

You can set values with different arguments: