Java Datatypes

  • + 0 comments

    Kucao, and all

    Kucao's solution is NOT exactly correct. What if the input is number = 10. Number 10 can be fit into ANY primitive type. So, every single "if" in Kucao's code will be duplicated in the output.

    Better to use if-else if construct. Like this

    long x=sc.nextLong();
                    System.out.println(x+" can be fitted in:");
                    if(x>=-128 && x<=127)System.out.println("* byte\n* short\n* int\n* long");
                    else if( x>=((-1)*(Math.pow(2,15))) && x<=((Math.pow(2,15)-1)) )System.out.println("* short\n* int\n* long");
                    else if( x>=((-1)*(Math.pow(2,31))) && x<=((Math.pow(2,31)-1)) )System.out.println("* int\n* long");
                    else if( x>=((-1)*(Math.pow(2,63))) && x<=((Math.pow(2,63)-1)) )System.out.println("* long");