Open In App

FloatBuffer put() methods in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 3;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer using put() method
            fb.put(8.56F);
            fb.put(9.61F);
            fb.put(7.86F);
            fb.rewind();
  
            // print the FloatBuffer
            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.




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 3;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer using put() method
            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);
  
            // rewind the floatbuffer
            fb.rewind();
  
            // print the FloatBuffer
            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.




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 3;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity using allocate() method
            FloatBuffer fb = FloatBuffer.allocate(capacity);
  
            // Creating a read-only copy of FloatBuffer
            // using asReadOnlyBuffer() method
            FloatBuffer fb1 = fb.asReadOnlyBuffer();
  
            System.out.println("Trying to put the float value"
                               + " in read only buffer");
  
            // putting the value in readonly floatbuffer
            // using put() method
            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:




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 3;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer using put() at  index 0
            fb.put(0, 8.56F);
  
            // putting the value in floatbuffer using put() at  index 2
            fb.put(2, 9.61F);
  
            // putting the value in floatbuffer using put() at  index 1
            fb.put(1, 7.86F);
  
            // rewinding the floatbuffer
            fb.rewind();
  
            // print the FloatBuffer
            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.




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 3;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer
            // using put() at  index 0
            fb.put(0, 8.56F);
  
            // putting the value in floatbuffer
            // using put() at  index 2
            fb.put(2, 9.61F);
  
            // putting the value in floatbuffer
            // using put() at  index -1
            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.




// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 3;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity using allocate() method
            FloatBuffer fb = FloatBuffer.allocate(capacity);
  
            // Creating a read-only copy of FloatBuffer
            // using asReadOnlyBuffer() method
            FloatBuffer fb1 = fb.asReadOnlyBuffer();
  
            System.out.println("Trying to put the float value"
                               + " in read only buffer");
  
            // putting the value in readonly floatbuffer
            // using put() method
            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


Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads