How do I get the minimum or maximum value between two numbers?

The code below show you how to use the Math.min() and Math.max() method call the get the minimum and maximum value between two numbers. As other method in the Math class these methods also overloaded to accept many types of primitive data.

package org.kodejava.math;

public class GetMinMaxValueExample {
    public static void main(String[] args) {

        Double value1 = 100.0D;
        Double value2 = 200.0D;

        double max1 = Math.max(value1, value2);
        double min1 = Math.min(value1, value2);

        float max2 = Math.max(value1.floatValue(), value2.floatValue());
        float min2 = Math.min(value1.floatValue(), value2.floatValue());

        int max3 = Math.max(value1.intValue(), value2.intValue());
        int min3 = Math.min(value1.intValue(), value2.intValue());

        long max4 = Math.max(value1.longValue(), value2.longValue());
        long min4 = Math.min(value1.longValue(), value2.longValue());

        System.out.println("Max value in double: " + max1);
        System.out.println("Min value in double: " + min1);
        System.out.println("Max value in float : " + max2);
        System.out.println("Min value in float : " + min2);
        System.out.println("Max value in int   : " + max3);
        System.out.println("Min value in int   : " + min3);
        System.out.println("Max value in long  : " + max4);
        System.out.println("Min value in long  : " + min4);
    }
}

The result of the above program are:

Max value in double: 200.0
Min value in double: 100.0
Max value in float : 200.0
Min value in float : 100.0
Max value in int   : 200
Min value in int   : 100
Max value in long  : 200
Min value in long  : 100

How do I get the absolute value of a number?

The example below show you how to get the absolute value of a number. To get the absolute value or the abs value of a number we use the Math.abs() method call. The Math.abs() method is an overloaded that can accept value in type of double, float, int or long.

package org.kodejava.math;

public class GetAbsoluteValueExample {
    public static void main(String[] args) {
        Double value = -10.0D;

        double abs1 = Math.abs(value);
        System.out.println("Absolute value in double: " + abs1);

        float abs2 = Math.abs(value.floatValue());
        System.out.println("Absolute value in float : " + abs2);

        int abs3 = Math.abs(value.intValue());
        System.out.println("Absolute value in int   : " + abs3);

        long abs4 = Math.abs(value.longValue());
        System.out.println("Absolute value in long  : " + abs4);
    }
}

The code snippet above print the following result:

Absolute value in double: 10.0
Absolute value in float : 10.0
Absolute value in int   : 10
Absolute value in long  : 10

How do I read data from a buffer into channel?

In this example you’ll see how to read data from buffer using FileChannel.write() method call. Reading from a buffer means that you are writing data into the channel object. In the snippet below the data from our dummy buffer will be read and written into the result.txt file.

package org.kodejava.io;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class BufferRead {
    public static void main(String[] args) throws Exception {
        FileChannel channel = null;

        try {
            // Define an output file and create an instance of FileOutputStream
            File file = new File("result.txt");
            FileOutputStream fos = new FileOutputStream(file);

            // Create a dummy ByteBuffer which value to be read into a channel.
            ByteBuffer buffer = ByteBuffer.allocate(256);
            buffer.put(new byte[]{65, 66, 67, 68, 69, 70, 71, 72, 73, 74});

            // Change the buffer from writing mode to reading mode.
            buffer.flip();

            // Gets the channel from the FileOutputStream object and read the
            // data available in buffer using channel.write() method.
            channel = fos.getChannel();
            int bytesWritten = channel.write(buffer);
            System.out.println("written : " + bytesWritten);
        } finally {
            if (channel != null && channel.isOpen()) {
                channel.close();
            }
        }
    }
}

How do I change the buffer mode between write and read?

A buffer can be read or written. To change the mode between the write mode and read mode we use the flip() method call on the buffer.

There are few things that you should know when you change between the write mode to the read mode. A buffer have the capacity, position and limit properties. The capacity will always be the same on write or read mode. But the position and limit will store different information in each mode.

In write mode the limit will be equals to the capacity, but in read mode the limit will be equals to the last position where the last data were written. The position on the write mode point where the data will be written in the buffer. When calling the flip() method the position will be set to 0 and the limit will be set to the last value of the position.

package org.kodejava.io;

import java.nio.CharBuffer;

public class BufferFlip {
    public static void main(String[] args) {
        CharBuffer buffer = CharBuffer.allocate(1024);
        System.out.println("capacity = " + buffer.capacity());
        System.out.println("position = " + buffer.position());
        System.out.println("limit    = " + buffer.limit());

        buffer.put("Java NIO Programming");

        System.out.println("capacity = " + buffer.capacity());
        System.out.println("position = " + buffer.position());
        System.out.println("limit    = " + buffer.limit());

        buffer.flip();
        System.out.println("capacity = " + buffer.capacity());
        System.out.println("position = " + buffer.position());
        System.out.println("limit    = " + buffer.limit());
    }
}

The program will print the following result:

capacity = 1024
position = 0
limit    = 1024
capacity = 1024
position = 20
limit    = 1024
capacity = 1024
position = 0
limit    = 20

How do I write data into ByteBuffer using put method?

The snippet below show you how to write some bytes into the java.nio.ByteBuffer object through a call to the put() method.

package org.kodejava.io;

import java.nio.ByteBuffer;

public class BufferPut {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put((byte) 65);
        buffer.put((byte) 66);
        buffer.put((byte) 67);
        buffer.put((byte) 68);
        buffer.put((byte) 69);


        // Writes a sequence of bytes
        byte[] bytes = new byte[]{70, 71, 72, 73, 74};
        buffer.put(bytes);

        // Write to the beginning of the buffer
        buffer.put(0, (byte) 75);

        buffer.flip();

        while (buffer.hasRemaining()) {
            System.out.print((char) buffer.get());
        }
    }
}