ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바
    프로그래밍 언어/expression 2011. 8. 24. 17:42

    객체를 println, printf, .+(), assert 구문에 넘길 때, 혹은 디버거가 객체를 출력할 때 자동으로 객체의 toString 메소드를 호출한다.

    System.valueOf()

    StringBuilder.append()

     

    System.out.println()

    '객체의 toString() 메소드'를 자동 호출

     

    .+()

    참조 자료형은 + 연산만 가능하다

    객체인 경우 '객체의 toString() 메소드'를 자동 호출

    default: getClass().getName() + '@' + Integer.toHexString(hashCode())
    참조 자료형 중에서 String 클래스만 사용 가능
    모든 참조 자료형에 + 연산을 하게 되면 해당 클래스에 있는 toString() 메소드의 결과와 그 연산자 뒤에 있는 문자열을 더한다
     
    두 피연산자 중 하나가 문자열이면 .+()는 다른 피연산자를 문자열로 변환시킨다.
    '객체의 toString() 메소드'를 호출하면 객체는 문자열로 변환된다.
     
    .==()
    레퍼런스 타입인 피연산자의 경우 두 피연산자가 동일한 객체나 배열을 참조하고 있는지를 검사
     
     
    자바 인터프리터는 모든 기본형에 대해서 내장된(?) 문자열 변환을 가지고 있다.
    boolean 타입은 숫자로 변환할 수 없기 때문에 형 변환이 불가능하다
    자바는 기본형과 레퍼런스 타입 간의 상호 변환은 어떤 것이라도 허용하지 않는다.
    레퍼런스 변수들 사이의 형변환은 상속 관계에서만 가능
    모든 배열 타입은 서로 구별된다.
    배열은 Object를 제외하고 타입 계층도를 가지지 않는다.
     
     
    casting
     
    Character
     

    String

    .getBytes[] // byte[]

    .valueOf() // static

    주어진 객체나 기본 데이터형을 문자열로 바꾼다.

    .charAt()

    .split(String regex) // String[]

    .toCharArray() // char[]

    e.g. char[] values2 = javaText.toCharArray();

    .copyValueOf(char data[])    // String; static

    e.g. char values[]  = {'J', 'a', 'v', 'a'}; 

    String javaText = String.copyValueOf(values);

    더보기

    class StringEx7

    {

    public static void main(String[] args) 

    {

    int value = 100;

    String strValue = String.valueOf(value);

    /* or

    String strValue = value + "";

    */

    ...

    }

     

    }

    String과 같은 문자열을 숫자 타입으로 변환

    .parse타입이름(): 기본 자료형

    .valueOf(): 참조 자료형

     

    Byte

    .parseByte() // static

     

    Short

    .parseShort() // static

     
    Integer
    .MIN_VALUE // static
    .MAX_VALUE // static
     
    .parseInt(String) // static
    .valueOf() // static
     
    .toString() // static
    .toBinaryString() // static
    .toHexString() // static
     
    .intValue()
    .doubleValue()
     
    Long
    .parseLong() // static
     

    .toString() // static

     

    BigInteger

     

    Float

    .parseFloat() // static

    .valueOf() // static

     

    .floatValue()

     

    Double

    .parseDouble() // static

    .valueOf() // static

     

    .toString() // static

     

    .doubleValue()

     

    BigDecimal

     

    Enum
    .values() // static
     

    Arrays

    .toString() // static

    .asList() // static

     

    List<Shape> shapeList = Arrays.asList( new Circle(), new Square(), new Triangle() );

    List<String> list = Arrays.asList(new String[]{"a", "b", "c"});

    List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));

    List<>.of() // static; JDK 9

     

    String[] tempArray = list.toArray(new String[0]); // ArrayList<String> list = new ArrayList<>();
    HashMap<String, String> map = new HashMap<String, String>();Collection<String> values = map.values();

     

    public enum Spiciness { NOT, MILD, MEDIUM, HOT, FLAMING }

     

    ... Spiciness.values() ...

    더보기

    package typeinfo;

    class Building {}

    class House extends Building {}

    public class ClassCasts {

    public static void main(String[] args) {

    Building b = new House();

    Class<House> houseType = House.class;

    House h = houseType.cast(b);

    /* or

    h = (House)b;

    */

    }

    }

     

Designed by Tistory.