CAJP Lecture 8
CAJP Lecture 8
java. Lang.
Classes and interfaces defined by java. Lang.
java.lang is automatically imported into all programs.
It contains classes and interfaces that are fundamental to
virtually all of Java programming.
It is Java’s most widely used package.
java.lang includes the following classes:
Boolean
Number
7
Float Wrapper class
Different constructor
public class JavaFloatExample {
public static void main(String[] args) {
//create a Float object using one of the below given constructors
//1. Create an Float object from float primitive type
float f = 10.10f;
Float fObj1 = new Float(f);
System.out.println(fObj1);
//2. Create an Float object from double primitive type
double d = 10.10;
Float fObj2 = new Float(d);
System.out.println(fObj2);
/* 3. Create and Float object from String. Please note that this method can
throw NumberFormatException if string doesnt contain parsable number. */
Float fObj3 = new Float("25.34");
System.out.println(fObj3);
}}
8
/*Java Float compare example
This example shows how to compare a Float object with other
Float object, Float with an Object, or two float primitive values
using methods provided by
java.lang.Float class.*/
public class JavaFloatCompareExample {
public static void main(String[] args) {
float f1 = 5.35f;
float f2 = 5.34f;
int i1 = Float.compare(f1,f2);
if(i1 > 0){
System.out.println("First is grater"); // i1 = ?
}else if(i1 < 0){
System.out.println("Second is grater");
}else{
System.out.println("Both are equal");
}
Float fObj1 = new Float("5.35");
Float fObj2 = new Float("5.34");
int i2 = fObj1.compareTo(fObj2);
if(i2 > 0){
System.out.println("First is grater"); //
}else if(i2 < 0){
System.out.println("Second is grater");
}else{
System.out.println("Both are equal");
} } } 9
Convert float to Float object Example
This example shows how a float primitive value can be
converted to a Float object
10
/* Convert Float object to String object.
This example shows how a Float object can be
converted into a String object.*/
public class floattostring {
public static void main(String[] args) {
Float fObj = new Float(10.25);
//use toString method of Float class to convert Float into String.
11
/* Convert Float to numeric primitive data types example
- Convert Float to byte
- Convert Float to short
- Convert Float to int
- Convert Float to float
- Convert Float to double*/
13
boolean equals(object o)
It returns true if invoking float object is equivalent to the object ‘o’
14
/* Java Float isNaN method example
This example shows how to use isNaN()
method of the Float class. */
15
Wrapper classes
2. Double wrapper class :-
Constructor are:
Double (float f)
Double (double d)
Double (String S) throws NFE
16
Wrapper classes
3. Byte Wrapper class
Constructors are:
Byte (byte b)
Byte (String S) throws NFE
4. Integer Wrapper class
Constructors are:
Integer (int i)
Integer (String S) throws NFE
17
Wrapper classes
5. Long Wrapper class
Constructors are:
Long (long l)
Long (String S) throws NFE
6. Short Wrapper class
Constructors are:
Short (short s)
Short (String S) throws NFE
18
Wrapper classes
All the typecasting methods are same
as Float.
isIfinite and isNaN are not supported by
last four.
All remaining method replace the
primitive and nonprimitive data type
with the respective class.
19
Converting Numeric Strings to Primitive
Numbers using Parsing Method
int i = Integer.parseInt(str)
Converts string to primitive integer.
long l = Long.parseLong(str)
Converts string to primitive long.
20
Converting Numeric Strings to Primitive
Numbers using Parsing Method (Example)
class i {
public static void main(String[] args) {
int i ;
long j;
String str = "10";
String str1 = "100";
i= Integer.parseInt(str);
j= Long.parseLong(str1);
System.out.println(str);//10
System.out.println(i);//10
System.out.println(j);//100
}
}
21
Integer Wrapper Class
Static int parseInt(String s, int r)
It returns an int value specified by ‘s’
using the specified radix ‘r’.
By default the value of r = 10.
i.e. Indicates decimal value.
The ‘r’ can be 2, 8, 16 for different
representation
22
Integer Wrapper Class
Static String toBinaryString(int i) :
It returns a string that contains binary
equivalent of i.
Static String toOctalString(int i) :
It returns a string that contains Octal
equivalent of i.
Static String toHexString(int i) :
It returns a string that contains Hexadecimal
equivalent of i.
23
Example
class conversion {
public static void main(String[] args) {
//declare string containing binary number
String strBinaryNumber = "111000";
int decimalNumber = Integer.parseInt(strBinaryNumber,2);
System.out.println("Binary number converted to decimal number");
24
Boolean Wrapper class
Boolean is very thin wrap class which wraps
the boolean values.
It is useful when we want to pass boolean
variable by reference
Constants: True False
Constructor:
Boolean (boolean b)
Boolean (String s)
25
Boolean Wrapper class methods:
boolean booleanValue
It returns the primitive boolean value of the boolean
object.
boolean equals (Object o)
If the values of two boolean objects are same then it
returns true.
String toString()
It converts boolean object into string.
Static string toString (boolean b)
It converts primitive boolean into string
Static Boolean valueOf (String S)
It returns true if s = “true” else it returns False
26
Character Wrapper class
Constructor : Character (char ch)
‘ch’ specifies a character that will be
wrapped by a character object.
Eg:
Character ch = new Character ('A');
27
Character Wrapper class methods
char charValue()
toString()
28
Character Wrapper class methods
boolean equals (object o)
If values of two char objects are same it returns true.
29
Character Wrapper class methods
boolean isLetter(char ch)
Returns true if passed character is really a character.
Eg:
System.out.println( Character.isLetter('c')); //True
System.out.println( Character.isLetter('5')); //false
30
Character Wrapper class methods
boolean isUpperCase(char ch)
Returns true if passed character is really an uppercase.
Eg: System.out.println( Character.isUpperCase('c')); //false
System.out.println( Character.isUpperCase('C')); //true
System.out.println( Character.isUpperCase('\n')); //false
System.out.println( Character.isUpperCase('\t')); //false
32
Character Wrapper class methods
boolean isWhitespace(char ch)
The method determines whether the specified char value is a
white space which includes space, tab or new line.
33
Character Wrapper class methods
Static char forDigit(int i, int r)
It converts an integer value into character value
for (int i = 0; i < 16; i++)
System.out.println (Character.forDigit (i, 16));
Static int digit(char ch, int r)
It converts an character into integer by specified radix
char [] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'x' };
for (int i = 0; i < digits.length; i++)
System.out.println (Character.digit (digits [i], 16));
34