0

When we're using python shell, we can use the following statements if we want know data type of Data type of the added variables.

>>> y=5
>>> x=3.56
>>> type(x+y)
<class 'float'>

Here, my question is how I do this in Java? I'm not asking about statements given below.

String str = "test";
String type = str.getClass().getName(); 

It gives us only the data type that we already declared. But I'm asking that the data type of Data type of the added variables.

Panduka Nandara
  • 504
  • 6
  • 14

2 Answers2

3

Java is statically typed language and Python is dynamically typed language.
This means that in Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.That’s what it means to say that Java is a statically typed language.

In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That’s what it means to say that Python is a dynamically typed language.

DevJava
  • 396
  • 1
  • 7
1

Maybe something like this?

((Object)(2.4 + 2)).getClass().getName();

But in Java you have strictly specified type of operation.

5.6.2 Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value of a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

Krzysztof Mazur
  • 568
  • 2
  • 13