Home>

What if I assign a Net typeclass to the String.valueOf () method?
Is it possible to cast the Net type variable Nom01 to a String using String.valueOf ()?

public String test (String netNo) {
   Net Nom01 = Yoyaku (netNo);
   String Nom02 = String.valueOf (Nom01);
}
  public String Yoyaku (String in Data) {
       String ch = inData;
    return Ch;
}
}
class Net {
   private String num;
   public String getNetNo () {
      return netno;
   }
  public String setNetNo () {
      this.netno;
   }

}
  • Answer # 1

    As already

    Here is the implementation of String # valueOf (Object).

      public static String valueOf (Object obj) {
            return (obj == null)? "null": obj.toString ();
        }

    If the argument is null, it returns "null", otherwise it just calls the argument's toString () method.

    So what's going on with this is
    String.valueOf (Nom01)
    When, the contents of Nom01 are passed to Object obj of the valueOf method and the method is called.

    Of the code aboveobjThe contents areNom01It will be a reference to the same instance as.
    (Actually, think of obj = Nom01)

    return (obj == null)? "null": obj.toString ();
    Is a ternary operator, so first check if obj, that is, Nom01, is null.
    If it is null,"null"Returns the string.
    Otherwise
    obj.toString ()Is called, and the return value is used as it is as its own return value.
    Here, obj means Nom01, so what is called is Nom01, and by extension, the toString method of the Net class.

    Such a method is the valueOf (Object) method.

    So what the valueOf method does is not convert an object to a String,
    If it was null, just return the string "null"
    The conversion to the actual string is thrown into the method of the passed object itself.

    Next, it is .toString that was thrown round, but since the definition of Net class is like this,

    class Net {
       private String num;
       public String getNetNo () {
          return netno;
       }
       public String setNetNo () {
          this.netno;
       }
    }

    In the first place, there is no toString method in the Net class.

    However, in Java, all classes are supposed to be in a class tree whose ancestors are the Object class.
    If you do not specify any class inheritance, the Object class is automatically inherited.
    This is such a specification.

    And the toString method is in the Object class.
    In other words, the Net class that inherits from the Object class also has a toString method.

    But I don't think that implementation is what you expect.
    So you need to override the toString method in your Net class to get it to work the way you want.
    By overriding, you can set any value as the instance string.

    For example, like this.

    @Override
      public String toString () {
        return "string you want to return";
      }


    (If you look closely, the definition of Net class is messed up ^^;
    I couldn't give an example)

    Only after doing thatString.valueOf (Nom01)Works as expected.

  • Answer # 2

    Here is the implementation of String # valueOf (Object).

      public static String valueOf (Object obj) {
            return (obj == null)? "null": obj.toString ();
        }

    If the argument is null, it returns "null", otherwise it just calls the argument's toString () method.

    Is it possible to cast to a String using String.valueOf ()?

    The return value is a String, so there is no need to cast it to a String type.