Quick introduction to Java Programming language - Part 1
Chapters
Working with Numbers
By using primitive data type we work with numbers, the primitive data types are
Primitive Type |
Wrapper Class(java.lang) |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
In some situation we need to need to use objects instead of primitive data types. Wrapper classes are used to convert primitive type into object.
- Wrapping is done using wrapper classes of that type wrapper classes.
- Wrapper classes are used to convert primitive type into object.
- Java provides a wrapper class for every primitive type.
- All wrapper classes are available in java.lang package.
- The process of converting primitive into object is known as boxing.
- The process of converting object into primitive type is known as unboxing.
Example:
public class BoxingUnboxingDemos { public static void main(String[] args) { int i=10; Integer iobj = new Integer(i); //Boxing int type to an Integer object System.out.println("iobj value is: "+iobj); int j = iobj.intValue(); //Unboxingthe Integer to a int type System.out.println("i value is: "+j); } }
Output
Number Methods:
abs():
This is a method which returns the absolute value of the argument which is type primitive data type are int, float, long, double, short, byte.
Syntax:
intabs(int age)
Parameters: Primitive data type.
Return Value: Returns the absolute value of the given argument.
Example:
public class AbsDemo { public static void main(String[] args) { Integer i = -11; float j = -1000; System.out.println(Math.abs(i)); System.out.println(Math.abs(j)); } }
Output
11
1000.0
asin():
This is a method which returns the arcsine of the given double value.
Syntax:
double asin(double 12.6)
Parameters: A double data types
Return Value: Returns the arcsine of the given double value.
Example
public class AsinDemo { public static void main(String[] args) { double deg = 65.0; double rad = Math.toRadians(deg); System.out.format("The value of pi is %.4f%n", Math.PI); System.out.format("The arcsine of %.4f is %.4f degrees %n", Math.sin(rad), Math.toDegrees(Math.asin(Math.sin(rad)))); } }
Output
xxxValue():
This is a method which converts the value of the Number Object that invokes the method to the primitive type that is returned from the method.
Syntax:
int intValue()
Parameters: Not applicable
Return Value: Returns the primitive data type that is specified in the signature.
Example:
public class ValueDemo { public static void main(String[] args) { Integer x = 11; System.out.println(x.shortValue()); // Returns short primitive type System.out.println( x.doubleValue() ); // Returns double primitive type } }
Output:
11
11.0
min():
This is a method which returns the minimum value out of given values.
Syntax:
int min(int val1,int val2)
Parameters: Primitive data type.
Return Value: Returns the minimum value of the two values.
Example:
public class MinimumDemo { public static void main(String[] args) { System.out.println(Math.min(11.1001, 11.1021)); System.out.println(Math.min(23, 242)); } }
Output
11.1001
23
max():
This is a method which returns the maximum value out of given values.
Syntax:int max(int val1,int val2)
Parameters: Primitive data type.
Return Value: Returnsthemaximumvalue of the two values.
Example:
public classMaximumDemo
{
public static void main(String[] args)
{
System.out.println(Math.max(11.1001, 11.1021));
System.out.println(Math.max(23, 242));
}
}
Output
11.1021
242
round():
This is a method which returns the closest int/long, as specified by the methods return type.
Syntax:int round(float a)
Parameters: Primitive data type of double/float.
Return Value: Returns the rounded off value long or int, as given by the method's return type, to the argument.
Example:
public class RoundDemo
{
public static void main(String[] args)
{
doubledeg = 700.680;
double amount = 101.900;
float interest = 10f;
System.out.println(Math.round(deg));
System.out.println(Math.round(amount));
System.out.println(Math.round(interest));
}
}
The Output is:
701
102
10
sqrt():
This is a method which returns the square root of the parameter.
Syntax:
int sqrt(int a)
Parameters: Primitive data types.
Return Value: Returns the square root of the given value.
Example:
public class SqrtDemo
{
public static void main(String[] args)
{
double x = 20.635;
System.out.printf("The value of e is %.4f%n", Math.E);
System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));
}
}
The Output is:
random():
This is s method is used to generate a random number between 0.0 &1.0.
The range is: 0.0 =<Math.random< 1.0.
Different ranges can be achieved by using arithmetic.
Syntax:
static double random()
Parameters: Not applicable.
Return Value: Returns adouble value.
Example:
public class RandomDemo
{
public static void main(String[] args)
{
System.out.println( Math.random() );
System.out.println( Math.random() );
}
}
The Output is:
compareTo():
This is a method which is used to compare the Number objects that invoked the method to the argument.
Syntax:
public int compareTo(SubClass_of_NumberreferenceName)
Parameters: referenceName-Byte, Short, Integer, Float, Double or Long.
Return Value:
- If the Integer is equal to the argument then it returns 0.
- If the Integer is less than the argument then it returns -1.
- If the Integer is greater than the argument then it returns 1.
Example:
public class ComparetoDemo
{
public static void main(String[] args)
{
Integer age = 10;
System.out.println(age.compareTo(5));
System.out.println(age.compareTo(10));
System.out.println(age.compareTo(13));
}
}
The Output is:
1
0
-1
equals():
This is a method which determines whether the Object of Number that invokes the method is equal to the argument.
Syntax:public boolean equals(Object o)
Parameters:Any object.
Return Value: if the argument is not null returns true and is an object of the same type and with the same numeric value.
Example:
public class EqualsDemo
{
public static void main(String[] args)
{
Integer age = 5;
Integer height = 5;
Integer weight =50;
System.out.println(age.equals(height));
System.out.println(age.equals(weight));
}
}
The Output is:
true
false
valueOf():
This is a method which returns the relevant Object of number storing the value of the argument passed.
This is a static method. It accepts two arguments, where one is a String and the other is a radix.
Syntax:static Integer valueof(int a)
static Integer valueof(String name, int radix)
Parameters:Primitive data type.
Return Value:
- valueOf(int a):Returns an Integer object storing the value of the given primitive.
- valueOf(String s):Returns an Integer object storing the value of the given string representation.
- valueOf(String s, int radix): Returns an Integer object storing the integer value of the given string representation, parsed with the value of radix.
Example:
public class ValueofDemo
{
public static void main(String[] args)
{
Integer box =Integer.valueOf(6);
Double color = Double.valueOf(15);
Float width = Float.valueOf("95");
Integer length = Integer.valueOf("567",10);
System.out.println(box);
System.out.println(color);
System.out.println(width);
System.out.println(length);
}
}
The Output is:
6
15.0
95.0
567
These were some of the commonly used function used with numbers. But Java API is so broad that everything cannot be covered in a single chapter. You should explore Java API to learn more functions and classes available for working with numbers
In next chapter we will explore working with Characters and Strings
Description
This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include
- Java basics
- Object oriented programming
- Advance concepts
Each part is subdivided in multiple chapters.
Java basics has the following chapters
- Introduction
- Environment Setup
- Basic Syntax
- Objects and Classes
- Basic Data Types
- Variable Types
- Modifier Types
- Basic Operators
- Loops
- Decision Making
- Numbers Class
- Character and String Class
- Arrays
- Date and Time
- Regular Expression
- Methods
- Streams, Files and I/O
- Exceptions Handling
Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.
Audience
Beginners or students seeking a refresher on Java language
Author: Subject Coach
Added on: 9th Mar 2015
You must be logged in as Student to ask a Question.
None just yet!