Java Long byteValue Example

Long class byteValue method example.


Math class abs method example. This example shows you how to use byteValue method. This method returns the values of long, int,
int and long datatypes in its respective byte range only

Syntax: byteValue()

Here is the code:-

/**
 * @(#) ByteValueLong.java 
 * ByteValueLong class demonstrates the working of byteValue() method of Long class of lang package
 @version 9-May-2008
 @author Rose India Team
 */

public class ByteValueLong {

  public static void main(String[] args) {
  
      //the method returns Long instance object's value in to byte range type
    int value1;  long value2, value3;
    value1 = 123;
    value2 = 130;// in result wiil be -127 because 129 crosses byte range thus as per the rule method 
    //subtractes 127 from 129 and locates it at the position from the beginning that is from -127
    // this number is out of range therefore it will be printed as per rule
    // defined for getting number in a byte range(-128--127)
    value3 = 134;

    // three Long objects for getting three variable values in their
    // parameters
    Long obj1 = new Long(value1);
    Long obj2 = new Long(value2);
    Long obj3 = new Long(value3);

    // method here takes int and long variable values the fit it in byte range which is -128 to 127
    byte b1 = obj1.byteValue();
    byte b2 = obj2.byteValue();
    byte b3 = obj3.byteValue();

    //the method 
    System.out.printlnb1 + " ------- 123 comes under byte range so it is as it is generated by the method  ");
    System.out.println(" ");
    System.out.println"("+b2 +")" " -------130 crosses byte range limit by difference +3 therefore counting from -128, -127, -126 method will switch to -126 ");
    System.out.println(" ");
    System.out.println("("+b3+")"  "-------134 crosses byte range limit by difference +6 therefore counting from -128, -127, -126--- method will switch to -122 ");
  }
}

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US