Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

How to use the IntBuffer flip() method in Java

Ayesha Naeem

The IntBuffer flip() method flips a buffer, sets the limit to its current position, sets the position to zero, and discards any marks.

The program needs to import the following module to use the IntBuffer mark() method:

java.nio.IntBuffer Class

Syntax

public final IntBuffer flip()

Parameters and return type

The IntBuffer flip() method accepts no parameters and returns a flipped IntBuffer.

Examples

The following example demonstrates how to use the IntBuffer flip() method in Java.

  • The program creates a buffer using an array through the IntBuffer.wrap() method.
  • It then sets the position of the buffer to 22 and displays the buffer.
  • The program flips the buffer and displays it, showing that the limit has been changed from 66 to 22, which was the buffer’s position. Additionally, the position of the buffer is set to 00.


import java.nio.*;
import java.util.*;

public class main {
	public static void main(String[] args)
	{

		// Declare and initialize an int array
		int[] arr
			= { 11,22,33,44,55,66 };

		//create a buffer
		IntBuffer buf = IntBuffer.wrap(arr);

		// set buffer position at 2
		buf.position(2);

		// display buffer
		System.out.println(	"Before buffer flipping: "	+ Arrays.toString(buf.array())
	    	+ "\nPosition: "+ buf.position()
			+ "\nLimit: "+ buf.limit());

		// Flip the Buffer
		// using flip() method
		buf.flip();

		// print the buffer
	System.out.println(	"After buffer flipping: "	+ Arrays.toString(buf.array())
	    	+ "\nPosition: "+ buf.position()
			+ "\nLimit: "+ buf.limit());
	}
}

RELATED TAGS

java

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring