Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the DoubleBuffer put() method in Java?

Kainat Asif

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.

java.nio.DoubleBuffer is a class that can be used to store a buffer of double values. The put() method of the java.nio.DoubleBuffer class is used to write a double to a buffer. The DoubleBuffer.put() method writes the double at the current positionThe index of the next element of the buffer that will be read or written of the buffer and increments the position by one.

Declaration

The DoubleBuffer.put() method can be declared as:

buff1.put(x);
  • buff1: The DoubleBuffer in which the double x will be written.
  • x: The double that will be written to buff1.

Return value

The DoubleBuffer.put() method returns the DoubleBuffer buff1 after writing the double x to it.

Note:

  • If the position of buff1 is not less than the limitThe first index of the buffer that should not be read or written of buff1, the BufferOverflowException is thrown.
  • If buff1 is read-only, the ReadOnlyBufferException is thrown.

Example

Example 1

Consider the code snippet below, which demonstrates the use of the DoubleBuffer.put() method.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 5;
int n2 = 4;
try {
DoubleBuffer buff1 = DoubleBuffer.allocate(n1);
buff1.put(1.2);
buff1.put(5.9);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
System.out.println("position: " + buff1.position());
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A DoubleBuffer buff1 is declared in line 8.
  • An element is written to buff1 using the DoubleBuffer.put() method in line 9. After adding the first element, the position of buff1 is incremented from 0 to 1.
  • Another element is written to buff1 using the DoubleBuffer.put() method in line 10. After adding the second element, the position of buff1 is incremented from 1 to 2.

Example 2

As explained above, using the DoubleBuffer.put() method on a read-only buffer throws the ReadOnlyBufferException. Consider the code snippet below that demonstrates this.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 5;
int n2 = 4;
try {
DoubleBuffer buff1 = DoubleBuffer.allocate(n1);
DoubleBuffer buff2 = buff1.asReadOnlyBuffer();
buff2.put(1.2);
buff2.put(5.9);
System.out.println("buff2: " + Arrays.toString(buff2.array()));
System.out.println("position: " + buff2.position());
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A DoubleBuffer buff1 is declared in line 8.
  • A DoubleBuffer buff2 is declared in line 10 that is the read-only copy of buff1.
  • The DoubleBuffer.put() method is used in line 11 to try writing a value to buff2. The ReadOnlyBufferException is thrown because buff2 is read-only and cannot be modified.

RELATED TAGS

java

CONTRIBUTOR

Kainat Asif
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