Java Tut – Numbers

Normally when we work with numbers, we use primitive data types such as byte,int,long,double, etc

e.g:
int i=400;
float gpa=13.65;
byte mask=0xaf;
In develoment we need to use objects instead of primitive data types.So java provides wrapper classes

All the weapper classes are the subclasses of the abstract class Number.
Number Subclasses
The object of the wrapper class contains or wraps it respective primitive data type.converting primitive data types into object called boxing, and this is taken care by the compiler.so while passing the wrapper class you just need to pass the value of the primitive data type to the constructor of the wrapper class.

And the wrapper object will be converted back to a primitive data type and this process is called un boxing.The Number class is part of the java.lang package.

example:

public class Test{
public static void main(String[] args)
{
Integer x=5; //boxes int into an Integer object
x=x+10; // unboxes the Integer to a int
System.out.println(x);
}
}

output:
15
When x is assigned integer value the compiler boxes the integer because x is integer object.Later, x is unboxed so that they can be added as integer.
Number Methods:
Here is the list of the instance methods that all the subclasses of teh Number class implement:

xxxxValue() method:
Syntax:
here is a separate method for each primitive data type:

byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()

example:

public class ValueMethod {
public static void main(String[] args)
{
	Integer x=5;
	//Returns byte primitive data type
	System.out.println(x.byteValue());
	//Returns double primitive data type
	System.out.println(x.doubleValue());
	//Returns long primitive data type
	System.out.println(x.longValue());
}
}

Output:

5
5.0
5

compareTo() method:
Two different types cannot be compared, both the argument and the Number object invoking(ডাকা) should be the same type:
Syntax:

public int compareTo(NumberSubClass referenceName)

Parameters:
referenceName – this could be a Byte,Double,Integer,Float,Long or Short
Return Value:
– If the integer in referenceName is equal to argument object then 0 is returned.
– If the Integer is less than the argument than 1 is returned.
– If the Integer is greater than the argument than -1 is returned

Example:

public class CompareTo {
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));
}
}

Output:

1
0
-1

equals() Method:
Syntax:
public boolean equals(Object o)
Parameters:
Here is the detail of parameters:
o — is any object
Return Value:
The method returns True or false if the arguments 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:

public class Equals {
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

 

Java Max() method:
The method gives the maximum of the two arguments.The arguments can be int,float,long,double
Syntax:
This method has following variants:

double max(double argu1,double argu2)
float max(float arg1,float arg2)
int max(int arg1,int arg2)
long max(long arg1,long arg2)

Parameters: This method accepts any primitive data type as parameter
Return value: This method returns the maximum of the two arguments
Example:

public class Max {
public static void main(String[] args){
System.out.println(Math.max(12.123, 12.456));
System.out.println(Math.max(23.12, 23));
}
}

Output:

12.456
23.12

exp():
Syntax:

double exp(double d)
example:

public class Exp {
	public static void main(String[] args){
	double x=11.635;
	double y = 2.76;
	
	System.out.printf("The value of e is %.4f%n",Math.E);
	System.out.printf("exp(%.3f) is %.3f%n",x,Math.exp(x));
	}
}

out:

The value of e is 2.7183
exp(11.635) is 112983.831

toRadians()
syntax:

double toRadians(double d)

 

Parameter: d  a double data type
Return value: This method returns a double value
example:

public class ToRadians {
	public static void main(String[] args){
		double x=45.0;
		double y=30.0;
		System.out.println(Math.toRadians(x));
		System.out.println(Math.toRadians(y));
	}
}

output:

0.7853981633974483
0.5235987755982988


floor() method/ceil method:

This method floor gives the largest integer that is less than or equal to the argument.

This method ceil gives the smallest integer that is greater than or equal to the argument.
Syntax:

double floor(double d)
double floor(float f)

Parameter:A double or flaot primitive data type
Return Value: This method returns the largest integer that is less than or equal to the argument.Returned as double.
example:

public class Floor {
	public static void main(String args[]){
		double d=-100.675;
		float f=-90;
		System.out.println(Math.floor(d));
		System.out.println(Math.floor(f));
		System.out.println(Math.ceil(d));
		System.out.println(Math.ceil(f));
	}
}

output:

-101.0
-90.0
-100.0
-90.0

toString() method:

This 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.

String toString()
static String toString(int i)

i — An int for which string representation would be returned

public class toString {
	public static void main(String args[]){
		Integer x=5;
		System.out.println(x.toString());
		System.out.println(Integer.toString(55));
	}
}

output:
5
55

valueOf():

public class ValueOf {
	public static void main(String args[]){
		Integer x=Integer.valueOf(9);
		Float a=Float.valueOf("80");
		Integer b=Integer.valueOf("444",16);
System.out.println(b);
	
	}
}

output:

1092

parseInt() or parseXXXX():

This method is used to get the primitive data type of a certain String.parseXXX() is a static method can have one argument or two.

static int parseInt(String s)
static int parseInt(String s, int radix)

Parameters:
s — This is a string representation of deciaml
radix – This would be used to convert String s into integer

Return Value:
parseInt(String s): This returns an integer(decimal only)
parseInt(int i): This returns an Integer, given a string representation of decimal,binary,octal or hexadecimal(radix equals 10,2,8 or 16 respectively) numbers as input.

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

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