Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what primitive or what type should we use to represent a telephone number in java?

Tags:

java

types

what type could contain a number like 2023209999 in java? do you think that using a string type to represent a telephone number is a good idea?

like image 586
fenec Avatar asked Nov 26 '22 22:11

fenec


1 Answers

Using a string is a very good idea. Remember that the point of OOP is that different types of data have different usage patterns. So let's look at a phone number patterns.

  • Do we add phone numbers or perform other arithmetic on them?
  • Do we split it based on length, match against parts of it, replace parts of it?

The answer to the first question is no. So we don't manipulate them like numbers. The answer to the second question is yes, so we manipulate them like strings.

Now, many here are advocating making phones a class by itself. There is merit in this regard, but I'm addressing the more pressing concern of how do you store the phone number, which is something you need to do no matter if a phone is a class or not. A phone class which stored its data as a number would not be a good fit.

like image 154
Daniel C. Sobral Avatar answered Mar 02 '23 01:03

Daniel C. Sobral