Can I return the default object if no methods are called?

As an example, I want to mimic the functionality of a String object:

String mystring = new String ( "Hi there." );
System.out.println(mystring); // prints "Hi there." without calling any methods on String
                              // (that is what I want with my object)
System.out.println(mystring.toUpperCase()); // prints "HI THERE."

      

Starting from this:

class MyObject
{
    private Int value ;

    public MyObject ( Int value )
    {
        this.value = value ;
    }
    public Int getValue ( )
    {
        return this.value ;
    }
    public Int multiplyBy ( Int multiplier )
    {
        return this.value * multiplier ;
    }
}

      

... How (or can I) do something like this:

MyObject myobject = new MyObject ( 6 ) ;
System.out.println( myobject ) ; // want to print myobject.value (6)
System.out.println( myobject.multiplyBy ( 2 ) ) ; // print 12

      

I'm new to Java and realize that I probably missed some fundamental concept, but any feedback would be appreciated. Thank.

Edit: The answers about overriding the toString method were helpful, but that's not exactly what I had in mind, here's the best example of what I want:

Double mydouble = new Double ( 20.1 ) ;
System.out.println ( mydouble + 10.1 ) ; // prints 30.2

      

How do I do this with my own object (if I want the default to be double)?

Thanks again.

Final Edit (hopefully): Your answers were enough to lead me to learn more about primitive packers and auto-update. I realize that there is no way to do what I want (which at this point I would call autounboxing my object as double), or at least I'll save this for another question. Greetings.

+3


source to share


4 answers


Yes, you can do this: all you have to do is override toString

:

public String toString() {
    return ""+this.value;
}

      



This works because it println

calls toString

on printable objects.

+7


source


override toString()method

in class MyObject .



private int value ;
public String toString() {
  return Integer.toString(this.value);
 }

      

+3


source


Java will always execute "internal" statements first. So, let's say we have a Double:

Double db = new Double(20.1);

Then you want to do this:

System.out.println(db + 10)

Then the compiler will execute that part first db + 10

, which with auto boxing / unboxing becomes db.doubleValue() + 10

, which evaluates to 30.1

. The outter ( System.out.println(x)

) function is then evaluated with x=30

.

When you have the following code System.out.println(db + "hi")

, the compiler sees that you are adding something ( db

) to the string "hi"

and hence calls .toString()

to db

. and then adds that (line) to "hi" which will give you the final line:"20.1hi"

Hope this helps you understand what's going on behind the scenes.

+1


source


Just make the instance method return a new instance:

public MyObject multiplyBy(int multipler)
{
    return new MyObject(value * multipler);
}

      

(oh and it will int

, not int

)

and implement .toString ():

@Override
public String toString()
{
    return Integer.toString(value);
}

      

0


source







All Articles