Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

flip
java

What is the doubleBuffer flip() method in Java?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The flip() method is part of the java.nio.DoubleBuffer class. This method flips the buffer, meaning the buffer is trimmed at the current position.

The programmer can set the current position using the doubleBuffer.position() method. This position is then set to zero after performing the flip operation.

Syntax

doubleBuffer.flip();

Return value

This method performs the flip operation and returns the double buffer instance with the flipping operation performed.

Code

Let’s look at a coding example to further understand this method.

import java.nio.*;
import java.util.*;
class HelloWorld {
public static void main( String args[] ) {
// initializing a double array
double[] arr = {20.0, 23.0, 45.2, 12.3, 21.1};
// use the wrap method to create a double buffer from array
DoubleBuffer buff
= DoubleBuffer.wrap(arr);
// set position at index 2
buff.position(2);
// printing the buffer, we can see position pos = 2
// and limit lim = 5
System.out.println("Buffer before applying flip: ");
System.out.println(buff);
// print the buffer array
System.out.println(Arrays.toString(buff.array()));
// Flip the Buffer using flip() method
buff.flip();
// printing the buffer, we can now see that position pos = 0
// and limit lim = 2
System.out.println("Buffer after applying flip: ");
System.out.println(buff);
// print the buffer array
System.out.println(Arrays.toString(buff.array()));
}
}

RELATED TAGS

flip
java

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring