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?

1
  • 1
    Object I think is what you are looking for. Commented Mar 9, 2017 at 16:09

3 Answers 3

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

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

4 Comments

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.
@Lew, you're 100% correct. But the posted anwser replays the question.
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.
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.
2

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

1 Comment

Java has type inference. Type inference is not dynamic typing. Type inference is a strategy for strong typing.
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.

7 Comments

C# has dynamic for dynamic typing. It's useful in some specific scenarios like JSON and interacting with COM.
I agree, I avoid dynamic everywhere possible, I love strongly typed approach. I just needed to find out about it for some exceptional cases.
Java handles run-time typing for JSON just fine. You don't need dynamic typing at all for that, @chris
@shal please describe an "exceptional" case where you need dynamic typing and I'll show you how to solve it in Java with strong typing.
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.
|

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.