-1

when i print the value of sentiment it gives me this value why? Sentiment@1540e19d Sentiment@677327b6

public class Sentiment extends  Sample{
    public String value;

    public Sentiment(String val){

      value = val;

    }
}

in main class
 Sentiment positive = new Sentiment("+");
        Sentiment negative = new Sentiment("-");

        System.out.println(positive);
        System.out.println(negative);
Christian
  • 259
  • 3
  • 7
  • 21
  • 1
    override the toString method in your Sentiment class and write it to output what youd like it to – JRowan Nov 24 '15 at 02:58

3 Answers3

1

Because the default toString() behaviour is to print class name and hashcode for the class.

Override toString() in your class to get better print friendly results

ex:

public class Sentiment extends  Sample{
    public String value;

    public Sentiment(String val){

      value = val;

    }
      @Override
     public String toString(){
     return "Value =  " +value;
    }
}
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
1

you should either do

System.out.println(positive.value);

or

define toString() in Sentiment class

public class Sentiment extends  Sample{
    public String value;

    public Sentiment(String val){

      value = val;

    }

public String toString(){

return ""+value;

}
}
vlatkozelka
  • 909
  • 1
  • 12
  • 27
0

When you try to write to stdout, Java will automatically call the toString() method. When an object doesn't explicitly have a toString() method, it reverts to the Object class which has the method.

Object.java:toString()

 getClass().getName() + '@' + Integer.toHexString(hashCode())
Sep
  • 43
  • 1
  • 9