Phone Number Data Type in Java

Mohammad Irfan Oct 12, 2023
  1. Data Type to Store Phone Number in Java
  2. String Data Type to Store Phone Number in Java
  3. Store Phone Number in Entity Class Java
Phone Number Data Type in Java

This tutorial introduces what should be the best data type to store phone numbers in Java, and how to use it in Java entity as well.

Phone number is a numeric value and contains digits only, so at first you might think of the long type to store it in, but the best data type to store it in is a String because it will ruin the formatting, remove preceding 0’s, and other undesirable things.

While working with phone numbers, we must first understand the input format getting from the user. For example, Country Code + Area Code + Phone Number the format requires a String type, and taking only the phone number can be stored in long type.

Let’s understand with some Java and Hibernate examples.

Data Type to Store Phone Number in Java

In this example, we used long type to store a numeric (phone number) value in Java. It is good if the numeric value is under the range; otherwise, the compiler throws out a range of errors. It is not good if the phone number includes non-numeric digits such as +. So, we must understand the input format before taking the user input. See the example below.

public class SimpleTesting {
  public static void main(String args[]) {
    Student student = new Student(10, "Rohan", 1245232542);
    System.out.println("id = " + student.getId());
    System.out.println("Name = " + student.getName());
    System.out.println("Phone Number =  " + student.getPhoneNumber());

    Student student2 = new Student(11, "Sohan", +1245232542);
    System.out.println("id = " + student2.getId());
    System.out.println("Name = " + student2.getName());
    System.out.println("Phone Number =  " + student2.getPhoneNumber());
  }
}
class Student {
  int id;
  String name;
  long phoneNumber;

  public Student(int id, String name, long phoneNumber) {
    super();
    this.id = id;
    this.name = name;
    this.phoneNumber = phoneNumber;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public long getPhoneNumber() {
    return phoneNumber;
  }
  public void setPhoneNumber(long phoneNumber) {
    this.phoneNumber = phoneNumber;
  }
}

Output:

id = 10
Name = Rohan
Phone Number =  1245232542
id = 11
Name = Sohan
Phone Number =  1245232542

String Data Type to Store Phone Number in Java

If we use + sign with the phone number in the above example, it does not work, and the Java compiler does not hold that. It is better to use String type to hold a phone number in Java to solve this problem. Now, we store phone numbers with a Country Code + Area Code + Phone Number format. See the example below.

public class SimpleTesting {
  public static void main(String args[]) {
    Student student = new Student(10, "Rohan", "1245232542");
    System.out.println("id = " + student.getId());
    System.out.println("Name = " + student.getName());
    System.out.println("Phone Number =  " + student.getPhoneNumber());

    Student student2 = new Student(11, "Sohan", "+911245232542");
    System.out.println("id = " + student2.getId());
    System.out.println("Name = " + student2.getName());
    System.out.println("Phone Number =  " + student2.getPhoneNumber());
  }
}
class Student {
  int id;
  String name;
  String phoneNumber;

  public Student(int id, String name, String phoneNumber) {
    super();
    this.id = id;
    this.name = name;
    this.phoneNumber = phoneNumber;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPhoneNumber() {
    return phoneNumber;
  }
  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }
}

Output:

id = 10
Name = Rohan
Phone Number =  1245232542
id = 11
Name = Sohan
Phone Number =  +911245232542

Store Phone Number in Entity Class Java

If you are working with hibernate and created an entity that has one field like a phone number, you should declare that as a string to store a phone number that has non-numeric digits.

In hibernate, this entity class maps to the table in MySQL or Oracle database and uses varchar type to store the phone number. This is an advanced concept, and if you are not familiar with ORM or hibernate concept, skip this example.

package com.example.myspring;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // This tells Hibernate to make a table
public class User {
  @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;
  private String name;
  private String email;
  private String phoneNumber; // phone number

  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getPhoneNumber() {
    return phoneNumber;
  }

  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }
}

Related Article - Java Data Type