Thursday, June 11, 2026
HomeLanguagesJavaFloatBuffer limit() Method in Java with Examples

FloatBuffer limit() Method in Java with Examples

The limit() method of java.nio.FloatBuffer Class is used to modify this FloatBuffer’s limit. This method takes the limit to be set as the parameter and sets that as the new limit of this Buffer. If the mark of this Buffer is already defined and is larger than the new specified limit, then this new limit is not set and discarded.

Syntax:

public final FloatBuffer limit(int newLimit)

Parameter: The method takes one parameter newLimit of integer type which refers to the limit that is to be set as the new limit of the buffer.

Return Value: The method returns this buffer after setting the specified new limit as the new limit of this Buffer.

Below examples illustrate the limit() method:

Examples 1:




// Java program to demonstrate
// limit() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating FloatBuffer
        // using allocate() method
        FloatBuffer floatBuffer
            = FloatBuffer.allocate(4);
  
        // put float value in FloatBuffer
        // using put() method
        floatBuffer.put(20.5f);
        floatBuffer.put(30.5f);
  
        // print the float buffer
        System.out.println(
            "FloatBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
  
        // Limit the floatBuffer
        // using limit() method
        floatBuffer.limit(1);
  
        // print the float buffer
        System.out.println(
            "\nFloatBuffer after "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
    }
}


Output:

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4

FloatBuffer after setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 1
Limit: 1

Examples 2:




// Java program to demonstrate
// limit() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating FloatBuffer
        // using allocate() method
        FloatBuffer floatBuffer
            = FloatBuffer.allocate(5);
  
        // put float value in FloatBuffer
        // using put() method
        floatBuffer.put(20.5f);
        floatBuffer.put(30.5f);
        floatBuffer.put(40.5f);
  
        // mark will be going to
        // discarded by limit()
        floatBuffer.mark();
  
        // print the float buffer
        System.out.println(
            "FloatBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
  
        // Limit the floatBuffer
        // using limit() method
        floatBuffer.limit(4);
  
        // print the double buffer
        System.out.println(
            "\nFloatBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
    }
}


Output:

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 4

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#limit-int-

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS