0
Scanner num=new Scanner(System.in);
  System.out.println("Enter  number in range from 1 to 20");
  int n=num.nextInt();
    int product=1;
    for(int i=1;i<=n;i++){
        product*=i;
        if(product>Integer.MAX_VALUE || product<Integer.MIN_VALUE){
         System.err.println("Out od Integer boundary");
        }
        else System.out.println("Product of numbers between 1 and "+n+" is "+product);
        }

        }
      }    

I'm working on same basic tasks and this particular one is: Compute the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14. Write down the product obtained and decide if the results are correct. and then Hints: Product of 1 to 13 (=6227020800) is outside the range of int [-2147483648, 2147483647], but within the range of long. Take note that computer programs may not produce the correct answer even though everything seems correct! So if i understand correct Java will automatically transcend int value into long value, but i was thinking to make program which will not allow that. Is is possible? Also feel free to explain anything what is incorrect in code or my thinking.Thanks. p.s Sorry for bad English.

Yurodivi
  • 53
  • 1
  • 7
  • 3
    No, Java will not magically turn an `int` into a `long`, it'll just overflow the `int`. – azurefrog Feb 15 '17 at 04:37
  • 1
    Change `int product=1;` to `long product=1;`, and you'll get your error. Might as well break on the first one too. – Elliott Frisch Feb 15 '17 at 04:38
  • http://stackoverflow.com/a/1657868/655424 will help in detecting where overflow is about to happen. This question is close to being a duplicate of that one – Atreys Feb 15 '17 at 04:42
  • So the best i can get is Error message but Java will make it into long no matter what. so it is not possible to stop java from doing that? Ok. thx. – Yurodivi Feb 15 '17 at 04:46
  • Java only automatically widens an `int` to a `long` if it's within an expression that involves a `long` (either a `long` literal, or a variable declared as `long`). – Kevin Feb 15 '17 at 04:46
  • 1
    In your program, Java will not make _anything_ into a `long`. – ajb Feb 15 '17 at 04:47
  • @ajb Indeed. Which seems to be the point of the exercise; to get the correct product, you need to declare `product` as a `long`. Having it as an `int` will give a different (incorrect) result. – Kevin Feb 15 '17 at 04:50
  • yes @ajb cleared this,thx. – Yurodivi Feb 15 '17 at 04:53

2 Answers2

0

Java will automatically convert int to long, but only in certain situations. Specifically, if something is declared as a long, and you give it an int, the program is legal and the int will be converted to a long (the numeric value will be the same). Examples:

int a = 100;
long b = a;  // legal
int c;
b = c;  // legal

public static void doSomethingWithLong(long x) { ... }

int d = 100;
doSomethingWithLong(d);  // legal

Another case where Java promotes an int to a long is if you have an arithmetic expression involving a long and an int:

long a = 10000000000L;
int b = 3;
long c = a + b;

Here b is promoted to a long inside the expression so that it can add a + b to it. But since the numeric value is the same, the fact that it's promoted is probably relevant only to implementors.

But that's all Java does. If you declare a variable or parameter to be an int, the variable does not get redeclared as a long just because you try to put too big a value into it. It's still an int. If you try to compute a value that is too big, it will wrap and you will get the wrong answer.

(P.S. This automatic conversion only works when converting to a "wider" type. It doesn't work both ways:

long a = 10;
int b = a;  // error

This is an error even though the value of a would actually fit in an int. Java won't automatically convert a long to an int, without a cast.)

ajb
  • 31,309
  • 3
  • 58
  • 84
  • It might be worth noting what happens if you have an expression of type `long` outside of the range of an `int` and attempt to assign it to a variable declared `int`. – Kevin Feb 15 '17 at 04:51
0

If I understand the question correctly, the simple solution would be to use Math.multiplyExact (which appeared in Java 8):

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner num = new Scanner(System.in);
        System.out.println("Enter  number in range from 1 to 20");
        int n = num.nextInt();
        int product = 1;
        try {
            for (int i = 1; i <= n; i++) {
                product = Math.multiplyExact(product, i);
            }
            System.out.println("Product of numbers between 1 and " + n + " is " + product);
        } catch (ArithmeticException e) {
            System.err.println("Out of Integer boundary");
        }
    }
}

The multiplyExact method ensures that your multiplication result does not overflow. If it does, an AriphmeticException is thrown which could be catched and handled.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334