I am new to Java so I have a problem with hex numbers usage in conditionals and its sizes. There is some issue about Scanner class too. I have searched java documents for primitive data types.
The program is about; take a number and look in which data type it could be written and print the appropriate data type.
import java.util.*;
import java.io.*;
import java.lang.Integer;
public class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
long t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=0x81 || x<=0x7f)System.out.println("* byte");
if(x<=0x7fff || x>=0x8001)System.out.println("* short");
if(x<=0x7fffffff || x>=0x80000001)System.out.println("* int");
//if(x<= (0x7fffffffffffffff)|| x>=
//(0x8000000000000001))System.out.println("*long");
System.out.printf("\n\n%x\n\n",x);
System.out.println();
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
}
Questions:
I get an error message for commented "if()" conditional "too large int :0x7fffffffffffffff " why is that so?
I run the program by commenting some "if()" and give it "10" to search possible data types but this time it prints just only "int" datatype.
If I give it "0x7b" as input it carries out to "catch()" section. Why?
Could you please explain it ? Thanks.