BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to Find length of an Integer in Java

Last Updated: June 10, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to find length of an Integer in Java. There are several ways you can achieve this. One of the easiest way you can do this is by converting the Integer to a string and then get the length of that string. This approach handles both positive and negative numbers correctly.

1. Using String Conversion

In this code, we are converting given number to string using toString() method and then returning the length of the String using length() method.

public class IntegerLength {
public static void main(String[] args) {
Integer number = 12345;
int length = getIntegerLength(number);
System.out.println("Number of digits: " + length);
}

public static int getIntegerLength(Integer number) {
if (number == null) {
return 0; // or throw an exception, depending on your needs
}
// converting number to String and then returning length
return number.toString().length();
}
}

2. Using String Conversion for Negative Numbers

In case of negative numbers, the above program would count the – sign as a digit, to rectify this, we can modify the above program like this, Here we are using replace() method of String class to replace negative sign with nothing, ultimately removing the sign from number.

public class IntegerLength {
public static void main(String[] args) {
Integer number = -12345;
int length = getIntegerLength(number);
System.out.println("Number of digits: " + length);
}

public static int getIntegerLength(Integer number) {
if (number == null) {
return 0; // or throw an exception, depending on your needs
}
return number.toString().replace("-", "").length();
}
}

3. Using Logarithms

Another way to count number of digits in an Integer is by using logarithms. You can use Math.abs() and Math.log10() method to do this.

  • Math.abs(number) handles negative numbers as it converts them into positive numbers.
  • Math.log10(number) returns the base 10 logarithm of the number.
  • Adding 1 to the result gives the number of digits in the given Integer.
public class IntegerLength {
public static void main(String[] args) {
Integer number = -12345;
int length = getIntegerLength(number);
System.out.println("Number of digits: " + length);
}

public static int getIntegerLength(Integer number) {
if (number == null) {
return 0;
}
if (number == 0) {
return 1;
}
return (int) Math.log10(Math.abs(number)) + 1;
}
}

Top Related Articles:

  1. Java StringBuffer setLength() Method
  2. How to Find Size of an int in Java
  3. How to set Path in Java
  4. Java Math.random() Method
  5. Java Math.exp() Method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap