Tutorial 9 - Byte, Short, Long & Width
Small data types takes less space and can be easily accessed. A byte occupies 8 bits. A bit is not directly represented in a primitive type – We have a Boolean which we will discuss in future video. So a Byte occupies 8 bits. We say that byte has a width of 8 . A short can store large range of numbers and occupies 16 bits and has a width of 16. An int has a much larger range as we know and occupies 32 bits and has a width of 32. The point here is that each primitive type occupies different amount of memory.We can see that that an int needs four times the amount of space,than a byte does Program : package com.company; public class Main { public static void main(String[] args) { int x = 10000; int myMinInt = Integer.MIN_VALUE; int myMaxInt= Integer.MAX_VALUE; System.out.println("Integer minimum value="+myMinInt); System.out.println("Integer maximum value"+myMaxInt); System.out.println("Busted minimum value"+(myMinInt-1)); System.out.println("Busted maximum value"+(myMaxInt+1)); int test = 2147483647; byte myMinByte = Byte.MIN_VALUE; byte myMaxByte= Byte.MAX_VALUE; System.out.println("Byte minimum value="+myMinByte); System.out.println("Byte maximum value"+myMaxByte); short myMinShort = Short.MIN_VALUE; short myMaxShort= Short.MAX_VALUE; System.out.println("Short minimum value="+myMinShort); System.out.println("Short maximum value"+myMaxShort); long y = 100L; long myMinLong = Long.MIN_VALUE; long myMaxLong= Long.MAX_VALUE; System.out.println("Long minimum value="+myMinLong); System.out.println("Long maximum value"+myMaxLong); long z =2147483647678L; System.out.println(z); } } output : Integer minimum value=-2147483648 Integer maximum value2147483647 Busted minimum value2147483647 Busted maximum value-2147483648 Byte minimum value=-128 Byte maximum value127 Short minimum value=-32768 Short maximum value32767 Long minimum value=-9223372036854775808 Long maximum value9223372036854775807 2147483647678
Small data types takes less space and can be easily accessed. A byte occupies 8 bits. A bit is not directly represented in a primitive type – We have a Boolean which we will discuss in future video. So a Byte occupies 8 bits. We say that byte has a width of 8 . A short can store large range of numbers and occupies 16 bits and has a width of 16. An int has a much larger range as we know and occupies 32 bits and has a width of 32. The point here is that each primitive type occupies different amount of memory.We can see that that an int needs four times the amount of space,than a byte does Program : package com.company; public class Main { public static void main(String[] args) { int x = 10000; int myMinInt = Integer.MIN_VALUE; int myMaxInt= Integer.MAX_VALUE; System.out.println("Integer minimum value="+myMinInt); System.out.println("Integer maximum value"+myMaxInt); System.out.println("Busted minimum value"+(myMinInt-1)); System.out.println("Busted maximum value"+(myMaxInt+1)); int test = 2147483647; byte myMinByte = Byte.MIN_VALUE; byte myMaxByte= Byte.MAX_VALUE; System.out.println("Byte minimum value="+myMinByte); System.out.println("Byte maximum value"+myMaxByte); short myMinShort = Short.MIN_VALUE; short myMaxShort= Short.MAX_VALUE; System.out.println("Short minimum value="+myMinShort); System.out.println("Short maximum value"+myMaxShort); long y = 100L; long myMinLong = Long.MIN_VALUE; long myMaxLong= Long.MAX_VALUE; System.out.println("Long minimum value="+myMinLong); System.out.println("Long maximum value"+myMaxLong); long z =2147483647678L; System.out.println(z); } } output : Integer minimum value=-2147483648 Integer maximum value2147483647 Busted minimum value2147483647 Busted maximum value-2147483648 Byte minimum value=-128 Byte maximum value127 Short minimum value=-32768 Short maximum value32767 Long minimum value=-9223372036854775808 Long maximum value9223372036854775807 2147483647678