0

Is it possible to cast a variable using a different variable instead of using the name of a class?

Here is functioning code:

Object five = new Integer(5);
int six = (Integer) five + 1;

I would love to replace that second line with

int six = five + 1;

but I can't, so could i do something like one of these alternatives:

int six = (foo) five + 1;
int six = foo(five) + 1;
int six = foo.cast(five) + 1;

??

why i want to do this
I have a Map with keys of a custom enum, and with values of type String, Integer, Double, etc.

I would like to perform class-specific operations on map entry values without hard-coding the cast class.

example

enum keyEnum { height, color; }

public static void main(String[] args) {
    Map<keyEnum, Object> map= new HashMap();
    String value1 = "red";
    Double value2 = 3.2;
    map.put(keyEnum.color, value1);
    map.put(keyEnum.height, value2);

    double x = (Double) map.get(keyEnum.height) + 10.5;
}

I would really like to avoid having to hard-code that (Double) in the last line. Have found no solutions so far, only indications that it might not be possible.

I'm using this setup for a program that needs to write and read and write large csv files. I would like a way for Java to automatically cast variables appropriately so I don't have to remember or code the class of every column type.

I have an enum of all the column titles which i use as keys for maps that store the column's variables. This is to avoid hard-coding the array index for each column (after row.split(",")) which is a maintenance nightmare. I'm open to better approaches to this

2
  • Java is a strongly typed language. Commented Jan 20, 2014 at 21:58
  • why doesn't java allow int six = five.getClass().cast(five) + 1;? Commented Jan 20, 2014 at 23:07

2 Answers 2

2

You are not using Java as it was intended so it's going to be slow, unsafe and ugly. What you should do is

class MyType { double height; String color; }

public static void main(String[] args) {
    MyType mt = new MyType();
    mt.color = "red";
    mt.height = 3.2;

    double x = mt.height;

    // to iterate over the fields
    for(Field field: MyType.class.getDeclaredFields()) {
        System.out.println(field.getName() + "= "+ field.get(mt));
    }
}

This will be much safer with compile time checks, use less code and it will use far less memory and CPU.

Sign up to request clarification or add additional context in comments.

4 Comments

but i want to use enum to specify column order in only one spot in the code. and to loop through all the columns (without building extra structures and then having to change each of their internal column orders and make sure they all match). i want the code to read csv file and figure out which column goes with which variable. i want to be able to add/remove/move columns by changing code in one spot, not having to change code for each header output, data output, and data input. for several file structures, each. using enum allows this.
@stuart It does but it will make all your other code painful. I would use reflections if you want to iterate over the fields. My MyType class is defined in one place too.
in order to detect column name/position in csv file, i think with MyType i need two extra index variables per column variable (input and output for when i need to change ordering). i have 55 columns. and then i'll change the name of the column, but forget to change the name of the column's index variables, and get confused. with enum, i can add int in_i and int out_i index variables that will be unique per enum value. can you help me understand how enum could be more painful? so far is considerably less painful but i am new to this.
@stuart You only need one map of field name to column id. You don't have to store this in every record/row.
1

Store the classtype in a variable, and leverage the cast method.

Class<T> cls and cls.cast()

1 Comment

for this to work, cls needs to be defined already knowing the cast-to class. but i want each column variable to access cls that will allow it to be casted without knowing the column class in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.