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

what type can contain a number like 2023209999 in java? Do you think using a string type to represent a phone number is a good idea?

+4
source share
11 answers

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

  • Do we add phone numbers or do other arithmetic operations?
  • Will we divide it by length, compare it with its parts, replace its parts?

The answer to the first question is no. Therefore, we do not manipulate them like numbers. The answer to the second question: yes, so we manipulate them as strings.

Now many here advocate making phones the class itself. There is merit in this regard, but I turn to the more urgent problem of how you store the phone number, what you need to do regardless of whether the phone is a class or not. A phone class that has saved its data as a number will not be suitable.

+28
source

I would write a PhoneNumber class that uses String as the base store and adds validation / good formatting features.

+9
source

I would call String, at least, but personally, I would create a PhoneNumber object. This is what gives additional methods, such as:

boolean isValid(); PhoneNumberUtils.getCountry(PhoneNumber number); PhoneNumberUtils.getState(PhoneNumber number); 

... or whatever. One thing that I would think of is simply letting people enter phone numbers and get the system to learn the rest. I despise the input of data that can be determined by the system. These are just my preferences.

At a simpler level, just encapsulating the String in the PhoneNumber object gives your brain a pen ... after a week or so, when your brain asks: โ€œWhere should this phone number method go?โ€, You may find yourself with a quick answer.

+4
source

I think the dedicated PhoneNumber class is a way to solve it. A phone number is not just a string. First of all, phone numbers comply with rules, such as: they contain only numbers, in the USA they can contain 7 or 10 digits. You will need a constructor to make sure your phone numbers are correct.

Secondly, the class will make it easier for you to distinguish between different formats. For example, 555-4834 and 5554834 are different lines, but have the same phone number.

Finally, you probably want to use methods such as getAreaCode () or getLocalNumber (). Calling such a method is much more concise and much less error prone than directly managing the string:

 String phoneNumber pn = ....; String localNumber = pn.length() == 7 ? pn : pn.substring(4) : 
+2
source

it's pretty late, but I can add my 2 cents ....

I am in the telecommunications sector and have gained the best experience for storing phone numbers in structures (or objects) with variable-length character members, i.e.

 struct TelephoneNumber ( InternationalPrefix VARCHAR; AreaCode VARCHAR; Subscriber VARCHAR; Extension VARCHAR;) 

I never store access digits (zeros, double zeros, pluses, etc.), they do not belong to the phone number as such, but are part of what I can call the โ€œdialing rulesโ€

 struct DialingRule ( International VARCHAR; National VARCHAR; Local VARCHAR;) 

typical values โ€‹โ€‹for DialingRule are "00", "0", "NULL" for a straight line, and "000", "00", "0" for a PBX requiring "0 to get a row"

to "display" the number, you can freely format objects, insert hyphens, brackets, or whatever you like

to create an accessible sequence, I determine the type (international, national or local) by comparing the corresponding elements of the FROM and TO number and adding the corresponding row from the set of dialing rules as a prefix.

All this may seem redundant, but I did not come up with international applications with high data integrity requirements and strong links to hardware. It eliminates the ambiguity and the need for hard coding lengths etc. when you want to manipulate numbers. It is also easy to fill in parts of the structure from the country / city search tables containing ISO country codes, IATA city codes and their corresponding prefixes.

Good luck MikeD

+2
source

In German regions, city codes start at 0, so an integer representation will lose this information.

However, I would not recommend using String. Use the Phonenumber class (or interface and implementations) instead. This approach has some advantages. If at some point you find that String is not enough, you just need to change the Phonenumber class not for every class using Phonenumbers.
In addition, this will allow you to separate the area code and the number inside.

+1
source

If you need to write leading 0 or + for international, you should use String. If this does not bother you, you can simply use the long one. eg.

 long phoneNumber = 2023209999L; // the L is for a long constant. 
0
source

It would be fair to reduce to what you plan to do with it. If you just want to save it in order to retype it, a string or maybe even a long one would be good if we assume that we are dealing with US numbers.

If you want to do something more complex, create a class with several lines, one for each component.

Basically, there is simply not enough information to make a real decision.

0
source

Using the String type allows you to split the phone number without voodoo or casting.

For example, if you change the format for receiving phone numbers, you will have to do additional work to get the phone number if it is long instead of String .

0
source

Another thing: a int will be too small. the maximum value of a int can be 2,147,483,647. as for the primitives, long is your best bet.

0
source

It depends on what you do. Let's say you want to be able to present international numbers, local numbers, exchange office numbers, etc. In this case, the string is a poor choice. He has no meta-information. You probably want the class to represent a phone number.

As for what you use to indicate a phone number in this class, you can use BigInteger (to be able to have international numbers), or, more simply, you could store each part of the phone number for how long.

0
source

Source: https://habr.com/ru/post/1286119/


All Articles