Java by API/java.lang/Integer — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:40, 31 мая 2010

Integer: bitCount(int i)

 
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int n = 170; // 10101010
    System.out.println("Value in binary: 10101010");
    System.out.println("Number of one bits: " + Integer.bitCount(n));
    System.out.println("Highest one bit: " + Integer.highestOneBit(n));
    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;
    for (int i = 0; i < 16; i++) {
      n = Integer.rotateLeft(n, 1);
      System.out.println(n);
    }
  }
}





Integer: equals(Object obj)

 
/*
 * Output:
 
true


 */

public class MainClass {
  public static void main(String args[]) {
    Integer iobj1 = new Integer(5);
    Integer iobj2 = new Integer("5");
    System.out.println(iobj1.equals(iobj2));
  }
}





Integer: highestOneBit(int i)

 
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int n = 170; // 10101010
    System.out.println("Value in binary: 10101010");
    System.out.println("Number of one bits: " + Integer.bitCount(n));
    System.out.println("Highest one bit: " + Integer.highestOneBit(n));
    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;
    for (int i = 0; i < 16; i++) {
      n = Integer.rotateLeft(n, 1);
      System.out.println(n);
    }
  }
}





Integer: Integer.SIZE

 
public class Main {
  public static void main(String[] arg) {
    System.out.println(Integer.SIZE);   
  }
}





Integer: intValue()

 
/*
 * Output:
 
i1 = 5
i2 = 6

 */

public class MainClass {
  public static void main(String args[]) {
    Integer iobj1 = new Integer(5);
    Integer iobj2 = new Integer("6");
    int i1 = iobj1.intValue();
    int i2 = iobj2.intValue();
    System.out.println("i1 = " + i1);
    System.out.println("i2 = " + i2);
  }
}





Integer: lowestOneBit(int i)

 
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int n = 170; // 10101010
    System.out.println("Value in binary: 10101010");
    System.out.println("Number of one bits: " + Integer.bitCount(n));
    System.out.println("Highest one bit: " + Integer.highestOneBit(n));
    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;
    for (int i = 0; i < 16; i++) {
      n = Integer.rotateLeft(n, 1);
      System.out.println(n);
    }
  }
}





Integer.MAX_VALUE

 
/*
i=32767
i=-32768
i=-32767
i=2147483647
i=-2147483648
i=-2147483647 
 */
public class MainClass {
  public static void main(String[] unused) {
    do_shorts();
    do_ints();
  }
  protected static void do_shorts() {
    short i = Short.MAX_VALUE;
    System.out.println("i=" + i++);
    System.out.println("i=" + i++);
    System.out.println("i=" + i++);
  }
  protected static void do_ints() {
    int i = Integer.MAX_VALUE;
    System.out.println("i=" + i++);
    System.out.println("i=" + i++);
    System.out.println("i=" + i++);
  }
}





Integer: MIN_VALUE

 
public class Main {
  public static void main(String[] arg) {
    System.out.println(Integer.MAX_VALUE);   
    System.out.println(Integer.MIN_VALUE);   
  }
}





Integer: numberOfLeadingZeros(int i)

 
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int n = 170; // 10101010
    System.out.println("Value in binary: 10101010");
    System.out.println("Number of one bits: " + Integer.bitCount(n));
    System.out.println("Highest one bit: " + Integer.highestOneBit(n));
    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;
    for (int i = 0; i < 16; i++) {
      n = Integer.rotateLeft(n, 1);
      System.out.println(n);
    }
  }
}





Integer: numberOfTrailingZeros(int i)

 
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int n = 170; // 10101010
    System.out.println("Value in binary: 10101010");
    System.out.println("Number of one bits: " + Integer.bitCount(n));
    System.out.println("Highest one bit: " + Integer.highestOneBit(n));
    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;
    for (int i = 0; i < 16; i++) {
      n = Integer.rotateLeft(n, 1);
      System.out.println(n);
    }
  }
}





Integer: parseInt(String stringValue)

 
/*
 * Output:
 
Sum is 8
 */
public class MainClass {
  public static void main(String args[]) {
    int i = Integer.parseInt("3");
    int j = Integer.parseInt("5");
    int sum = i + j;
    System.out.print("Sum is " + sum);
  }
}





Integer: reverseBytes(int i)

 
public class Main {
  public static void main(String args[]) {
      System.out.println(Integer.reverseBytes(10));
      System.out.println(Integer.reverseBytes(-10));
      System.out.println(Integer.reverseBytes(0));
  }
}





Integer: rotateLeft(int i, int distance)

 
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int n = 170; // 10101010
    System.out.println("Value in binary: 10101010");
    System.out.println("Number of one bits: " + Integer.bitCount(n));
    System.out.println("Highest one bit: " + Integer.highestOneBit(n));
    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;
    for (int i = 0; i < 16; i++) {
      n = Integer.rotateLeft(n, 1);
      System.out.println(n);
    }
  }
}





Integer: signum(int i)

 
public class Main {
  public static void main(String args[]) {
      System.out.println(Integer.signum(10));
      System.out.println(Integer.signum(-10));
      System.out.println(Integer.signum(0));
  }
}





Integer: toBinaryString(int intValue)

 
/*
 * Output:
 
Binary is 1011

 */
public class MainClass {
  public static void main(String args[]) {
    int i = 11;
    System.out.println("Binary is " + Integer.toBinaryString(i));
  }
}





Integer: toHexString(int intValue)

 
/*
 * Output:
 
Hex is b
 */
public class MainClass {
  public static void main(String args[]) {
    int i = 11;
    System.out.println("Hex is " + Integer.toHexString(i));
  }
}





Integer: toOctalString(int intValue)

 
/*
 * Output:
 
Octal is 13
 */
public class MainClass {
  public static void main(String args[]) {
    int i = 11;
    System.out.println("Octal is " +
      Integer.toOctalString(i));
  }
}





Integer: valueOf(String stringValue)

 
/*
 * Output:
 
true
c
12
2
13245
12341234
11234.123
4.321324123412341E10

 */
public class MainClass {
  public static void main(String args[]) {
    Boolean bool = Boolean.valueOf("true");
    Character c = new Character("c");
    Byte b = Byte.valueOf("12");
    Short s = Short.valueOf("2");
    Integer i = Integer.valueOf("13245");
    Long l = Long.valueOf("12341234");
    Float f = Float.valueOf("11234.1234");
    Double d = Double.valueOf("43213241234.123412341234");
    System.out.println(bool);
    System.out.println(c);
    System.out.println(b);
    System.out.println(s);
    System.out.println(i);
    System.out.println(l);
    System.out.println(f);
    System.out.println(d);
  }
}