java.lang
The java.lang Provides classes that are fundamental to the design of the Java programming language.
Java compiler automatically imports this package. Second, the java.lang package is imported implicitly. Java is a pure object-oriented programming language where the code is written in form of classes. These class components are called types.
It is default package.
The java.lang.package class contains version information about the implementation and specification of a Java package.
The package java.lang contains classes and interfaces that are essential to the Java language. These include:
- The object, the ultimate superclass of all classes in Java.
- Thread, the class that controls each thread in a multithreaded program.
- Throwable, the superclass of all error and exception classes in Java.
- Classes that encapsulate the primitive data types in Java.
- Classes for accessing system resources and other low-level entities.
- Math, a class that provides standard mathematical methods.
- String, the class that represents strings.
The java.lang contains following class.
1) Object
2) Wrapper
3) Math
4) String
5) String Buffer
#1 Object Class
The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java.
The Object class is beneficial if you want to refer any object whose type you don't know. Notice that parent class reference variable can refer the child class object, know as upcasting.
#2 Wrapper Class
Wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
Sub Classes
Integer, Float, Double, String, Character, Boolean...
Example: Primitive to Wrapper
public class WrapperExample1
{
public static void main(String args[])
public static void main(String args[])
{
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}
Integer i=Integer.valueOf(a);//converting int into Integer
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}
}
#3 Math Class
The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Math class is the final class.
We can calculate arithmetic operation using math class.
We can perform scientific operation also.
Methods Of Math Class
1. int min(int, int)
2. long min(long, long)
3. double min(double, double)
4. float min(float, float)
5. int max(int, int)
6. long max(long, long)
7. double max(double, double)
8. float max(float, float)
9. double sin()
10. double cos()
11. double tan()
12. double asin()
13. double acos()
14. int sqrt
15. double ceil()
16. double floor()
17. int pow()
18. int abs()
19. double atan()
20. int abs()
21. float abs()
22. double abs()
Example Of Math Class
import java.lang.*;
class Mathdemo
{
public static void main(String agrs[])
{
{
System.out.println("Square root:"+math.sqrt(64));
System.out.println("Max.. value:"+math.max(10,5));
System.out.println("Sin:"+math.sin(20,2));
System.out.println("Minimum:"+math.min(5,2));
System.out.println("Minimum:"+math.min(5,2));
}
}
#4 String Class
The java.lang.String class provides a lot of methods to work on the string. By the help of these methods, we can perform operations on a string such as trimming, concatenating, converting, comparing, replacing strings etc.
Java String is a powerful concept because everything is treated as a string if you submit any form in window based, web-based or mobile application.
Constructors
String()
String(char ch[])
String (char char args[], int start, int end)
String(obj)
String(byte bytearg[])
String(byte between[], int first, int end)
Methods Of String Class
- length()
- concat(string)
- toString()
- char charAt(int)
- void getChars(int start, int end)
- byte getBytes(0
- toCharArray()
- equals()
- equalsIgnoreCase()
- compareTo()
- startWith(str)
- endWith(str)
- String subString()
#5 String Buffer Class
This class is used to break the string.
This class split the string or create a token of string.
The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.
Constructors
StringBuffer()
creates an empty string buffer with the initial capacity of 16.
StringBuffer(String str)
creates a string buffer with the specified string.
StringBuffer(int capacity)
creates an empty string buffer with the specified capacity as length.
creates an empty string buffer with the initial capacity of 16.
StringBuffer(String str)
creates a string buffer with the specified string.
StringBuffer(int capacity)
creates an empty string buffer with the specified capacity as length.
Methods
StringBuffer append(char c)
This method appends the string representation of the char argument to this sequence.
StringBuffer append(boolean b)
This method appends the string representation of the boolean argument to the sequence
StringBuffer append(char[] str)
This method appends the string representation of the char array argument to this sequence.
StringBuffer append(char[] str, int offset, int len)
This method appends the string representation of a subarray of the char array argument to this sequence.
StringBuffer append(CharSequence s)
This method appends the specified CharSequence to this sequence.
StringBuffer append(CharSequence s, int start, int end)
This method appends a subsequence of the specified CharSequence to this sequence.
StringBuffer append(double d)
This method appends the string representation of the double argument to this sequence.
StringBuffer append(float f)
This method appends the string representation of the float argument to this sequence.
StringBuffer append(int i)
This method appends the string representation of the int argument to this sequence.
StringBuffer append(long lng)
This method appends the string representation of the long argument to this sequence.
StringBuffer append(Object obj)
This method appends the string representation of the Object argument.
StringBuffer append(String str)
This method appends the specified string to this character sequence.
StringBuffer append(StringBuffer sb)
This method appends the specified StringBuffer to this sequence.
StringBuffer appendCodePoint(int codePoint)
This method appends the string representation of the codePoint argument to this sequence.
int capacity()
This method returns the current capacity.
char charAt(int index)
This method returns the char value in this sequence at the specified index.
int codePointAt(int index)
This method returns the character (Unicode code point) at the specified index
int codePointBefore(int index)
This method returns the character (Unicode code point) before the specified index
int codePointCount(int beginIndex, int endIndex)
This method returns the number of Unicode code points in the specified text range of this sequence
StringBuffer delete(int start, int end)
This method removes the characters in a substring of this sequence.
StringBuffer deleteCharAt(int index)
This method removes the char at the specified position in this sequence
void ensureCapacity(int minimumCapacity)
This method ensures that the capacity is at least equal to the specified minimum.
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
This method characters are copied from this sequence into the destination character array dst.
int indexOf(String str)
This method returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
StringBuffer insert(int offset, boolean b)
This method inserts the string representation of the boolean argument into this sequence.
StringBuffer insert(int offset, char c)
This method inserts the string representation of the char argument into this sequence.
StringBuffer insert(int offset, char[] str)
This method inserts the string representation of the char array argument into this sequence.
StringBuffer insert(int index, char[] str, int offset, int len)
This method inserts the string representation of a subarray of the str array argument into this sequence.
StringBuffer insert(int dstOffset, CharSequence s)
This method inserts the specified CharSequence into this sequence.
StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
This method inserts a subsequence of the specified CharSequence into this sequence.
StringBuffer insert(int offset, double d)
This method inserts the string representation of the double argument into this sequence.
StringBuffer insert(int offset, float f)
This method inserts the string representation of the float argument into this sequence.
StringBuffer insert(int offset, Object obj)
This method inserts the string representation of the Object argument into this character sequence.
StringBuffer insert(int offset, String str)
This method inserts the string into this character sequence.
int lastIndexOf(String str)
This method returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)
This method returns the index within this string of the last occurrence of the specified substring.
int length()
This method returns the length (character count).
int offsetByCodePoints(int index, int codePointOffset)
This method returns the index within this sequence that is offset from the given index by codePointOffset code points.
StringBuffer replace(int start, int end, String str)
This method replaces the characters in a substring of this sequence with characters in the specified String.
StringBuffer reverse()
This method causes this character sequence to be replaced by the reverse of the sequence.
void setCharAt(int index, char ch)
The character at the specified index is set to ch.
void setLength(int newLength)
This method sets the length of the character sequence.
CharSequence subSequence(int start, int end)
This method returns a new character sequence that is a subsequence of this sequence.
This method appends the string representation of the char argument to this sequence.
StringBuffer append(boolean b)
This method appends the string representation of the boolean argument to the sequence
StringBuffer append(char[] str)
This method appends the string representation of the char array argument to this sequence.
StringBuffer append(char[] str, int offset, int len)
This method appends the string representation of a subarray of the char array argument to this sequence.
StringBuffer append(CharSequence s)
This method appends the specified CharSequence to this sequence.
StringBuffer append(CharSequence s, int start, int end)
This method appends a subsequence of the specified CharSequence to this sequence.
StringBuffer append(double d)
This method appends the string representation of the double argument to this sequence.
StringBuffer append(float f)
This method appends the string representation of the float argument to this sequence.
StringBuffer append(int i)
This method appends the string representation of the int argument to this sequence.
StringBuffer append(long lng)
This method appends the string representation of the long argument to this sequence.
StringBuffer append(Object obj)
This method appends the string representation of the Object argument.
StringBuffer append(String str)
This method appends the specified string to this character sequence.
StringBuffer append(StringBuffer sb)
This method appends the specified StringBuffer to this sequence.
StringBuffer appendCodePoint(int codePoint)
This method appends the string representation of the codePoint argument to this sequence.
int capacity()
This method returns the current capacity.
char charAt(int index)
This method returns the char value in this sequence at the specified index.
int codePointAt(int index)
This method returns the character (Unicode code point) at the specified index
int codePointBefore(int index)
This method returns the character (Unicode code point) before the specified index
int codePointCount(int beginIndex, int endIndex)
This method returns the number of Unicode code points in the specified text range of this sequence
StringBuffer delete(int start, int end)
This method removes the characters in a substring of this sequence.
StringBuffer deleteCharAt(int index)
This method removes the char at the specified position in this sequence
void ensureCapacity(int minimumCapacity)
This method ensures that the capacity is at least equal to the specified minimum.
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
This method characters are copied from this sequence into the destination character array dst.
int indexOf(String str)
This method returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
StringBuffer insert(int offset, boolean b)
This method inserts the string representation of the boolean argument into this sequence.
StringBuffer insert(int offset, char c)
This method inserts the string representation of the char argument into this sequence.
StringBuffer insert(int offset, char[] str)
This method inserts the string representation of the char array argument into this sequence.
StringBuffer insert(int index, char[] str, int offset, int len)
This method inserts the string representation of a subarray of the str array argument into this sequence.
StringBuffer insert(int dstOffset, CharSequence s)
This method inserts the specified CharSequence into this sequence.
StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
This method inserts a subsequence of the specified CharSequence into this sequence.
StringBuffer insert(int offset, double d)
This method inserts the string representation of the double argument into this sequence.
StringBuffer insert(int offset, float f)
This method inserts the string representation of the float argument into this sequence.
StringBuffer insert(int offset, Object obj)
This method inserts the string representation of the Object argument into this character sequence.
StringBuffer insert(int offset, String str)
This method inserts the string into this character sequence.
int lastIndexOf(String str)
This method returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)
This method returns the index within this string of the last occurrence of the specified substring.
int length()
This method returns the length (character count).
int offsetByCodePoints(int index, int codePointOffset)
This method returns the index within this sequence that is offset from the given index by codePointOffset code points.
StringBuffer replace(int start, int end, String str)
This method replaces the characters in a substring of this sequence with characters in the specified String.
StringBuffer reverse()
This method causes this character sequence to be replaced by the reverse of the sequence.
void setCharAt(int index, char ch)
The character at the specified index is set to ch.
void setLength(int newLength)
This method sets the length of the character sequence.
CharSequence subSequence(int start, int end)
This method returns a new character sequence that is a subsequence of this sequence.
Simple Example
class Strdemo
{
public static void main(String args[])
public static void main(String args[])
{
StringBuffer s1=new StringBuffer("ABC");
StringBuffer s1=new StringBuffer("ABC");
StringBuffer s2=new StringBuffer("Piyush");
s1.setCharAt(z,'b');
s1.setCharAt(z,'b');
System.out.println("After insert"+s1);
s1.delete(0,3);
System.out.println(s1);
}
}