Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

HOME

java certification tutorial

chapter1 | chapter2 | chapter3 | chapter4 | chapter5 | chapter6 |

chapter7 | chapter8 | chapter9 | chapter10 | chapter11 |

 

Chapter8: java.lang package

 

  • Math class:The class Math contains methods for performing basic numeric operations such as elementary exponential, logarithm, square root and trignometric functions.
  • Math class is final and the methods are static.
  • abs:Returns the absolute value of a double value.If the argument is not negative,the argument is returned.If the argument is negative,the negation of the argument is returned.
  • abs() is overloaded for int, long, float and double.
  • ex: int i=Math.abs(-5); // i will be 5
  • ceil: Returns the smallest(closest to negitive infinity) double value that is not less than the argument and is equal to a mathematical integer (or) if we put in other way ceil returns the next while number up that is an integer.
  • ex:double X=Math.ceil(9.01);// returns 10.0
  • double X=Math.ceil(-0.1);// returns -0.0
  • double X=Math.ceil(100);// returns 100.0
  • floor :Returns the largest(closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
  • ex:double X=math.floor(9.01);//returns 9.0
  • double X=math.floor(-0.1);//returns -1.0
  • double X=math.floor(100);//returns 100.0
  • max: returns the greater of two values this is overloaded for int, long, float, double parameters.
  • ex:X=Math.max(5,10);//X will be 10
  • if either value is NaN then the result is NaN
  • Unlike the numerical comparision operators, this method considers negitive zero to be strictly smaller than positive zero.
  • min: Returns the smallest of two values.This is overloaded for int,long,float,double parameters.
  • unlike the numerical comparision operators this method considers negative zero to be strictly smaller than positive zero.
  • ex: X=Math.min(5,10);// X will be 5
  • X=Math.min(-5,-10);// X will be -10
  • random: Returns a random number greater than or equal to 0.0 and less than 1.0
  • Returned values are chosen randomly with(approximately)uniform distribution from that range
  • ex:double r=Math.random();
  • so to generate a random number say between 0 and 100
  • System.out.println(Math.random(1)*100));
  • round: Rounds to the nearest integer
  • if the value is more than half way towards the higher integer, the value is rounded up to the next integer.
  • If the number is less than this the next lowest integer is returned.
  • ex:Math.round(2.7);//returns 3.0
  • Math.round(2.2);//returns 2.0
  • round function is expressed as an int if the parameter is a float and as a long if the parameter is a double
  • int round(float X);// returns the closest int to the argument
  • long round(float X);// returns the closest long to the argument
  • sin,cos,tan: Returns the trignometric sine,cosine and tan of the angle respectively
  • These take the double arguments(angle is in radian)and returns a double
  • sqrt:Returns the square root of a double value
  • if the argument is NaN or less than zero the result is NaN.

 

Immutability of String objects:

  • once a String object is created it can never be changed.
  • Concatenating a String creates a new String to be instantiated behind the scenes.
  • The class String includes methods for examining individual characters of the sequence, for comparing the Strings ,for searching the Strings, for extracting substrings and for creating a copy of a String with all characters translated to upper case or to lower case.
  • length(): returns the length of the String i.e, returns the number of characters in a String.
  • ex:String X="Hello";
  • System.out.println(a.length()); //prints 5
  • toUpperCase: converts all the characters in the String to uppercase.
  • "xyz".touppercase() returns "XYZ".
  • toLowerCase:Converts all the characters in the String to lower case.
  • toLowerCase() and toUpperCase() will return a reference to original String object if it is already in lower case or upper case.
  • equals(): Compares this String to the specified object.The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
  • String's equals() is overriden to give you equality of the contents of the object.
  • StringBuffer's equals just check for equality of the object references.This equals() is not overriden
  • equals Ignore case():This method is like the equals() method but the case is ignored.
  • charAt():This method returns the character at the specified Index.An index ranges from 0 to length() -1.
  • Throws IndexOutOfBoundsException If the index argument is negative or not less than the length of this String.

ex:String X="Anil";

char c=X.charAt(3); //prints "l".

  • indexOf(int ch):Returns the index within this String of the first occurance of the specified character. If doesnot occur in the String -1 is returned.
  • indexOf(int ch,int from Index):Returns the index within this String of the first occurance of the specified character, starting the search at the specified index.

ch-character

fromIndex-the index to start the search from

 

  • index of(String):Returns the index within this String of the first occurance of the specified substring.
  • index of(String,int):Returns the index within this String of the first occurance of the specified substring,starting at the specified index

 

  • lastIndexOf:
  • lastIndex Of(int ch):Returns the index within this String of the last occurance of the specified character.
  • lastIndexof(int ch,int fromIndex):Returns the index within this String of the rightmost occurance of the specified substring.
  • lastIndexOf(String str,int fromIndex):Returns the index within this String of the last occurance of the specified substring.

ex:String X="XYZXYZYZZ";

int Y=X.lastIndexof('Z');//returns 9.

  • substring():
  • substring(int beginIndex):Returns a new String that is a substring of this String.The substring begins with the character at the specific index and extends at the end of the string.

ex:"unhappy".substring(2)//returns"happy"

  • If begin Index is negitive or longer than the length of the string then IndexOut of Bounds Exception is thrown.
  • substring(int beginIndex,int endIndex):Returns a new string that is a substring of this string.The substring begins at the specified beginIndex and extends to the character at index endIndex -1.Thus the length of the substring is endIndex-beginIndex.

ex:"hamburger".substring(4,8)//returns urge.

  • If the beginIndex is negitive or endIndex is larger than the lengthof this String object, or beginIndex is larger than endIndex.
  • concat:concatenates the specified String to the end of the string.

ex:"cares".concat("s")returns "caress".

  • If the lengthof the argument to concat() method is 0 then this String object is returned,otherwise a new String object is created.
  • toString():Returns a string representation of the object.
  • overrides the toString() method of class object.
  • By default the toString method for class object returns a String consisting of the name of the class of which the object is an instance,the at sign character 'e' and the unsigned hexadecimal representation of the hash code of the object.
  • trim():Removes the whitespaces from both ends of the string
  • String objects are immutable and consume lot of memory and time to stringBuffer class can be used because it can be modified i.e, it is mutable.
  • String can be converted to stringBuffer by passing the String to one of the stringBuffer constructor.
  • stringBuffer can be converted back to string using toString() method of stringBuffer.
  • You can't compare String to stringBuffer.
  • equals() is overriden only by String and wrapper classes and not by stringBuffer.

.

 

 

 

 

Copyrights anilbachi.8m.com All rights reserved

for queries contact anil_kuchana@yahoo.com