Open In App

FloatBuffer limit() Method in Java with Examples

Last Updated : 10 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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-



Similar Reads

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 set to the number of floats copied. Syntax : public
3 min read
FloatBuffer duplicate() method in Java with Examples
The duplicate() method of java.nio.FloatBuffer Class is used to Create a new float buffer that shares the given buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent. The ne
3 min read
FloatBuffer asReadOnlyBuffer() method in Java with Examples
The asReadOnlyBuffer() method of java.nio.FloatBuffer Class is used to create a new, read-only float buffer with this buffer's content. The new buffer is a replica of this buffer. Hence changes made to this buffer's content will be visible in the new buffer. Since the new buffer is read-only, therefore any modification to its content won't be allow
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, considered independently of their starting position
4 min read
FloatBuffer array() method in Java With Examples
The array() method of java.nio.FloatBuffer Class is used to Return the float array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure that this buffer has an accessible backing ar
3 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, and vice versa. The two buffers' position, limit, a
3 min read
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 it
4 min read
FloatBuffer allocate() method in Java With Examples
The allocate() method of java.nio.FloatBuffer Class is used to allocate a new float buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing array, and its array offset will be zero.Syntax: publ
2 min read
FloatBuffer arrayOffset() method in Java With Examples
The arrayOffset() method of java.nio.FloatBuffer class is used to return the offset within the buffer's backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). Inorder to check whether this buffer has a backing array, hasArray() method
3 min read
FloatBuffer hasArray() method in Java with Examples
The hasArray() method of java.nio.FloatBuffer class is used to ensure whether or not the given buffer is backed by an accessible float array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() methods may safely be invoked, as they work on th
2 min read
Practice Tags :