简体   繁体   中英

Automatic cast from object to primitive type

I have been looking for a transparent way to cast an object to a primitive type, something like:

class Example {
    double number;

    public Example(double number) {
        this.number = number;
    }

    // this or something similar to this
    public toDouble() {
        return number;
    }

    ...
}

Example ex = new Example(18.0);
double number = ex;

After a few searches i am almost sure that it does not exists, but if there would be a big help.

Update 1

Excuse me if the questions was not very clear.

I am developing a code editor for an internal tool: the idea is write a program with a reduced set of java instructions, translate to java and compile.

I want avoid that the editor have to make a lot of times the same casts or call same calls, and the exprexions sometimes will be very complex to translate.

A better example could be:

the simplified code

Example ex = new Example(18.0);
Example ex2 = new Example(23.0);

Example mean = Mean(ex + 1.0, ex2, 3.0);

(i would prefer avoid Example mean = Mean(ex.toDouble() + 1.0, ex2.toDouble(), 3.0); )

and the function in java

double Mean(double... numbers)

All the primitive types in java have their wrappers: Integer , Double etc.

According to Java tutorials:

Converting an object of a wrapper type ( Integer ) to its corresponding primitive ( int ) value is called unboxing.

And this is done pretty automatically, so I am not sure which object else would you like to "cast" to primitive? You can use a method like your toDouble() , which will return primitive, but it won't be casting at all.

You can use the wrapper of double, Double .

Double d = new Double(10.3);
double d1 = d; // This will be auto(un)boxing and not casting

transparent way to cast an object to a primitive type

double number = ex; this is illegal as ex is of type Example and number is of the primitive type double . This really doesn't make sense.

You either need to use toDouble() , or completely remove the Example class and use the wrapper class Double as mentioned above.

You cannot cast, and little confusing what you are trying to do.

If you need double then , call toDouble()

double number = ex.toDouble();

or even, as some one commented, use the Double class constructor Double(double value)

  Double wrapper = new Double(18.0);  //class
  double primitiveDouble =wrapper;    // primitive

There is no analogy to C#`s implicit modifier. In general there are less syntax sugar in Java then in .NET. I dont this this is a problem. You can look inside sources of Integer, Double, Float etc. All they have methods like floatValue(), longValue() and so on. Making casts explicit improves code readability. While implicit casts, like available in C#, are very hard to understand. You can make your own method yourTypeValue(). This is a sort of Java convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM