SlideShare uma empresa Scribd logo
1 de 18
Computer
Programming 2
Lesson 10– Java –Numbers Class
Prepared by: Analyn G. Regaton
NUMBER
CLASS
Normally, when we work with Numbers, we use primitive data
types such as byte, int, long, double, etc.
Example
int i = 5000;
float gpa = 13.65f;
double mask = 125;
However, in development, we come across situations where
we need to use objects instead of primitive
data types. In order to achieve this, Java provides wrapper
classes.
All the wrapper classes (Integer, Long, Byte,
Double, Float, Short) are subclasses of the abstract
class Number.
DIAGRAM
Converting primitive data types into object is called boxing,
and this is taken care by the compiler.
The Number class is part of the java.lang package.
EXAMPLE
PROGRAM
public class Test {
public static void main(String args[]) {
Integer x = 5; // boxes int to an Integer object
x = x + 10; // unboxes the Integer to a int
System.out.println(x);
}
}
Output
15
When x is assigned an integer value, the compiler
boxes the integer because x is integer object.
Later, x is unboxed so that they can be added as
an integer.
NUMBER
METHOD
Sr.No. Method & Description
1 xxxValue()
Converts the value of this Number object to the xxx data type and
returns it.
2 compareTo()
Compares this Number object to the argument.
3 equals()
Determines whether this number object is equal to the argument.
4 valueOf()
Returns an Integer object holding the value of the specified primitive.
5 toString()
Returns a String object representing the value of a specified int or
Integer.
6 parseInt()
This method is used to get the primitive data type of a certain String.
NUMBER
METHOD
7 abs()
Returns the absolute value of the argument.
8 ceil()
Returns the smallest integer that is greater than or equal to the
argument. Returned as a double.
9 floor()
Returns the largest integer that is less than or equal to the argument.
Returned as a double.
10 rint()
Returns the integer that is closest in value to the argument. Returned as
a double.
11 round()
Returns the closest long or int, as indicated by the method's return type
to the argument.
12 min()
Returns the smaller of the two arguments.
Sr.No. Method & Description
NUMBER
METHOD
13 max()
Returns the larger of the two arguments.
14 exp()
Returns the base of the natural logarithms, e, to the power of the argument.
15 log()
Returns the natural logarithm of the argument.
16 pow()
Returns the value of the first argument raised to the power of the second
argument.
17 sqrt()
Returns the square root of the argument.
18 sin()
Returns the sine of the specified double value.
19 cos()
Returns the cosine of the specified double value.
Sr.No. Method & Description
NUMBER
METHOD
20 tan()
Returns the tangent of the specified double value.
21 asin()
Returns the arcsine of the specified double value.
22 acos()
Returns the arccosine of the specified double value.
23 atan()
Returns the arctangent of the specified double value.
24 atan2()
Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.
25 toDegrees()
Converts the argument to degrees.
26 toRadians()
Converts the argument to radians.
27 random()
Returns a random number.
Sr.No. Method & Description
xxxValue()
Method
The method converts the value of the Number Object that invokes the
method to the primitive data type that is
returned from the method.
Syntax
Here is a separate method for each primitive data type −
byte byteValue() short shortValue() int intValue() long longValue()
float floatValue() double doubleValue()
Parameters
Here is the detail of parameters −
•All these are default methods and accepts no parameter.
Return Value
•This method returns the primitive data type that is given in the signature.
EXAMPLE
PROGRAM
Output
5
5.0
5
compareTo()
Method
The method compares the Number object that invoked the
method to the argument. It is possible to compare
Byte, Long, Integer, etc.
However, two different types cannot be compared, both
the argument and the Number object
invoking the method should be of the same type.
Syntax
public int compareTo( NumberSubClass referenceName )
Parameters
Here is the detail of parameters −
•referenceName − This could be a Byte, Double, Integer, Float,
•Long, or Short.
Return Value
•If the Integer is equal to the argument then 0 is returned.
•If the Integer is less than the argument then -1 is returned.
•If the Integer is greater than the argument then 1 is returned.
EXAMPLE
PROGRAM
public class Test {
public static void main(String args[]) {
Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
}
}
This will produce the following result −
Output
-1
0
1
equals()
Method
The method determines whether the Number object that invokes
the method is equal to the object that is passed as an argument.
Syntax
public boolean equals(Object o)
Parameters
Here is the detail of parameters −
•Any object.
Return Value
•The method returns True if the argument is not null and is an
object of the same type and with the
• same numeric value. There are some extra requirements for
Double and Float objects that are described in the Java API
documentation.
EXAMPLE
PROGRAM
public class Test {
public static void main(String args[]) {
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;
System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
}
}
Output
False
true
false
valueOf()
Method
The valueOf method returns the relevant Number Object holding the value of the
argument passed.
The argument can be a primitive data type, String, etc.
This method is a static method. The method can take two arguments, where one is
a String and the other is a radix.
Syntax
Following are all the variants of this method −
static Integer valueOf(int i)static Integer valueOf(String s)static Integer valueOf(String s, int
radix)
Parameters
Here is the detail of parameters −
•i − An int for which Integer representation would be returned.
•s − A String for which Integer representation would be returned.
•radix − This would be used to decide the value of returned Integer based on the
passed String.
Return Value
•valueOf(int i) − This returns an Integer object holding the value of the specified
primitive.
•valueOf(String s) − This returns an Integer object holding the value of the
specified string representation.
•valueOf(String s, int radix) − This returns an Integer object holding the integer
value of the specified string representation, parsed with the value of radix.
EXAMPLE
PROGRAM
public class Test {
public static void main(String args[]) {
Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}
}
Output
95.0
80.0
10
92
toString()
Method
The method is used to get a String object representing the value of the Number
Object.
If the method takes a primitive data type as an argument, then the String object
representing the primitive data type value is returned.
If the method takes two arguments, then a String representation of the first
argument in the radix specified by the
second argument will be returned.
Syntax
Following are all the variants of this method −
String toString()static String toString(int i)
Parameters
Here is the detail of parameters −
•i − An int for which string representation would be returned.
Return Value
•toString() − This returns a String object representing the value of this Integer.
•toString(int i) − This returns a String object representing the specified integer.
EXAMPLE
PROGRAM
public class Test {
public static void main(String args[]) {
Integer x = 5;
System.out.println(x.toString());
System.out.println(Integer.toString(12));
}
}
Output
5
12

Mais conteúdo relacionado

Mais procurados

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
tech4us
 

Mais procurados (19)

Python data handling
Python data handlingPython data handling
Python data handling
 
Chap07alg
Chap07algChap07alg
Chap07alg
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
 
Chap12alg
Chap12algChap12alg
Chap12alg
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
 
C string
C stringC string
C string
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
C programming assignment help
C programming assignment helpC programming assignment help
C programming assignment help
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Python numbers
Python numbersPython numbers
Python numbers
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Python ppt
Python pptPython ppt
Python ppt
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 

Semelhante a Computer programming 2 Lesson 10

Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 

Semelhante a Computer programming 2 Lesson 10 (20)

Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Built-in Classes in JAVA
Built-in Classes in JAVABuilt-in Classes in JAVA
Built-in Classes in JAVA
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
130717666736980000
130717666736980000130717666736980000
130717666736980000
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Java 8
Java 8Java 8
Java 8
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
Arrays
ArraysArrays
Arrays
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 

Mais de MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

Computer programming 2 Lesson 10

  • 1. Computer Programming 2 Lesson 10– Java –Numbers Class Prepared by: Analyn G. Regaton
  • 2. NUMBER CLASS Normally, when we work with Numbers, we use primitive data types such as byte, int, long, double, etc. Example int i = 5000; float gpa = 13.65f; double mask = 125; However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper classes. All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.
  • 3. DIAGRAM Converting primitive data types into object is called boxing, and this is taken care by the compiler. The Number class is part of the java.lang package.
  • 4. EXAMPLE PROGRAM public class Test { public static void main(String args[]) { Integer x = 5; // boxes int to an Integer object x = x + 10; // unboxes the Integer to a int System.out.println(x); } } Output 15 When x is assigned an integer value, the compiler boxes the integer because x is integer object. Later, x is unboxed so that they can be added as an integer.
  • 5. NUMBER METHOD Sr.No. Method & Description 1 xxxValue() Converts the value of this Number object to the xxx data type and returns it. 2 compareTo() Compares this Number object to the argument. 3 equals() Determines whether this number object is equal to the argument. 4 valueOf() Returns an Integer object holding the value of the specified primitive. 5 toString() Returns a String object representing the value of a specified int or Integer. 6 parseInt() This method is used to get the primitive data type of a certain String.
  • 6. NUMBER METHOD 7 abs() Returns the absolute value of the argument. 8 ceil() Returns the smallest integer that is greater than or equal to the argument. Returned as a double. 9 floor() Returns the largest integer that is less than or equal to the argument. Returned as a double. 10 rint() Returns the integer that is closest in value to the argument. Returned as a double. 11 round() Returns the closest long or int, as indicated by the method's return type to the argument. 12 min() Returns the smaller of the two arguments. Sr.No. Method & Description
  • 7. NUMBER METHOD 13 max() Returns the larger of the two arguments. 14 exp() Returns the base of the natural logarithms, e, to the power of the argument. 15 log() Returns the natural logarithm of the argument. 16 pow() Returns the value of the first argument raised to the power of the second argument. 17 sqrt() Returns the square root of the argument. 18 sin() Returns the sine of the specified double value. 19 cos() Returns the cosine of the specified double value. Sr.No. Method & Description
  • 8. NUMBER METHOD 20 tan() Returns the tangent of the specified double value. 21 asin() Returns the arcsine of the specified double value. 22 acos() Returns the arccosine of the specified double value. 23 atan() Returns the arctangent of the specified double value. 24 atan2() Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta. 25 toDegrees() Converts the argument to degrees. 26 toRadians() Converts the argument to radians. 27 random() Returns a random number. Sr.No. Method & Description
  • 9. xxxValue() Method The method converts the value of the Number Object that invokes the method to the primitive data type that is returned from the method. Syntax Here is a separate method for each primitive data type − byte byteValue() short shortValue() int intValue() long longValue() float floatValue() double doubleValue() Parameters Here is the detail of parameters − •All these are default methods and accepts no parameter. Return Value •This method returns the primitive data type that is given in the signature.
  • 11. compareTo() Method The method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc. However, two different types cannot be compared, both the argument and the Number object invoking the method should be of the same type. Syntax public int compareTo( NumberSubClass referenceName ) Parameters Here is the detail of parameters − •referenceName − This could be a Byte, Double, Integer, Float, •Long, or Short. Return Value •If the Integer is equal to the argument then 0 is returned. •If the Integer is less than the argument then -1 is returned. •If the Integer is greater than the argument then 1 is returned.
  • 12. EXAMPLE PROGRAM public class Test { public static void main(String args[]) { Integer x = 5; System.out.println(x.compareTo(3)); System.out.println(x.compareTo(5)); System.out.println(x.compareTo(8)); } } This will produce the following result − Output -1 0 1
  • 13. equals() Method The method determines whether the Number object that invokes the method is equal to the object that is passed as an argument. Syntax public boolean equals(Object o) Parameters Here is the detail of parameters − •Any object. Return Value •The method returns True if the argument is not null and is an object of the same type and with the • same numeric value. There are some extra requirements for Double and Float objects that are described in the Java API documentation.
  • 14. EXAMPLE PROGRAM public class Test { public static void main(String args[]) { Integer x = 5; Integer y = 10; Integer z =5; Short a = 5; System.out.println(x.equals(y)); System.out.println(x.equals(z)); System.out.println(x.equals(a)); } } Output False true false
  • 15. valueOf() Method The valueOf method returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. This method is a static method. The method can take two arguments, where one is a String and the other is a radix. Syntax Following are all the variants of this method − static Integer valueOf(int i)static Integer valueOf(String s)static Integer valueOf(String s, int radix) Parameters Here is the detail of parameters − •i − An int for which Integer representation would be returned. •s − A String for which Integer representation would be returned. •radix − This would be used to decide the value of returned Integer based on the passed String. Return Value •valueOf(int i) − This returns an Integer object holding the value of the specified primitive. •valueOf(String s) − This returns an Integer object holding the value of the specified string representation. •valueOf(String s, int radix) − This returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.
  • 16. EXAMPLE PROGRAM public class Test { public static void main(String args[]) { Integer x =Integer.valueOf(9); Double c = Double.valueOf(5); Float a = Float.valueOf("80"); Integer b = Integer.valueOf("444",16); System.out.println(x); System.out.println(c); System.out.println(a); System.out.println(b); } } Output 95.0 80.0 10 92
  • 17. toString() Method The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned. If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned. Syntax Following are all the variants of this method − String toString()static String toString(int i) Parameters Here is the detail of parameters − •i − An int for which string representation would be returned. Return Value •toString() − This returns a String object representing the value of this Integer. •toString(int i) − This returns a String object representing the specified integer.
  • 18. EXAMPLE PROGRAM public class Test { public static void main(String args[]) { Integer x = 5; System.out.println(x.toString()); System.out.println(Integer.toString(12)); } } Output 5 12