Conditional operator in concatenated string

后端 未结 4 1870
天命终不由人
天命终不由人 2020-12-11 01:35

I\'d like know why the following program throws a NPE

public static void main(String[] args) {
    Integer testInteger = null;
    String test = \"test\" + t         


        
4条回答
  •  不知归路
    2020-12-11 01:51

    I believe you need to add the parenthesis. Here is a working example which produces "http://localhost:8080/catalog/rest"

    public static String getServiceBaseURL(String protocol, int port, String hostUrl, String baseUrl) {
        return protocol + "://" + hostUrl + ((port == 80 || port == 443) ? "" : ":" + port) + baseUrl;
    }
    

提交回复
热议问题