Skip to content

Instantly share code, notes, and snippets.

@slava-konashkov
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save slava-konashkov/9194976 to your computer and use it in GitHub Desktop.

Select an option

Save slava-konashkov/9194976 to your computer and use it in GitHub Desktop.
Get SizeOf
package methods;
public class GetSize {
public static void main(String[] args) {
Byte[] b = {1};
Short[] s = {2, 2};
Integer[] i = {3, 3, 3};
Long[] l = {4l, 4l, 4l, 4l};
Float[] f = {5f, 5f, 5f, 5f, 5f};
Double[] d = {6d, 6d, 6d, 6d, 6d, 6d};
System.out.println(getArrSize(b));
System.out.println(getArrSize(s));
System.out.println(getArrSize(i));
System.out.println(getArrSize(l));
System.out.println(getArrSize(f));
System.out.println(getArrSize(d));
}
private static <T> byte getArrSize(T[] arr) {
if (arr.length <= 0) {
return 0;
}
return (byte)(sizeof(arr[0])*arr.length);
}
private static byte sizeof(byte b) { return 1; }
private static byte sizeof(short s) { return 2; }
private static byte sizeof(int i) { return 4; }
private static byte sizeof(long l) { return 8; }
private static byte sizeof(float f) { return 4; }
private static byte sizeof(double d) { return 8; }
private static byte sizeof(Object o) {
switch (o.getClass().getName()) {
case "java.lang.Byte":
return 1;
case "java.lang.Short":
return 2;
case "java.lang.Integer":
return 4;
case "java.lang.Long":
return 8;
case "java.lang.Float":
return 4;
case "java.lang.Double":
return 8;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment