Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:10:53+00:00 2026-06-17T13:10:53+00:00

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

  • 0

As an example, I want to mimic the functionality of the 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 with 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 quite new to Java and realize that I’m probably missing some fundamental concept, but any feedback would be appreciated. Thank you.

Edit:
The responses about overriding the toString method were helpful, but it’s not quite doing what I had in mind, here’s a better example of what I want:

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

How would I do this with my own object (assuming I want the default value to be a double)?

Thanks again.

Final Edit (hopefully):
Your answers were enough to lead me to learn more about Primitive Wrappers and Autounboxing. My understanding is that there’s no way to do what I want (which at this point, I would describe as autounboxing my object as a double), or at least I’ll save that for another question. Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:10:54+00:00Added an answer on June 17, 2026 at 1:10 pm

    Yes, you can do it: all you need to do is overriding toString:

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

    This works because println calls toString on objects being printed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example: I want to know if there are images in a directory (eg.
Is there any javascript function that can encrypt data: For example i want to
For example I want to use custom logger: logger = require('basic-logger'), logger.setLevel('info') var customConfig
For example: I want: if file1 exists: CLEAN_SRC = *.h file3 else CLEAN_SRC =
for example : I want to remove all highlighted tags alt text http://shup.com/Shup/299976/110220132930-My-Desktop.png
Example: Suppose in the following example I want to match strings that do not
Here's a contrived example: I want to divide two numbers and I need to
I want to round a value in Twig. Example: I want to display 80.5555
Possible Duplicate: Best way to programmatically detect iPad/iPhone hardware For example I want my
I have created a separate class (let's call it class2.cs for example) and want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.