-3

Rather than implicit conversion, is there a way to force the java compiler to issue an error when variables types do not match. For example:

int intValue=3;
double dblValue = 2.2;
double result;
result = 1/intValue*(intValue-dblValue);

In the example above java will implicitly convert (1/intValue) to an integer with the value 0.

What I want is for the compiler to issue an error when variable types don't match rather than implicitly converting.

Thanks in advance

Edit1: The equation above is just an example. I do know how to fix this issue by using either one of the solutions below:

result = 1.0/intValue*(intValue-dblValue);

or

result = 1/(double)intValue*((double)intValue-dblValue);

but thats not what I'm after. Thanks

Edit2: I was referring to type conversion not type casting. I'm looking for the compiler to enforce explicit type conversions.

I'm looking for something like this: https://support.microsoft.com/en-us/help/311329/option-explicit-and-option-strict-in-visual-basic-.net-and-in-visual-basic

pedrumj
  • 163
  • 2
  • 12
  • 1
    Well, this is perfectly legal Java code, so there's no reason to have it produce an error in this case. – Joe C Jan 28 '17 at 21:27
  • 5
    "implicitly type cast (1/intValue) to an integer with the value 0" - what? There's no casting involved there. It doesn't produce a double and then cast it to int or anything like that; the division operator produces an int directly. – user2357112 Jan 28 '17 at 21:28
  • No. The rules of Java are already defined, and the construct you mention is not an error. It is not even a typecast. It is an implicit conversion in the cases involving ints and doubles, and nothing at all in the case involving only ints. Unclear what you're asking. – user207421 Jan 28 '17 at 23:20
  • @EJP, What I meant to say was type conversion not type casting. I've updated the post. – pedrumj Jan 29 '17 at 00:30
  • But there is no type conversion in the `1/intValue` expression. And the widening primitive conversion loses no information. What possible complaint could the compiler deliver, even if you could change its behavior? – Lew Bloch Jan 29 '17 at 22:38

2 Answers2

1

How about, rather than dividing two integers, you divide a double and an integer?

result = 1.0/intValue*(intValue-dblValue);
Joe C
  • 15,324
  • 8
  • 38
  • 50
0

"Rather than implicit typecasting, is there a way to force the java compiler to issue an error when variables types do not match." It does issue an error when types don't match, or there'll be a loss of information. As pointed out in a comment above, there isn't any "type casting", or narrowing conversion to be specific, in your example. The compiler is doing exactly what it is supposed to, and no, you can't change that.

"What I want is for the compiler to ..." Sadly, what you want isn't considered by the compiler. It will only do what is specified for compliance with the Java language by the Java Language Specification and compatibility requirements.

In your case you specified a perfectly legal int operation requiring neither a widening nor narrowing conversion, so even if the compiler did care about what you want, there'd be nothing for it to complain about.

You can use lint tools like Findbugs and CheckStyle to preprocess your source for all kinds of bug risks, even custom rules. They might detect the use case of a programmer mistakenly ignoring associativity or type risks in mixed-type expressions. They're excellent tools; check them out.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10