Is it possible to change the type of the variable in Java?

12

Is it possible to change the type of my variable in Java? For example, I created a variable x , being a Double :

double x;

I want to continue using my variable x but it is now a int :

int x;

Is this possible?

    
asked by anonymous 19.02.2015 / 22:02

5 answers

8

It is not possible and there is no reason to do this.

In theory it would be possible to create a type (or use ( object ) to store any type but rarely does this make sense.) In any case, the type of data will be more specialized than the type declared in the variable.

The definition of a variable is a reservation of memory space and the establishment of a contract that there will always be some kind of information there. So you can not change your type. At least not in Java and other so-called static (the correct term).

Some "solutions" have been posted and apparently you have found a "magic" solution. I recommend doing none of them, not even what you seem to have found as a solution. Hiding the real problem is not a solution. Look for a solution to your problem. If so, post another question with the actual problem you want to resolve.

It seems to me that you are trying to apply a solution to a problem that does not require it. Set your problem well and look for the best solution for it. We can help with this by posting a more detailed question on the actual problem.

    
19.02.2015 / 22:23
5

No, the variables in java are immutable, however, just out of curiosity , you can by stealing, changing the object but keeping the variable.

public static void main(String[] args) {
        Object a = 1;
        a =  new StringBuilder("ola");
        a = 666;
        System.out.println(a);

}

Well, almost everything in java is an object, so if you want to do a java run through this is the path of darkness

  

I do not recommend using this type of technique, however you are changing the object and not the type, the problem is that the type is controlled by the object, not deep?

    
19.02.2015 / 22:43
5

The short answer (as already said) is not .

To understand the reason, note that there are Strongly typed languages , Loosely typed and Untyped languages

  • Strongly typed languages are languages in which the type declaration is mandatory. All variables have a specific type that has to be explicit. Java , C# , Fortran and Cobol are examples of this type.

  • Loosely typed languages are those where you can change the type of data contained in a variable during program execution. They allow the programmer not to do cast type conversions. PHP , Javascript , Ruby and Python are examples of this type.

  • Untyped languages are those where there is only one generic type or no data type. Among these are Assembly , BCPL , Perl and some versions of Forth .

Source: link

If the reason is to migrate a software from one language to another, you should rewrite the software according to the type of language, so you will not be able to migrate from a Strongly typed language to another Language strongly typed and it will practically be impossible to migrate from a strongly typed language to Loosely typed language without rewriting the entire software.

    
19.02.2015 / 23:59
5

YES

But it will be very laborious. This is a work that involves a lot of black magic and evil witchcraft. Some entities of darkness will have to be invoked into the world of the living.

What you need to do is something like the lombok project underneath the cloths. He does everything I describe below and that is exactly why he has superpowers that we mere mortals do not have.

Basically what you're going to have to do is create an annotation processor that hijacks the compiler. This is possible because in the case of javac and the eclipse compiler, they are written in java and the annotation processor runs in the same JVM as the compiler. To hijack the compiler, you have to get access to the syntax trees from the code and come out by digging and tinkering at will. Because the compiler API is internal and should not be used publicly, in order to gain access to it you will need to use all sorts of cast, reflection, rule violation, trickery, and gambiarra as needed. It's worth handling bytecode in compiler classes.

After hijacking the compiler and having full domain over syntax trees, all you have to do is tinker with these trees to accept that you can redeclarate variables in the same scope and using a different type. After all, at this point, the compiler is already yours and you do whatever you want with it.

Good luck!

And if you want to follow this path, then welcome to the dark side of the force!

But ... Why do you even want to do this?

    
20.02.2015 / 02:56
3

Try this:

double x;

int newX = (int) x;

Being newX the conversion result and (int) x to double being converted ...

    
19.02.2015 / 22:14