How to create dynamic variables in Java?
Asked Answered
J

3

7

For example, in Haxe I can create strictly typed variables: var a:Float = 1.1; or var b:String = "hello" and also dynamic, if needed:

var d:Dynamic = true; d = 22; d = "hi";

How do I create this kind of variables in Java?

Jug answered 9/3, 2017 at 16:7 Comment(1)
Object I think is what you are looking for.Multiform
E
12

You can use Object

Object d = true; 
d = 22; 
d = "hi";

and you can use instanceof operator to check which type of data d is holding

Object d = true; 
System.out.println(d instanceof Boolean); // true
d = 22; 
d = "hi";       
System.out.println(d instanceof Integer); // false
System.out.println(d instanceof String);  // true

The Type Comparison Operator instanceof

Eliott answered 9/3, 2017 at 16:9 Comment(4)
Use of instanceof is a code smell, usually wafting from a flawed type analysis, and tends to create messy, bug-ridden code unless you know what you're doing.Partlet
@Lew, you're 100% correct. But the posted anwser replays the question.Sessions
I would expect a dynamic variable to resolve usage at runtime, like C#'s dynamic. That is, d.foo() would check at runtime that d has a foo method.Timorous
There is a difference between run-time typing and dynamic typing. The Object strategy described here creates more problems than it solves. You can do what's necessary using generics and run-time type tokens without sacrificing type orientation. This is not a good answer; in fact quite the opposite.Partlet
I
2

You could look at mixing in the groovy language which runs on the JVM. This has type inferrance

Iridotomy answered 9/3, 2017 at 22:10 Comment(1)
Java has type inference. Type inference is not dynamic typing. Type inference is a strategy for strong typing.Partlet
P
1

Dynamic typing is evil so Java eschewed it. Like Swift and C#, Java is strongly typed, which leads to safer and cleaner code. So give in to the Dark Side and put aside your rebel ways. Embrace the Force of type-oriented programming. You'll be the better for it.

Partlet answered 9/3, 2017 at 16:27 Comment(7)
C# has dynamic for dynamic typing. It's useful in some specific scenarios like JSON and interacting with COM.Timorous
I agree, I avoid dynamic everywhere possible, I love strongly typed approach. I just needed to find out about it for some exceptional cases.Jug
Java handles run-time typing for JSON just fine. You don't need dynamic typing at all for that, @TimorousPartlet
@Jug please describe an "exceptional" case where you need dynamic typing and I'll show you how to solve it in Java with strong typing.Partlet
@LewBloch, it's irrelevant now, I asked a question 3 years ago today 🤷Jug
Truth remains truth even years later.Partlet
I use an autogenerated JSON parser which aliases various JSON defs into a single type. C#'s dynamic beautifully allows me to locally bypass the type system to deal with this. There are other solutions, but all of them are uglier and hackier. I wondered how would I have fixed this in Java, found this answer... and now I'm so glad I'm doing this in C#! Funny how limitations can be praised as features. That won't help Java improve.Cortege

© 2022 - 2024 — McMap. All rights reserved.