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

Asked

Viewed 1,721 times

14

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

double x;

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

int x;

That’s possible?

  • 1

    Casting is possible. But the variable will contain double and with its value as declared, and there may also be data loss.

  • 1

    Here is strong typing bro! No these parrots of dynamic little tongues (sorry, I couldn’t resist :P).

  • Any answers solved the problem? You know you can accept one, right?

5 answers

10

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 this makes sense. Anyway the data type will be more specialized than the type declared in the variable.

The definition of a variable is a reserve of memory space and the establishment of a contract that there will always be a type of information there. So you can not change your type. At least not in Java and other spoken languages static (the correct term). If it were a class it would be possible to create an object of a compatible type (derivative) because the variable is only a reference to the real object that has a specific type, this is possible because of the indirect.

Some "solutions" were posted and apparently you found a "magic" solution. I recommend not doing any of them, not even the one 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 real problem you want to solve.

It seems to me that you are trying to apply a solution to a problem that does not require it. Define your problem well and look for the best solution for it. We can help with this if you post a question with more details of the real problem.

  • 5

    +1 By: there is no reason to do this.

7

The short answer (as already stated) is nay.

To understand the reason note that there are Strongly typed languages, Weakly typed languages and Languages not typed

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

  • Weakly typed languages (Dynamically typed) are those where during the execution of the program they can change the type of data contained in a variable. Allow the programmer not to have to do type conversions (cast). PHP, Javascript, Ruby and Python are examples of this kind.

  • Languages not typed are those where there is only one generic type or even no data type. Among these are the Assembly, BCPL, Perl and some versions of Forth.

Source: http://pt.wikipedia.org/wiki/Linguagem_tipada

If the reason is to migrate software from one language to another, you should rewrite the software according to the type of language, so you can hardly migrate from one Strongly typed language to another Strongly typed language and it will be practically impossible to migrate from a language Strongly typed language for Weakly typed language without rewriting the entire software.

5

No, java variables are immutable, but, just out of curiosity, you can give a "steal", 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 object, so if you want to make a suruba with java this is the way of darkness

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

5

YES

But it will be very laborious. This is a job that involves a lot of black magic and evil sorcery. Some dark entities will have to be summoned to the world of the living.

What you need to do is something similar to Lombok project does it underneath the covers. He does everything I describe below and that’s exactly why he has superpowers that we mere mortals don’t have.

Basically what you will 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’re written in java and the annotation processor runs on the same JVM as the compiler. To hijack the compiler, you have to get access to the syntactic trees of the code and go out poking around them at will. Since the compiler API is internal and should not be used publicly, to get access to it you will need to use any type of cast, Reflection, rule-breaking, trickery, and scamming you need. Even bytecode manipulation is valid in compiler classes.

After sequestering the compiler and having full command over the syntactic trees, all you have to do is tinker with these trees to accept that you can redeclare 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... because even you want to do it?

  • 2

    Kkkkkk just wanted to take out the ". 0" that appears when I ask to display the number"1" as double... Appears like this: 1.0... But I already solved this by assigning to a String kkk Valeu

  • 1

    @Victor I couldn’t resist, the dark side of the force is stronger Luke. That’s your answer to me, man.

  • lkkkkkkkkkkkkkkk

3

Try it like this:

double x;

int newX = (int) x;

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

  • No master, the question was how to change the type of the variable, keeping the same variable.

  • 1

    Well, then I see no point in him wanting to do it...

  • This is the point, this happens when we migrate from language, I did the same questions when I migrated from python -> java, where the type mattered little.

  • This was the only answer that captured the need for AP, which, like most software needs, was simpler than most guessed. We programmers love to complicate. + 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.