Why is Solution$Cat@74a14482 removed?



  • public class Solution {
        public static void main(String[] args) {
            Cat cat2 = new Cat("Barsik", 3, 3, 3);
            System.out.println(cat2);
        }
        public static class Cat{
    
            public static int count = 0;
            private String name;
            private int age;
            private int weight;
            private int strength;
    
            public Cat(String name, int age, int weight, int strength) {
                count++;
                this.name = name;
                this.age = age;
                this.weight = weight;
                this.strength = strength;
    //            public String toString() { return name; }
            }
        }
    }
    


  • Decommissioning operators, e.g. println Unknown method toString at the facility (he is present at all facilities). This method serves to present the facility as a line.

    Each class has its own method. For example, Integer such code:

    public static String toString(int i, int radix) {
            if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
                radix = 10;
    
        /* Use the faster version */
        if (radix == 10) {
            return toString(i);
        }
    
        char buf[] = new char[33];
        boolean negative = (i &lt; 0);
        int charPos = 32;
    
        if (!negative) {
            i = -i;
        }
    
        while (i &lt;= -radix) {
            buf[charPos--] = digits[-(i % radix)];
            i = i / radix;
        }
        buf[charPos] = digits[-i];
    
        if (negative) {
            buf[--charPos] = '-';
        }
    
        return new String(buf, charPos, (33 - charPos));
    

    }

    The method appears to be returning the line. return new String(buf, charPos, (33 - charPos));

    Other classes have the same thing. It can be implemented in any class. You can write anything in there. return "hello, world"; That's the one. System.out.println This method will not appear. toString at the facility and will lead this "hello, world."

    In default for the facility, if not redesign the method toString()will lead us to the method. java.lang.Object.toString (i.e.) Cat It's an object and it's not clear to say that heir to class. java.lang.Objecta class Object default method ToString() The line is written:

    getClass().getName() + '@' + Integer.toHexString(hashCode())

    That's what you see.

    https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() :-

    As a consequence, in order to see something human when the object is decommissioned by the withdrawal operators, some logic in the method should be identified. toString()

    In your case, it's just that method of commenting and pulling out of the designer (because you got a method inside the designer I don't understand).

    public static class Cat {

    ...
    // объявление полей
    ...
    
    public Cat(String name, int age, int weight, int strength) {
        ...
        // некая логика конструктора
        ...
    }
    
    // волшебный метод, который превращает объект не в непонятные символы,
    // а в нечто человекочетаемое и симпатичное глазу
    public String toString() { 
        return name; 
    } 
    

    }

    On tape

    Cat cat2 = new Cat("Barsik", 3, 3, 3);
    System.out.println(cat2);

    He'll be out. Barsik


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2