FloatBuffer wrap() method in Java with Examples
Last Updated :
19 Jan, 2023
wrap(float[] array)
The wrap() method of java.nio.FloatBuffer Class is used to wraps a float array into a buffer. The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero, and its mark will be undefined. It's backing array will be the given array, and its array offset will be zero.
Syntax :
public static FloatBuffer wrap(float[] array)
Parameters: This method takes array(The array that will back this buffer) as a parameter.
Return Value: This method returns the new float buffer.
Below are the examples to illustrate the wrap() method:
Examples 1:
Java
// Java program to demonstrate
// wrap() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the float array
float[] fbb = { 1.23F, 2.34F, 4.56F };
// print the float array length
System.out.println("Array length : " + fbb.length);
// print the float array element
System.out.println("\nArray element : "
+ Arrays.toString(fbb));
// wrap the float array into floatBuffer
// using wrap() method
FloatBuffer floatBuffer = FloatBuffer.wrap(fbb);
// Rewind the floatbuffer
floatBuffer.rewind();
// print the float buffer
System.out.println("\nfloatBuffer : "
+ Arrays.toString(floatBuffer.array()));
// print the FloatBuffer capacity
System.out.println("\nfloatbuffer capacity : "
+ floatBuffer.capacity());
// print the FloatBuffer position
System.out.println("\nfloatbuffer position: "
+ floatBuffer.position());
}
}
Output:Array length : 3
Array element : [1.23, 2.34, 4.56]
floatBuffer : [1.23, 2.34, 4.56]
floatbuffer capacity : 3
floatbuffer position: 0
wrap(float[] array, int offset, int length)
The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.length, its position will be offset, its limit will be offset + length, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
Syntax :
public static FloatBuffer wrap (float[] array, int offset, int length)
Parameters: This method takes following parameters:
- array: The array that will back the new buffer.
- offset: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer's position will be set to this value.
- length: The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer's limit will be set to offset + length.
Return Value: This method returns the new float buffer.
Throws: This method throws the IndexOutOfBoundsException(If the preconditions on the offset and length parameters do not hold).
Below are the examples to illustrate the wrap() method:
Examples 1:
Java
// Java program to demonstrate
// wrap() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the float array
float[] fbb = { 1.23F, 2.34F, 4.56F };
// print the float array length
System.out.println("Array length : " + fbb.length);
// print the float array element
System.out.println("\nArray element : "
+ Arrays.toString(fbb));
// wrap the float array into floatBuffer
// using wrap() method
FloatBuffer floatBuffer = FloatBuffer.wrap(fbb, 0,
fbb.length);
// Rewind the floatbuffer
floatBuffer.rewind();
// print the float buffer
System.out.println("\nfloatBuffer : "
+ Arrays.toString(floatBuffer.array()));
// print the FloatBuffer capacity
System.out.println("\nfloatbuffer capacity : "
+ floatBuffer.capacity());
// print the FloatBuffer position
System.out.println("\nfloatbuffer position: "
+ floatBuffer.position());
}
}
Output:Array length : 3
Array element : [1.23, 2.34, 4.56]
floatBuffer : [1.23, 2.34, 4.56]
floatbuffer capacity : 3
floatbuffer position: 0
Examples 2: To demonstrate NullPointerException
Java
// Java program to demonstrate
// wrap() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the float array
float[] fbb = { 1.23F, 2.34F, 4.56F };
// print the float array length
System.out.println("Array length : " + fbb.length);
// print the float array element
System.out.println("\nArray element : " + Arrays.toString(fbb));
try {
// wrap the float array into floatBuffer
// using wrap() method
System.out.println("\nHere "
+ "offset and length does not hold"
+ " the required condition ");
FloatBuffer floatBuffer = FloatBuffer.wrap(fbb,
1,
fbb.length);
// Rewind the floatbuffer
floatBuffer.rewind();
// print the float buffer
System.out.println("\nfloatBuffer : "
+ Arrays.toString(floatBuffer.array()));
// print the FloatBuffer capacity
System.out.println("\nfloatbuffer capacity : "
+ floatBuffer.capacity());
// print the FloatBuffer position
System.out.println("\nfloatbuffer position: "
+ floatBuffer.position());
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws: " + e);
}
}
}
Output:Array length : 3
Array element : [1.23, 2.34, 4.56]
Here offset and length does not hold the required condition
Exception throws: java.lang.IndexOutOfBoundsException
Similar Reads
FloatBuffer get() methods in Java with Examples get() The get() method of java.nio.FloatBuffer Class is used to reads the float at the given buffer's current position, and then increments the position. Syntax : public abstract float get() Return Value: This method returns the float value at the buffer's current position. Throws: This method throw
5 min read
FloatBuffer put() methods in Java with Examples put(float f) The put(float f) method of java.nio.FloatBuffer Class is used to write the given float into the newly created float buffer at the current position, and then increments the position. Syntax : public abstract FloatBuffer put(float f) Parameters: This method takes the float value f as a pa
6 min read
FloatBuffer flip() methods in Java with Examples The flip() method of java.nio.FloatBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be automa
2 min read
FloatBuffer slice() method in Java with Examples The slice() method of java.nio.FloatBuffer Class is used to creates a new float buffer whose content is a shared subsequence of the given buffer's content.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, a
3 min read
FloatBuffer equals() method in Java with Examples The equals() method of java.nio.FloatBuffer Class is used to check whether or not the given buffer is equal to another object. Two float buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, c
4 min read
FloatBuffer compact() method in Java With Examples The compact() method of java.nio.FloatBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is se
3 min read