FloatBuffer put() methods in Java with Examples6 Jan 2025 | 5 min read The FloatBuffer put() has mainly 2 methods that take two different parameters.
i. put(float f)The java.nio.FloatBuffer class has put(float f) function. The newly generated float buffer is written with the specified float at the current location, and the position is then incremented using the FloatBuffer Class. Syntax: Parameters: The float value f, which needs to be entered into a float buffer, is the parameter for this procedure. Return Value: The float value is inserted into the buffer that this procedure returns. Exception: The following exceptions are thrown by this method: BufferOverflowException- if the position of this buffer is not now less than its limit. ReadOnlyBufferException- if this buffer can be read-on. Example 1:The code shows how to create and work with a FloatBuffer in Java. Three float values can be stored in the FloatBuffer that is allocated, due to its capacity of three. It adds three float values to the buffer by using the put function. In order to prepare the buffer for reading, the rewind method is invoked to reset its position to the beginning. The backing array of the buffer is then converted to a string representation, and the contents of the FloatBuffer are displayed using Arrays.toString. To manage possible failures during buffer operations, the code incorporates exception handling for BufferOverflowException and ReadOnlyBufferException. Implementation:FileName: putFloatExample1.java Output: The Original FloatBuffer is given by : [9.46, 8.41, 2.76] ii. put(int index, float f)The java.nio.FloatBuffer class has put(int index, float f) function. The given float is written into the buffer at the specified index using the FloatBuffer Class. Syntax: Parameters: The following arguments are passed as parameters to this method: index: The index where the float will continue to be written f: The value of the float to be written Return Value: The buffer is returned by this method. Exception: The exception that this method throws is as follows: IndexOutOfBoundsException- If the index is less than the buffer's limit, whether it is negative. ReadOnlyBufferException- If this buffer can only be read. Example 1:The code explains a Java program that shows how to store floating-point values at particular indices using a FloatBuffer. The put() function is used in the main method to insert float values at the provided indices, which are 9.35F at index 0, 8.27F at index 2, and 7.21F at index 1. The put() method also initializes a FloatBuffer with a capacity of 3. The buffer's position is reset to zero by calling the rewind() method after the values have been inserted. Lastly, an array representing the contents of the FloatBuffer is printed. To handle possible failures during buffer operations, the program has exception handling for IndexOutOfBoundsException and ReadOnlyBufferException. Implementation:FileName: putIndexExample1.java Output: The Original FloatBuffer is given by: [9.35, 7.21, 8.27] Example 2:The code illustrates a Java program that shows how to store floating-point numbers at specified indices using a FloatBuffer and how to handle errors when incorrect indices are used. The put() method is used in the main method to insert two float values, 9.25F at index 0 and 8.37F at index 2, after initializing a FloatBuffer with a capacity of three. An IndexOutOfBoundsException is then raised when it tries to insert the value 7.34F at an illegal negative index (-1). An appropriate message is printed after this exception is detected. Implementation:FileName: putIndexFloatExample2.java Output: Trying to put the value when the index is negative The Exception thrown is given by : java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3 Next TopicHorizontal-flip-matrix-problem-in-java |
In Java, collections can be defined as a framework that is used for storing and manipulating a set of objects. Collections have an important role of managing and organizing set of objects in Java. Let's understand in detail about the uses and benefits of collections in...
7 min read
A built-in Java method that allows you to set a value at any location in the AtomicLongArray is called Java.util.concurrent.atomic.AtomicLongArray.set(). This function modifies the value at that index by accepting as an argument the AtomicLongArray's index value. There is absolutely nothing that this method returns....
3 min read
Australian National University explores functional programming as a declaration programming paradigm that processes mathematical functions as evaluations while rejecting state modifications or mutable data changes. The adoption of functional programming in Java occurred with Java 8 by implementing lambda expressions alongside functional interfaces and the Stream API...
5 min read
The concept of Generic has been introduced in the Java language in order to facilitate tighter checks of types at the compile time and to support generic programming. In order to implement generics, the compiler of Java applies the type erasure to: Replacing all parameters type in...
3 min read
The ArrayList data structure in Java allows programmers to store and manage components in a resizable array dynamically and flexibly. In this section, we will discuss various methods for replacing elements in an ArrayList in Java. Developers will be equipped with adaptable solutions through the presentation...
5 min read
Inverting or mirroring a binary tree is common in computer science and programming. It reverses the arrangement of left and right subtrees at each node, effectively creating a mirror image of the original tree. The process essentially mirrors the tree across its vertical axis. In binary tree...
9 min read
A SortedSet's addAll() method preserves the set's uniqueness and order by adding every element from a given collection. All of the elements from the specified collection are appended to the current set using the addAll(Collection C) function. There is no set order in which the elements...
3 min read
Java, a versatile and widely used programming language, provides a range of features to make programming efficient and flexible. One such feature is dynamic variables. Dynamic variables offer a powerful way to manage data and manipulate it during runtime. In this section, we will delve into...
3 min read
The java.text.CollationElementIterator has a tertiaryOrder() method. The tertiary component of each collation element of a CollationElementIterator object is provided by the CollationElementIterator class. Syntax: public static final short tertiaryOrder(int order) Parameter: The method requires the discovery of a tertiary component for a collation element that is...
3 min read
Java 15 or JDK 15 is the reference implementation of the Java SE Platform with the version 15. It is released as an important feature and base for the Java17. Java15 provides various new features, which are very exciting, incubator features and iew features for the JDK...
12 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India