I am new to Java, and I tried to write my first relatively complex program. After I wrote several classes, I realized that I barely use the built-in classes (e.g. BigInteger, MessageDigest, ByteBuffer) directly, because they do not fully meet my needs. Instead, I write my own class and inside the class I use the built-in class as an attribute. Example:
public class SHA1 { public static final int SHA_DIGEST_LENGTH = 20; private MessageDigest md; public SHA1() { try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public void update(byte[] data) { md.update(data); } public void update(BigNumber bn) { md.update(bn.asByteArray()); } public void update(String data) { md.update(data.getBytes()); } public byte[] digest() { return md.digest(); } }
With the following simple class, I don't need to use try catch when using SHA1, I can put my own BigNumber class as a parameter, and I can also put a String parameter as a parameter for updating.
The next BigNumber class contains all the functions that I need and exactly how I need them.
public class BigNumber { private BigInteger m_bn; public BigNumber() { m_bn = new BigInteger("0"); } public BigNumber(BigInteger bn) { m_bn = bn; } public BigNumber(String hex) { setHexStr(hex); }
My question is, how common are the Java languages ββto use these classes instead of the built-in classes? Does this make my code unreadable to other programmers (compared to implementing everything with built-in classes)?
I read that new C ++ programmers are desperate to write the codes they used to write in C, so the benefits of C ++ remain hidden to them. I'm afraid I'm doing something similar in Java: I'm trying to implement everything myself, and not directly use the built-in classes. Does this happen (e.g. in the BigNumber class)?
Thank you for your opinion!
source share