FloatBuffer get() methods in Java with ExamplesThe FloatBuffer get() has two main methods.
get():The java.nio.FloatBuffer Class's has get() function. The FloatBuffer Class is used to read the float at the current position of the buffer and increment its value. Syntax: Return Value: The float value at the buffer's current position is returned by this method. BufferUnderflowException: This exception is raised in a notification that the buffer's current position is not less than its maximum. Example 1:The Java code shows how to use a FloatBuffer. Three float values are added to a FloatBuffer that has been allocated with a capacity of 5. The rewind() function is used to reset the buffer's position to the beginning. The contents of the initial buffer are shown, and then the get() method is used to read and display the float values at the current and next places. Any possible IllegalArgumentException that may be thrown during these activities is handled by the program. Implementation:FileName: BufferGetExample1.java Output: The Original FloatBuffer is given by: [8.56, 9.61, 1.24, 0.0, 0.0] The Float Value is: 8.56 The Next Float Value is: 9.61 Example 2:The given Java program that uses the FloatBuffer from the java.nio package is shown. First, a FloatBuffer with a capacity of three is allocated. Two float values, 7.26F, and 8.97F, are then added to the buffer. The buffer's contents are shown by calling Arrays.toString(floatbuf.array()). Using the get() method, the program reads and outputs the float values at the current and next places. The buffer captures and prints an IllegalArgumentException if the location of the buffer goes beyond the limit. Implementation:FileName: getFloatExample2.java Output: The Original FloatBuffer is given by: [7.26, 8.97, 0.0] The Float Value is: 0.0 Since the current position of the buffer is increased to exceed its upper limitException in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.Buffer.nextGetIndex(Buffer.java:702) at java.base/java.nio.HeapFloatBuffer.get(HeapFloatBuffer.java:172) at getFloatExample2.main(getFloatExample2.java:28) get(int index):The article depicts about the processed at a specified index using the FloatBuffer get(int index) method. Syntax: Parameters: This method's parameter is an index, which is the index from which the float will be read. Return Value: The float value at the specified index is returned by this method. Exception: The IndexOutOfBoundsException is thrown by this method. This exception is raised if the index is negative or not less than the buffer's limit. Example 1:The Java program that is given shows how to use the FloatBuffer from the java.nio package. First, a FloatBuffer with a capacity of three is allocated, and three float values (7.26F, 8.23F, and 9.14F) are placed inside of it. Arrays.toString(floatbuff.array()) is used to display the contents of the buffer. The get(int index) function is then used by the program to retrieve and print the float values at particular indices (0, 1, and 2). The program ensures robust error handling by catching and printing the necessary error messages in the event of an illegal argument or index out of bounds exception. Implementation:FileName: GetIndexFloatExample1.java Output: The Original FloatBuffer is given by: [7.26, 8.23, 9.14] The Float Value at index 0 is: 7.26 The Float Value at index 1 is: 8.23 The Float Value at index 2 is: 9.14 Example 2:This Java program shows how to use the java.nio package's FloatBuffer operations. It adds three float values (7.26F, 8.23F, and 9.14F) and allots a FloatBuffer with a capacity of 3. Arrays.toString(floatbuff.array()) is used to display the contents of the buffer. The get(int index) method is used by the application to retrieve and print float data at specified indices. An exception is raised when an attempt is made to access an out-of-bounds index (index 7). The program prints the relevant error messages for each sort of problem, including IllegalArgumentException, IndexOutOfBoundsException, and BufferUnderflowException. Implementation:FileName: GetIndexFloatExample2.java Output: The Original FloatBuffer is given by: [7.26, 8.23, 9.14] The Float Value at index 0 is: 7.26 The Float Value at index 1 is: 8.23 ReadOnlyBufferException is catched |
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