Surely, one of those examples does exactly what the other example says
you should not do. That is, assume that the memory used to store a primitive data type is the same as the apparent size of that data type. A Java int might be stored in 4 bytes, but it might be stored in more than 4. A short might be stored in 2, 4, 8 or some other number of bytes. You don't know.
Any attempt to measure the true amount of memory used by a Java object is full of dodgy assumptions and/or involves time-consuming and inapproximate measurement. Therefore, you should think very carefully about why you think your application needs to know the size of the object.
I have written an awful lot of Java (but hopefully not a lot of awful Java) and have never found a situation where it was absolutely required to know how big a Java object was. A different approach has always proved possible.
If you really do need to know this information, please tell us why - it will be interesting!