This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Saturday, January 30, 2010

28 - Review - xxxValue() - parseXxx() - valueOf() - Java 1.6.0_17

package chap3review;

public class Main {

    public static void main(String[] args) {
      
        // NOT STATIC
        //xxxValue()

        Byte aByte = 125;
        byte b = aByte.byteValue();
        short s = aByte.shortValue();
        int i = aByte.intValue();
        long l = aByte.longValue();
        float f = aByte.floatValue();
        double d =  aByte.doubleValue();

        Integer aInteger = 6753;
        i = aInteger.intValue();
        l = aInteger.longValue();
        f = aInteger.floatValue();
        d = aInteger.doubleValue();

        Boolean aBoolean = true;
        boolean bo = aBoolean.booleanValue();

        // Static
        // parseXxx

        b = Byte.parseByte("127"); // Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"256" Radix:10
        // 8 bits signed -128..127 !!!
        i = Integer.parseInt("123456");
        bo = Boolean.parseBoolean("TRUE");
      
        //static 
        // valueOf()

        Byte anotherByte = Byte.valueOf("-128");
        anotherByte = Byte.valueOf(b); // NO BYTE LITTERALS !!!

        Integer  anotherInteger = Integer.valueOf("123456");
        anotherInteger = Integer.valueOf(123456);
    }
}

1 comment:

  1. Hello. First of all thanks for your notes.

    b = Byte.parseByte("127"); // Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"256" Radix:10

    127 is acceptable. But based on Exception it should be Byte.parseByte("256") in code.

    ReplyDelete

Followers