11/07/2014

Java - Type Casting vs valueOf

Java의 BufferWriter로 윈도우와 소켓통신을 하려는데 write 함수가 char array 을 데이터로 받는다.

int32나 uint64로 type으로 윈도우에서 받으려고 하는데, 내쪽에서 그냥 char로 casting 해서 보내면 default system encoding 으로 system dependency가 생길거 같아, 어떻게 해야할지 찾아보았다.

Just two different ways of doing the same thing. It may be a historical reason (can't remember if one came before the other).

큰 차이는 없다는 것 같기도 하면서

아래에 보면 Exception throw에서 차이가 있는 듯하다.

One huge difference is that if you invoke toString() in a null object you'll get a NullPointerException whereas, using String.valueOf() you may not check for null.


누구는 Integer가 null object가 없으니 valueOf 에서도 null check을 안한다고 하는데, 바로 아래에서는 된다고 하니 확인이 필요하다.



Ref: http://stackoverflow.com/questions/3335737/integer-tostringint-i-vs-string-valueofint-i




댓글 없음:

댓글 쓰기