Java Object to Char castToChar(Object value)

Here you can find the source of castToChar(Object value)

Description

cast To Char

License

Apache License

Declaration

public static final Character castToChar(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static final Character castToChar(Object value) {
        if (value == null) {
            return null;
        }/* w  ww. ja  v a2 s .c om*/

        if (value instanceof Character) {
            return (Character) value;
        }

        if (value instanceof String) {
            String strVal = (String) value;

            if (strVal.length() == 0) {
                return null;
            }

            if (strVal.length() != 1) {
                throw new NumberFormatException("can not cast to byte, value : " + value);
            }

            return strVal.charAt(0);
        }

        throw new NumberFormatException("can not cast to byte, value : " + value);
    }
}

Related

  1. castToCharacter(Object value)
  2. objectToChar(Object p1)