[Go to site: main page, start]

0% found this document useful (0 votes)
5 views13 pages

Java Number Class Methods Explained

The document discusses the Java Number class, which is an abstract class that includes various wrapper classes for primitive data types. It explains the importance of using wrapper classes, common methods associated with the Number class, and concepts of autoboxing and unboxing. Additionally, it provides examples of Java programs related to different number types and properties.

Uploaded by

annabiswas317
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Java Number Class Methods Explained

The document discusses the Java Number class, which is an abstract class that includes various wrapper classes for primitive data types. It explains the importance of using wrapper classes, common methods associated with the Number class, and concepts of autoboxing and unboxing. Additionally, it provides examples of Java programs related to different number types and properties.

Uploaded by

annabiswas317
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Numbers – Number Methods

with Syntax and Examples


BY DATAFLAIR TEAM
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
In the previous article, we learned about the concept of Java Wrapper classes and
why we need them during development. Java String class, Java Number class are
all examples of these wrapper classes which convert a primitive datatype to an
object of the class. Here in this article, we are going to talk about the Number
class and its methods in Java programming.

Number Class in Java


We have to note that the number class is not a real class but an abstract class. It
has the following wrapper classes that define the functions inside it.
a. Integer
b. Byte
c. Double
d. Short
e. Float
f. Long

The number class is a part of the [Link] package.


You can see and remember that these are the same as the primitive data types
we learned about in the previous articles. But all of these are individual classes
that are signified by the capital letters starting each class name(This is a class
naming convention).
The compiler automatically converts the primitive data type to an object and vice
versa as and when required by a particular function or scope of the program.
This is called Autoboxing and Unboxing. We will learn about them soon.

Why should you use a wrapper class


instead of a primitive datatype?
Now, the question arises as to why you should use Wrapper Class objects when
you can simply use primitive datatypes?
The answer lies in the use of these objects.

 There are many methods that take an Object as an argument. We can use
Wrapper class objects there. Eg-MAX_VALUE, MIN_VALUE.
 There is also a need for manipulating and performing particular functions on a
datatype. That is when objects can be used.
 Interconversion methods such as binary to hexadecimal can be used by
passing the particular number as an object.
 There are particular constants that are provided by the number class which is
useful while programming.

Methods of Java Number Class


There are a lot of classes that implement the abstract class but there are some
methods that are common to all the classes. Some of them are explained below.
1. Value Method in Java
This method converts the value of the number object into the datatype
mentioned. It has the syntax as:

<variable>.<datatype>Value();
After conversion it stores the value in a datatype which is primitive.

Java program to illustrate the use of Value method:


package [Link];
public class ValueMethod {
public static void main(String[] args) {
Integer num1 = new Integer("5");
float f = [Link]();
double d = [Link]();
[Link]("The integer value converted to float is " + f);
[Link]("The integer value converted to double is " + d);
}
}

Output
The integer value converted to float is 5.0
The integer value converted to double is 5.0

2. Java compareTo Method


The compareTo function compares a given object to a different object of the same
type. If both the values are the same then the function returns 0. If the given
number is less than the argument then it returns -1. Else it returns 1.

Its syntax is:


int compareTo(NumberClass ReferenceName);
Java program to illustrate the use of compareTo:
package [Link];
public class CompareToMethod {
public static void main(String[] args) {
Integer num1 = new Integer("5");
[Link]("comparing num1 with 10 is " + [Link](10));
[Link]("comparing num1 with 1 is " + [Link](1));
}
}

Output
comparing num1 with 10 is -1
comparing num1 with 1 is 1

3. equals Method in Java


The equals method checks if the number and the argument passed through the
method is not null and equal to the number. If both the numbers are equal, it
returns true. If not, it returns false. They also have to be of the same type.

The syntax is:

boolean <variable>.equals(Reference)
Java program to illustrate the usage of equals method:
package [Link];
public class EqualsMethod {
public static void main(String[] args) {
Integer num1 = new Integer("5");
Short s = new Short("5");
Integer num2 = new Integer("5");
[Link]("Is the short value equal to num1? " + [Link](s));
[Link]("is the integer value equal to num1? " + [Link](num2));
}
}

Output
Is the short value equal to num1? false
is the integer value equal to num1? true

4. parseInt Method in Java


This method converts the value of the String passed through it into a data type
which is a primitive datatype which, in this case, is int. If we want to convert to
other datatypes such as float or long we have to use parseFloat or parseLong.

The radix is the base of the string from which it is to be converted to decimal.
Care should be taken that the string does not contain any digits which lie outside
the scope of the radix mentioned. For example, if the radix is 2 which signifies a
binary base then the string cannot have numbers other than 0 or 1. If it does the
compiler will throw an exception.

There are two methods of using the parseInt method:

a. int [Link](String,radix);
b. int [Link](String);
When no radix is mentioned it has a default value of 10 which means all the
strings passed without a radix would be converted to a decimal number.

Java program to illustrate the use of parseInt:


package [Link];
public class ParseIntMethod {
public static void main(String[] args) {
int num1 = [Link]("AB", 16);
int num2 = [Link]("1001", 2);
int num3 = [Link]("543"); //if no radix is mentioned it is converted to decimal.
[Link]("num1 " + num1); //Hexadecimal converted to decimal
[Link]("num2 " + num2); //Binary converted to decimal
[Link]("num3 " + num3); //Direct conversion of string to decimal
}
}

Output
num1 171
num2 9
num3 543

5. toString Method in Java


Java toString method is used for converting a numeric datatype to a string. This
helps in returning the value of an object. Calling this method means, overriding
the .toString() method. It is useful while printing the value of the object.
Although while printing an object, the compiler automatically invokes
the .toString() method.

It has four variations:

a. <variable>.toString()
b. [Link](int i);
c. [Link](int i);
d. [Link](int i);
The variation where an integer value i is passed returns a decimal String object
of value i.

Java program to illustrate the use of toString method:


package [Link];
public class ToStringMethod {
public static void main(String[] args) {
Integer int1 = new Integer("167");
[Link]("Converting to decimal string" + [Link]());
[Link]("Converting to octal string " + [Link](int1));
[Link]("Converting to Hexadecimal string " + [Link](int1));
}
}

Output
Converting to decimal string167
Converting to octal string 247
Converting to Hexadecimal string a7

6. valueof Method in Java


This method returns the number object which is relevant. Simply put, this
method converts the argument passed through it to an Integer object. However if
there is a radix mentioned in the argument, the respective data is converted into
the base of the radix first and then converted to an Integer object. This is a static
method.

There are three alternative ways to use this method:


a. valueOf(int i)- This returns an Integer object which represents the integer
value.
b. valueOf(String i)- This returns an Integer representation of the string passed
c. valueOf(String i,radix)-This returns an Integer representation of the string
passed of base radix.
Java program to illustrate the use of valueOf:
package [Link];
public class ValueOfMethod {
public static void main(String[] args) {
Integer int1 = new Integer("167");
[Link]("Using [Link](int1) is " + [Link](int1));
[Link]("Using [Link](\"542\") is " + [Link]("542"));
[Link]("Using [Link](\"AB2\") is " + [Link]("AB2", 16));
}
}

Output
Using [Link](int1)is 167
Using [Link](“542”) is 542
Using [Link](“AB2”)is 2738

Autoboxing and Unboxing in Java


Java Autoboxing is the process of converting a primitive datatype to its specific
wrapper class object. This is useful when:
 Primitive datatype is passed as an argument to a method that expects an
object.
 Variable is assigned to the corresponding wrapper class.
Java Unboxing is the reverse of autoboxing. It is the process of converting the
Wrapper class object into its corresponding primitive datatype. This is useful
when:
 Wrapper class object is used where a primitive datatype is expected.
 It is assigned to a primitive variable.
Java program to illustrate the concept of Autoboxing and Unboxing:
package [Link];
public class AutoboxingUnboxing {
public static void main(String[] args) {
[Link]("Understanding Autoboxing");
//This is a primitive datatype
Integer i = 10;
//Autoboxing of the primitive value 10 to an object
[Link]("The integer is " + i);
[Link]("Understanding Unboxing");
Integer num = new Integer(98);
int unboxnum = num;
//Unboxing of the num object to a primitive datatype int
[Link]("The value of the integer is " + unboxnum);
}
}

Output
Understanding Autoboxing
The integer is 10
Understanding Unboxing
The value of the integer is 98

Java autoboxing- and unboxing


Autoboxing seamlessly links primitive data types with their corresponding
wrapper classes, while unboxing reverses this process, converting wrapper
objects back to primitives for greater code readability and efficiency. This is
illustrated in the snippet Integer num = 10; // Autoboxing where the integer
value 10 is automatically converted to an Integer object, and then subsequently
unboxed back to the primitive int type in int num2 = num; // Unboxing.
Number Based Programs in Java
Armstrong Number Program in Java

Java Programs

Armstrong Number is a positive number if it is equal to the sum of cubes of its digits is
called Armstrong number and if its sum is not equal to the number then its not a
Armstrong number. Armstrong Number Program is very popular in java, c language,
python etc. Examples: 153 is Armstrong, (1*1*1)+(5*5*5)+(3*3*3) = 153

Automorphic Number Program in Java

Java Programs

An Automorphic number is a number whose square “ends” in the same digits as the
number itself. Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625

Buzz Number Program in Java

Java Programs

A number is said to be Buzz Number if it ends with 7 or is divisible by 7. Example: 1007


is a Buzz Number.

Circular Prime Program in Java

Java Programs

A circular prime is a prime number with the property that the number generated at
each intermediate step when cyclically permuting its digits will be prime. For example,
1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.

CoPrime Numbers Program in Java

Java Programs
Two integers a and b are said to be relatively prime, mutually prime, or coprime if the
only positive integer that divides both of them is 1. Example: 13 and 15 are co prime.

Digit to Word Program in Java

Java Programs

import [Link]; public class DigitToWord { public static void main(String[]


args) { // TODO code application logic here int r, […]

Duck Number Program in Java

Java Programs

A Duck number is a number which has zeroes present in it, but there should be no zero
present in the beginning of the number. For example 3210

Factorial Program in Java

Java Programs

A Duck number is a number which has zeroes present in it, but there should be no zero
present in the beginning of the number. For example 3210

Factors Program in Java

Java Programs

Factor a number or algebraic expression that divides another number or expression


evenly—i.e., with no remainder. For example, 3 and 6 are factors of 12 because 12 ÷ 3
= 4 exactly and 12 ÷ 6 = 2 exactly. The other factors of 12 are 1, 2, 4, and 12. Factors
of 12: 1, 2, 3, 4, 6, 8, 12.

Fibonacci Series Program in Java

Java Programs

A series of numbers in which each number ( Fibonacci number ) is the sum of the two
preceding numbers. The simplest is the series 0, 1, 1, 2, 3, 5, 8, etc.

Floyd Triangle Program in Java

Java Programs

Floyd Triangle Program in Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15


Greatest Common Divisor Program in Java

Java Programs

the greatest common divisor (gcd) of two or more integers, which are not all zero, is
the largest positive integer that divides each of the integers. For example, the gcd of 8
and 12 is 4.

Happy Number Program in Java

Java Programs

A happy number is a natural number in a given number base that eventually reaches 1
when iterated over the perfect digital invariant function for. Those numbers that do not
end in 1 are -unhappy numbers.

Harshad Number Program in Java

Java Programs

In mathematics, a harshad number (or Niven number) in a given number base is an


integer that is divisible by the sum of its digits when written in that base.

Least Common Multiple Program in Java

Java Programs

The least common multiple, lowest common multiple, or smallest common multiple of
two integers a and b, usually denoted by LCM(a, b), is the smallest positive integer that
is divisible by both a and b.

Multiply Of Digit Program in Java

Java Programs

If a number=1234, then 1*2*3*4 ,Multiply of digit=24, Multiply Of Digit Program in Java

Neon Number Program in Java

Java Programs

A neon number is a number where the sum of digits of square of the number is equal
to the number. For example if the input number is 9, its square is 9*9 = 81 and sum of
the digits is 9. i.e. 9 is a neon number.

Niven Number Program in Java


Java Programs

In mathematics, a Niven number (or harshad number) in a given number base is an


integer that is divisible by the sum of its digits when written in that base.

Palindrome Number Program in Java

Java Programs

A palindromic number is a number that remains the same when its digits are reversed.
Like 16461, for example,

Perfect Number Program in Java

Java Programs

A perfect number is a positive integer that is equal to the sum of its positive divisors,
excluding the number itself. For instance, 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6,
so 6 is a perfect number.

Prime Number Program in Java

Java Programs

A prime number is a natural number greater than 1 that cannot be formed by


multiplying two smaller natural numbers. A natural number greater than 1 that is not
prime is called a composite number. For example, 5 is prime because the only ways of
writing it as a product, 1 × 5 or 5 × 1, involve 5 itself.

Reverse Number Program in Java

Java Programs

If a number=1234, then reverse of number is 4321.

Special Number Program in Java

Java Programs

A number is said to be special number when the sum of factorial of its digits is equal to
the number itself. Example- 145 is a Special Number as 1!+4!+5!=145.

Spy Number Program in Java

Java Programs
A spy number is a number where the sum of its digits equals the product of its digits.
For example, 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the
product of its digits is 1*1*2*4=8.

Twin Prime Program in Java

Java Programs

A twin prime is a prime number that is either 2 less or 2 more than another prime
number—for example, either member of the twin prime pair (41, 43). In other words, a
twin prime is a prime that has a prime gap of two.

Twisted Prime Program in Java

Java Programs

A number is called a twisted prime number if it is a prime number and reverse of this
number is also a prime number.

Unique Number Program in Java

Java Programs

A number is said to be unique , if the digits in it are not repeated. for example, 12345
is a unique number. 123445 is not a unique number.

Disarium Number Program in Java

Java Programs

A number is called Disarium number if the sum of its power of the positions from left to
right is equal to the number. Example: 1 + 3*3 + 5*5*5 = 1 + 9 + 125 = 135

Tech Number Program in Java

Java Programs

A tech number can be tech number if its digits are even and the number of digits split
into two number from middle then add these number if the added number’s square
would be the same with the number it will called a Tech Number. If the number is split
in two equal halves,then the square of sum of these halves is equal to the number
itself. Write a program to generate and print all four digits tech numbers. Note: If
number of digits is not even then it is not a tech number.

Prime Number Up to N Terms Program in Java

Java Programs
Prime Number Up to N Terms Program in Java. Ex: Enter size of prime=5 Number is
prime=2 Number is prime=3 Number is prime=5 Number is prime=7 Number is
prime=11 Number is prime=13

Magic Number Program in Java

Java Programs

Magic number is the if the sum of its digits recursively are calculated till a single digit If
the single digit is 1 then the number is a magic number. Magic number is very similar
with Happy Number.

Pronic Number Program in Java

Java Programs

A number is said to be a pronic number if product of two consecutive integers is equal


to the number, is called a pronic number. Example- 42 is said to be a pronic number,
42=6×7, here 6 and 7 are consecutive integers

Ugly Number Program in Java

Java Programs

A number is said to be an Ugly number if positive numbers whose prime factors only
include 2, 3, 5. For example, 6(2×3), 8(2x2x2), 15(3×5) are ugly numbers while
14(2×7) is not ugly since it includes another prime factor 7. Note that 1 is typically
treated as an ugly number.

You might also like