FloatBuffer put() methods in Java with Examples
Last Updated :
06 Dec, 2018
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 parameter which is to be written in float buffer.
Return Value: This method returns this buffer, in which the float value is inserted.
Exception: This method throws the following exceptions:
- BufferOverflowException- If this buffer’s current position is not smaller than its limit
- ReadOnlyBufferException- If this buffer is read-only
Below are the examples to illustrate the put(float f) method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
fb.put( 7 .86F);
fb.rewind();
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original FloatBuffer: [8.56, 9.61, 7.86]
Example 2: To demonstrate BufferOverflowException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 8 .56F);
fb.put( 9 .61F);
fb.put( 7 .86F);
System.out.println( "Trying to put the float at the "
+ "position more than its limit" );
fb.put( 7 .86F);
fb.rewind();
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the float at the position more than its limit
Exception throws : java.nio.BufferOverflowException
Examples 3: To demonstrate ReadOnlyBufferException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
FloatBuffer fb1 = fb.asReadOnlyBuffer();
System.out.println( "Trying to put the float value"
+ " in read only buffer" );
fb1.put( 8 .56F);
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the float value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
put(int index, float f)
The put(int index, float f) method of java.nio.FloatBuffer Class is used to write the given float into the buffer at the given index.
Syntax:
public abstract FloatBuffer put(int index, float f)
Parameters: This method takes the following arguments as a parameter:
- index: The index at which the float will be written
- f: The float value to be written
Return Value: This method returns the this buffer.
Exception: This method throws the following exception:
- IndexOutOfBoundsException- If index is negative or not smaller than the buffer’s limit
- ReadOnlyBufferException- If this buffer is read-only
Below are the examples to illustrate the put(int index, float f) method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 0 , 8 .56F);
fb.put( 2 , 9 .61F);
fb.put( 1 , 7 .86F);
fb.rewind();
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original FloatBuffer: [8.56, 7.86, 9.61]
Example 2: To demonstrate IndexOutOfBoundsException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
fb.put( 0 , 8 .56F);
fb.put( 2 , 9 .61F);
System.out.println( "Trying to put the value"
+ " at the negative index" );
fb.put(- 1 , 7 .86F);
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException
Example 3: To demonstrate ReadOnlyBufferException.
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
FloatBuffer fb = FloatBuffer.allocate(capacity);
FloatBuffer fb1 = fb.asReadOnlyBuffer();
System.out.println( "Trying to put the float value"
+ " in read only buffer" );
fb1.put( 0 , 8 .56F);
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the float value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Share your thoughts in the comments
Please Login to comment...