Java Datatypes

Sort by

recency

|

1610 Discussions

|

  • + 7 comments

    This is a great exercise for understanding how data types work in Java, especially when it comes to choosing the right type for storing numeric values. Cricplus Pro

  • + 0 comments
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            try (Scanner scanner = new Scanner(System.in)) {
                int t = scanner.nextInt();
                while (t-- > 0) {
                    BigInteger n = scanner.nextBigInteger();
                    if (n.bitLength() < Long.SIZE) {
                        System.out.println(n + " can be fitted in:");
                    } else {
                        System.out.println(n + " can't be fitted anywhere.");
                        continue;
                    }
                    if (n.bitLength() < Byte.SIZE) {
                        System.out.println("* byte");
                    }
                    if (n.bitLength() < Short.SIZE) {
                        System.out.println("* short");
                    }
                    if (n.bitLength() < Integer.SIZE) {
                        System.out.println("* int");
                    }
                    if (n.bitLength() < Long.SIZE) {
                        System.out.println("* long");
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
  • + 1 comment

    import java.util.; import java.io.;

    class Solution{ public static void main(String []argh) {

        Scanner sc = new Scanner(System.in);
        int 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>=-128 && x<=127)System.out.println("* byte");
                //Complete the code
                if(x>=-32768 && x<=32767)System.out.println("* short");
                if(x>=-2147483648 && x<=2147483647)System.out.println("* int");
                if(x>=-9223372036854775808L && x<=9223372036854775807L)System.out.println("* long");
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            } 
        }
    }
    

    }

  • + 0 comments

    The instructions for input and output are also practical, showing how to handle multiple test cases and check each primitive’s capacity. Andhra 365 Login

  • [deleted]
    + 1 comment

    import java.util.; import java.io.;

    class Solution{ public static void main(String []argh) {

        Scanner sc = new Scanner(System.in);
        int 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>=-128 && x<=127)System.out.println("* byte");
                //Complete the code
                if((short) x == x)System.out.println("* short");
                if((int) x == x)System.out.println("* int");
                System.out.println("* long");
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }

    My approach is simple. I tried to squeeze a number into smaller datatype. So, if the number stays same it means it fits in that datatype if does not fits it means the number is changed.