Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use String/int/long etc. in one class

I want to use:

user_update("lastlogin", INT);
user_update("nick", STRING);
user_update("money" DOUBLE);

but in one class:

public void update_user(String what, String value){
[...]
}

So here is my question : How can I handle INT, STRING, DOUBLE in class?

public void update_user(String what, String/int/double value){
[...]
}

2 Answers

You may as well use a generic method

public <T> void update_user(String what, T value) {}

This may be useful if you are intended to add more types used by the method in the future. Using generics may save you from writing too much boilerplate code.

like image 70
xuesheng Save Ukraine Avatar answered Mar 18 '26 07:03

xuesheng Save Ukraine


You can use Object.

public void update_user(String what, Object value){}

Take a look here

java.lang.Object


OR

you can implement 3 methods:

public void update_user(String what, String value){}

public void update_user(String what, int value){}

public void update_user(String what, double value){}
like image 38
ruhungry Avatar answered Mar 18 '26 07:03

ruhungry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!