0% found this document useful (0 votes)
10 views7 pages

Inbuilt Methods - Copmuter

Java methods

Uploaded by

pra sat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Inbuilt Methods - Copmuter

Java methods

Uploaded by

pra sat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Description of all the Java library methods

Method Name Description Example


void print (any type of value) Displays the output and the System.out.print(10);//10
cursor remains in the same System.out.print(12.3);//12.3
line. System.out.print('a');//a
System.out.print("Hello ");//Hello
void println (any type of value) Displays the output and the System.out.println(10);//10
cursor comes to the next System.out.println(12.3);//12.3
line. System.out.println('a');//a
System.out.println("Hello ");//Hello
int/long/float/double abs(int/float/double/long) Returns the absolute value of Math.abs(-5);//5
the argument. Math.abs(-5.5f);//5.5
Math.abs(-123.456);//123.456
int/long/float/double max(val1, val2) Returns the maximum value Math.max(1,4);//4
among the arguments. Math.max(1.2f,8.6f);//8.6
Math.max(45.6, 89.90);//89.90
Math.max('a','A');//97
int/long/float/double min(val1, val2) Returns the minimum value Math.min(1,4);//1
among the arguments. Math.min(1.2f,8.6f);//1.2
Math.min(45.6, 89.90);//45.6
Math.min('a','A');//65
double sqrt(int/float/double) Returns the square root Math.sqrt(4);//2.0
value of the argument. Math.sqrt(25.0f);//5.0
Math.sqrt(625.0);//25.0
Math.sqrt(-25); //Nan [Runtime error]
double cbrt(int/float/double) Returns the cube root value Math.cbrt(27);//3.0
of the argument. Math.cbrt(-27);//-3.0
double pow(val1, val2) Returns the value of val1 Math.pow(1,2);//1.0
raised to val2 Math.pow(1.2f,3.4f);
Math.pow(2.3,4.5);
Math.pow('a', 2); //finds 972 = 99409.0
double ceil(int/float/double) Returns the value greater Math.ceil(4); //4.0
than or equal to the Math.ceil(4.2f);//5.0
argument. Math.ceil(5.0);//5.0
Math.ceil(5.3); //6.0
Math.ceil(-1.2); //-1.0
double floor(int/float/double) Returns the value lesser than Math.floor(4);//4.0
or equal to the argument. Math.floor(4.2f); //4.0
Math.floor(5.0);//5.0
Math.floor(5.3); //5.0
Math.floor(-1.2); //-2.0
double random() Returns a random number Math.random();
between 0(inclusive) and (int)Math.random()*10; // returns any
1(exclusive) number in the range 0 to 9
int/long round(float/double) Returns the rounded value of Math.round(5.6f); //6
a number from .5 and above. Math.round(4.5);//5
int length() Returns the total no. of String s="Java program";
characters present in the int L=s.length(); //12
string object including space.
char charAt(int) Returns the character at the String s="Java program";
corresponding index char c=s.charAt(2); //v
position. If the index position char c1=s.charAt(s.length());
is not found, then it raises a //Runtime Error-StringIndexOutOfBounds
run-time error char c2=s.charAt(s.length()-1);// m
StringIndexOutOfBounds
Exception.
int indexOf(char/string) Returns the first occurrence String s="Java program";
of the character or string int i1=s.indexOf('a'); //1
present in the string object. int i2=s.indexOf('A');//-1
If the character/string is not int i3=s.indexOf("va"); //2
found then it displays the
index position as -1 by
default.
int lastIndexOf(char/string) Returns the last occurrence String s="Java program";
of the character or string int i1=s.lastIndexOf('a'); //10
present in the string object.
boolean startsWith(string) Checks whether the string String s="Java program";
starts with the specified boolean b=s.startsWith("J"); //true
string argument and returns boolean b1=s.startsWith("Jav"); //true
true/false boolean b3=s.startsWith("j"); //false
boolean endsWith(string) Checks whether the string String s="Java program";
ends with the specified string boolean b=s.endsWith("m"); //true
argument and returns boolean b1=s.endsWith("ram"); //true
true/false boolean b3=s.endsWith("Ram"); //false
boolean equals(string) Checks the equality between String s1="Java";
calling string and argument String s2="Java";
string and returns true if boolean b=s1.equals(s2); //true
they are exactly matching Here s1 is calling string, and s2 is argument
with the case of characters, string.
otherwise false.
boolean equalsIgnoreCase(string) Checks the equality between String s1="Java";
calling string and argument String s2="JAVA";
string by ignoring the case of boolean b=s1.equalsIgnoreCase(s2); //true
characters and returns true Here s1 is calling string, and s2 is argument
if they are having same string.
characters, otherwise false.
String toUpperCase() Returns the string in String s1="convert";
uppercase by converting s1=s1.toUpperCase(); //CONVERT
lowercase characters to
uppercase.
String toLowerCase() Returns the string in String s1="CONVERT";
Lowercase by converting s1=s1.toLowerCase();//convert
uppercase characters to
lowercase.
String replace(char,char) Returns the string after String s1="babbage";
String replace(string, string) replacing all the occurrences s1=s1.replace('b','*'); // *a**age
of existing character/string String s2="keyboard";
with new character/string. s2=s2.replace("key","Black");
//Blackboard
String substring(int) Returns the part of the string String s1="Computer";
String substring(int, int) from the specified index String s2=s1.substring(3);//puter
positions. String s3=s1.substring(3, 7);//pute
String trim() Returns the string after String s1=" Go to the class ";
removing the space before s1=s1.trim(); //Go to the class
and after the end of the
string. It does not remove
space in between the string.
String concat(string) Concatenates or joins the String s1="St. ";
calling string with the string String s2="Johns";
present as the argument. String s3=s1.concat(s2); //St.Johns
int compareTo(string) Compares the calling string String s1="Laptop";
and argument string by String s2="Desktop";
taking the difference in ASCII int c=s1.compareTo(s2);
values. It returns +ve/-ve/0. // subtracts the ascii value of L and D and
If +ve then calling string is gives the output as positive value indicating
greater than the argument that Laptop is greater than the Desktop.
string. If it is –ve, then
calling string is lesser than
the argument string. If 0,
then it indicates both the
strings are same.
int compareToIgnoreCase(string) Compares the calling string String s1="Laptop";
and argument string by String s2="Latest";
taking the difference in ASCII int c=s1.compareTo(s2);
values by ignoring the case of // subtracts the ascii value of p and t and
characters. gives the output as positive value indicating
that Laptop is greater than the Latest.

String toString(number) Returns a numerical string int x=12;


by converting numbers to String s1=Integer.toString(x);
string. float y=12.3f;
String s2=Float.toString(y);
double z=12.3;
String s3=Double.toString(z);
char ch=’a’;
String s4=Character.toString(ch);

String valueOf(number) Returns a numerical string int x=12;


by converting numbers to String s1=String.valueOf(x);
string float y=1.2f;
String s2=String.valueOf(y);
double z=67.7;
String s3=String.valueOf(z);
boolean isUpperCase(char) Checks and returns true if boolean b=Character.isUpperCase('D'); //
the character is upper case, true
otherwise false.
boolean isLowerCase(char) Checks and returns true if boolean b=Character.isLowerCase('a'); //
the character is lower case, true
otherwise false.
boolean isDigit(char) Checks and returns true if boolean b=Character.isDigit('5'); // true
the character is digit,
otherwise false.
boolean isLetter(char) Checks and returns true if boolean b=Character.isLetter('D'); // true
the character is an alphabet,
otherwise false.
boolean isLetterOrDigit(char) Checks and returns true if boolean b=Character.isLetterOrDigit('a'); //
the character is true
alphabet/digit, otherwise b=Character.isLetterOrDigit('3');//true
false.
boolean isWhitespace(char) Checks and returns true if boolean b=Character.isWhitespace(' '); //
the character is upper case, true
otherwise false.
char toUpperCase(char) Converts the character from char c1=Character.toUpperCase('a'); //A
lowercase to uppercase. char c2=Character.toUpperCase('B'); //B
char c3=Character.toUpperCase('$');
// $
char toLowerCase(char) Converts the character from char c1=Character.toLowerCase('A'); //a
uppercase to lowercase. char c2=Character.toLowerCase('b'); //b
char c3=Character.toLowerCase('$');
// $
byte parseByte(number as string) Converts the numerical String s1="12";
string to byte type. byte b=Byte.parseByte(s1); [or]
byte b=Byte.valueOf(s1);
short parseShort(number as string) Converts the numerical String s1="123";
string to short type. short s=Short.parseShort(s1); [or]
short s=Short.valueOf(s1);
int parseInt(number as string) Converts the numerical String s1="123";
string to int type. int a=Integer.parseInt(s1);
[or] int a=Integer.valueOf(s1);
long parseLong(number as string) Converts the numerical String s1="123";
string to long type. long a=Long.parseLong(s1);
[or] long a=Long.valueOf(s1);
float parseFloat(number as string) Converts the numerical String s1="123.4";
string to float type. float a=Float.parseFloat(s1);
[or]float a=Float.valueOf(s1);
double parseDouble(number as string) Converts the numerical String s1="123.56";
string to double type. double a=Double.parseDouble(s1);
[or]
double a=Double.valueOf(s1);
boolean parseBoolean(boolean value as string) Converts the numerical String s1="true";
string as true/false to boolean a=Boolean.parseBoolean(s1);
boolean type. [or]
boolean a=Boolean.valueOf(s1);
int nextByte() Reads the token from the Scanner s=new Scanner(System.in);
int nextShort() scanner object as integer. System.out.println("Enter 3 integers");
int nextInt() int x=s.nextInt();
int y=s.nextByte();
int z=s.nextShort();
float nextFloat() Reads the token from the Scanner s=new Scanner(System.in);
scanner object as float. System.out.println("Enter an float value");
float x=s.nextFloat();
double nextDouble() Reads the token from the Scanner s=new Scanner(System.in);
scanner object as double. System.out.println("Enter an double value");
double x=s.nextDouble();
long nextLong() Reads the token from the Scanner s=new Scanner(System.in);
scanner object as long System.out.println("Enter an long value");
integer. long x=s.nextLong();
boolean nextBoolean() Reads the token from the Scanner s=new Scanner(System.in);
scanner object as true or System.out.println("Enter a boolean value");
false. boolean x=s.nextBoolean();
String next() Reads a string token from a Scanner s=new Scanner(System.in);
scanner object till it finds System.out.println("Enter a string");
first space character.It String x=s.next();
means it reads only one word
from the user’s input.
String nextLine() Reads a string token from Scanner s=new Scanner(System.in);
scanner object till the user System.out.println("Enter a string");
press enter key. It means it String x=s.nextLine();
reads more than one word
from the user’s input.
char next().charAt(0) Reads a single character Scanner s=new Scanner(System.in);
System.out.println("Enter a character");
char x=s.next().charAt(0);

You might also like