- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
CharBuffer wrap() method in Java
A character array can be wrapped into a buffer using the method wrap() in the class java.nio.CharBuffer. This method requires a single parameter i.e. the char array to be wrapped into a buffer and it returns the new buffer created. If the returned buffer is modified, then the contents of the array are also similarly modified and vice versa.
A program that demonstrates this is given as follows −
Example
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { try { char[] arr = { 'A', 'P', 'P', 'L', 'E' }; System.out.println("The array length is: " + arr.length); System.out.println("Array elements are: " + Arrays.toString(arr)); CharBuffer buffer = CharBuffer.wrap(arr); buffer.rewind(); System.out.println("
The CharBuffer is: " + Arrays.toString(buffer.array())); System.out.println("The position is: " + buffer.position()); System.out.println("The capacity is: " + buffer.capacity()); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
Output
The array length is: 5 Array elements are: [A, P, P, L, E] The CharBuffer is: [A, P, P, L, E] The position is: 0 The capacity is: 5
- Related Articles
- CharBuffer equals() method in Java
- CharBuffer put() method in Java
- CharBuffer asReadOnlyBuffer() method in Java
- CharBuffer hasArray() method in Java
- CharBuffer array() method in Java
- CharBuffer arrayOffset() method in Java
- CharBuffer get() method in Java
- CharBuffer compareTo() method in Java
- CharBuffer slice() method in Java
- CharBuffer compact() method in Java
- CharBuffer duplicate() method in Java
- CharBuffer allocate() method in Java
- IntBuffer wrap() method in Java
- FloatBuffer wrap() method in Java
- DoubleBuffer wrap() method in Java

Advertisements