Home  >  Core Java

Example of LongBuffer and ShortBuffer in Java NIO

By Arvind Rai, November 17, 2013
LongBuffer and ShortBuffer belongs to the java.nio package. These are abstract classes. To get instances of these class, there is allocate() method. Or we get objects by ByteBuffer as below.

ByteBuffer.asLongBuffer()
ShortBuffer.asLongBuffer()

Find the simple example.

BufferTest.java
package com.concretepage.nio;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
public class BufferTest {
	public static void main(String[] args) throws FileNotFoundException {
	   String s="12567.545";
	   LongBuffer lb = ByteBuffer.wrap(s.getBytes()).asLongBuffer();
           System.out.println(lb.get());
       
           ShortBuffer sb = ByteBuffer.wrap(s.getBytes()).asShortBuffer();
           System.out.println(sb.get());
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us