객체를 println, printf, .+(), assert 구문에 넘길 때, 혹은 디버거가 객체를 출력할 때 자동으로 객체의 toString 메소드를 호출한다.
System.valueOf()
StringBuilder.append()
System.out.println()
'객체의 toString() 메소드'를 자동 호출
.+()
참조 자료형은 + 연산만 가능하다
객체인 경우 '객체의 toString() 메소드'를 자동 호출
default: getClass().getName() + '@' + Integer.toHexString(hashCode())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
.toString() // static
BigInteger
Float
.parseFloat() // static
.valueOf() // static
.floatValue()
Double
.parseDouble() // static
.valueOf() // static
.toString() // static
.doubleValue()
BigDecimal
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 9String[] 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;
*/
}
}