value-of

Differences between new Integer(123), Integer.valueOf(123) and just 123

孤人 提交于 2019-11-26 19:43:29
问题 Recenlty I saw code (Java) like this: myMethod(new Integer(123)); I am currently refactoring some code, and there is a tip in Sonar tool, that it's more memory friendly to use sth like this: myMethod(Integer.valueOf(123)); However in this case, I think that there is no difference if I would use: myMethod(123); I could understand that, if I would pass a variable to the method, but hard coded int? Or if there would be Long/Double etc and I want Long representation of number. But integer? 回答1:

Override valueof() and toString() in Java enum

旧时模样 提交于 2019-11-26 17:38:15
问题 The values in my enum are words that need to have spaces in them, but enums can't have spaces in their values so it's all bunched up. I want to override toString() to add these spaces where I tell it to. I also want the enum to provide the correct enum when I use valueOf() on the same string that I added the spaces to. For example: public enum RandomEnum { StartHere, StopHere } Call toString() on RandomEnum whose value is StartHere returns string "Start Here" . Call valueof() on that same