FloatBuffer wrap() method in Java with Examples
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
float [] fbb = { 1 .23F, 2 .34F, 4 .56F };
System.out.println("Array length : " + fbb.length);
System.out.println("\nArray element : "
+ Arrays.toString(fbb));
FloatBuffer floatBuffer = FloatBuffer.wrap(fbb);
floatBuffer.rewind();
System.out.println("\nfloatBuffer : "
+ Arrays.toString(floatBuffer.array()));
System.out.println("\nfloatbuffer capacity : "
+ floatBuffer.capacity());
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
float [] fbb = { 1 .23F, 2 .34F, 4 .56F };
System.out.println("Array length : " + fbb.length);
System.out.println("\nArray element : "
+ Arrays.toString(fbb));
FloatBuffer floatBuffer = FloatBuffer.wrap(fbb, 0 ,
fbb.length);
floatBuffer.rewind();
System.out.println("\nfloatBuffer : "
+ Arrays.toString(floatBuffer.array()));
System.out.println("\nfloatbuffer capacity : "
+ floatBuffer.capacity());
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
float [] fbb = { 1 .23F, 2 .34F, 4 .56F };
System.out.println("Array length : " + fbb.length);
System.out.println("\nArray element : " + Arrays.toString(fbb));
try {
System.out.println("\nHere "
+ "offset and length does not hold"
+ " the required condition ");
FloatBuffer floatBuffer = FloatBuffer.wrap(fbb,
1 ,
fbb.length);
floatBuffer.rewind();
System.out.println("\nfloatBuffer : "
+ Arrays.toString(floatBuffer.array()));
System.out.println("\nfloatbuffer capacity : "
+ floatBuffer.capacity());
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
Last Updated :
19 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...