- 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 slice() method in Java
A new CharBuffer with the content as a shared subsequence of the original CharBuffer can be created using the method slice() in the class java.nio.CharBuffer. This method returns the new CharBuffer that is read-only if the original buffer is read-only and direct if the original buffer is direct.
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) { int n = 5; try { CharBuffer buffer1 = CharBuffer.allocate(n); buffer1.put('A'); buffer1.put('B'); buffer1.put('C'); System.out.println("The Original CharBuffer is: " + Arrays.toString(buffer1.array())); System.out.println("The position is: " + buffer1.position()); System.out.println("The limit is: " + buffer1.limit()); CharBuffer buffer2 = buffer1.slice(); System.out.println("
The Subsequence CharBuffer is: " + Arrays.toString(buffer2.array())); System.out.println("The position is: " + buffer2.position()); System.out.println("The limit is: " + buffer2.limit()); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
Output
The Original CharBuffer is: [A, B, C, , ] The position is: 3 The limit is: 5 The Subsequence CharBuffer is: [A, B, C, , ] The position is: 0 The limit is: 2
- 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 wrap() method in Java
- CharBuffer compact() method in Java
- CharBuffer duplicate() method in Java
- CharBuffer allocate() method in Java
- IntBuffer slice() method in Java
- FloatBuffer slice() method in Java
- DoubleBuffer slice() method in Java

Advertisements