ITC C106 – Computer
Programming
Java Pre Defined Methods
Prepared by: A. C. Valderama – CSE- IT
Dept.
Predefined Classes
• Methods already written and provided by Java
• Organized as a collection of classes (class
libraries)
• To use: import package
• Method type: data type of value returned by
method
Predefined Method Example
import [Link];
public class PredefinedMethods {
public static void main(String args[]) {
String output="";
double u = 2.5;
double v = 3.0;
double y, w;
int z = -23;
y = [Link](2.0, 3.2);
w = [Link](u, 7);
output += "RESULTS:\n" + u + " to the " + v + " is “ +
[Link](u,v);
output += "\nValue of w is " + w;
output += "\nThe Absolute Value of " + z + " is " +
[Link](z);
[Link](null, output);
[Link](0);
}
}
Syntax: Value-Returning Method
modifier(s) returnType methodName(formal parameter list)
{
statements
}
Syntax: Value-Returning Method
Example
public static int square(int number)
{
return number * number;
}
double u = 2.5;
double v = 3.0;
double x, y, w;
x = [Link](u, v);
y = [Link](2.0, 3.2);
w = [Link](u, 7);
Various String Methods
• Substring ()
• public String 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.
• Examples:
– “hamburger".substring(4, 8) returns "urge"
– "smiles".substring(1, 5) returns "mile"
• Parameters:
– beginIndex - the beginning index, inclusive.
– endIndex - the ending index, exclusive.
• Returns:
– the specified substring.
• Throws:
– IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is
larger than the length of this String object, or beginIndex is larger than
endIndex.
Various String Methods
• Substring ()
• public String substring(int beginIndex)
• Returns a new string that is a substring of this string. The substring begins
with the character at the specified index and extends to the end of this
string.
• Examples:
– "unhappy".substring(2) returns "happy“
– "Harbison".substring(3) returns "bison“
– "emptiness".substring(9) returns "" (an empty string)
• Parameters:
– beginIndex - the beginning index, inclusive.
• Returns:
– the specified substring.
• Throws:
– IndexOutOfBoundsException - if beginIndex is negative or larger than the
length of this String object.
• length()
• public int length()
• Returns the length of this string. The length is
equal to the number of 16-bit Unicode
characters in the string.
• Returns:
– the length of the sequence of characters
represented by this object.
• charAt()
• public char charAt(int index)
• Returns the character at the specified index. An index ranges
from 0 to length() - 1. The first character of the sequence is at
index 0, the next at index 1, and so on, as for array indexing.
• Parameters:
– index - the index of the character.
• Returns:
– the character at the specified index of this string. The first character is
at index 0.
• Throws:
– IndexOutOfBoundsException - if the index argument is negative or not
less than the length of this string.
• indexOf()
• public int indexOf(char ch)
• Returns the index within this string of the first
occurrence of the specified character.
• Parameters:
– ch - a character.
• Returns:
– the index of the first occurrence of the character in the
character sequence represented by this object, or -1 if the
character does not occur.
• indexOf()
• public int indexOf(String str) Returns the index within this
string of the first occurrence of the specified substring.
• Parameters:
– str - any string.
• Returns:
– if the string argument occurs as a substring within this object, then the
index of the first character of the first such substring is returned; if it
does not occur as a substring, -1 is returned.
• Throws:
– NullPointerException - if str is null.
• toUpperCase()
• public String toUpperCase()
• Converts all of the characters in this String to upper case using the rules of the
default locale, which is returned by [Link].
• If no character in this string has a different uppercase version, based on calling the
toUpperCase method defined by Character, then the original string is returned.
• Otherwise, this method creates a new String object representing a character
sequence identical in length to the character sequence represented by this String
object and with every character equal to the result of applying the method
[Link] to the corresponding character of this String object.
• Examples:
– “Ruskin Antoine".toUpperCase() returns “RUSKIN ANTOINE“
– "Visit Paris!".toUpperCase() returns "VISIT PARIS!"
• Returns:
– the string, converted to uppercase.
• toLowerCase()
• public String toLowerCase()
• Converts all of the characters in this String to lower case using the rules of the
default locale, which is returned by [Link].
• If no character in the string has a different lowercase version, based on calling the
toLowerCase method defined by Character, then the original string is returned.
• Otherwise, this method creates a new String object that represents a character
sequence identical in length to the character sequence represented by this String
object, with every character equal to the result of applying the method
[Link] to the corresponding character of this String object.
• Examples:
– "French Fries".toLowerCase() returns "french fries“
– “UNDERSTAND JAVA”.toLowerCase() returns “understand java“
• Returns:
– the string, converted to lowercase.
public boolean equalsIgnoreCase(String anotherString) Compares this String
to another object. Returns true if the object is equal to this String; that
is, has the same length and the same characters in the same sequence.
Upper case characters are folded to lower case before they are
compared.
• Parameters:
anotherString - the String to compare this String against
• Returns:
true if the Strings are equal, ignoring case; false otherwise.
EXAMPLE:
String password = “JRU”;
String passwordinput;
if ([Link](password)) {
[Link]("Password Ok!”);
else
[Link]("Password Denied!");
public boolean regionMatches(int toffset, String other,
int ooffset, int len)
Determines whether a region of this String matches
the specified region of the specified String.
• Parameters:
– toffset - where to start looking in this String
– other - the other String
– ooffset - where to start looking in the other String
– len - the number of characters to compare
• Returns:
– true if the region matches with the other; false
otherwise.
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int
ooffset, int len)
• Determines whether a region of this String matches the specified region of the
specified String. If the boolean ignoreCase is true, upper case characters are
considered equivalent to lower case letters.
• Parameters:
– ignoreCase - if true, case is ignored
– toffset - where to start looking in this String
– other - the other String
– ooffset - where to start looking in the other String
– len - the number of characters to compare
• Returns:
– true if the region matches with the other; false otherwise.
EXAMPLE:
String str1 = "Ruskin";
String str2 = "Antoine";
String str3 = "ruskin";
output = "str1 match with str2?: " + [Link](0,str2,0,6);
//str1 match with str2?: false
output = "str1 match with str3?: "+[Link](true,0,str3,0,6);
//str1 match with str3?: true
public boolean startsWith(String prefix)
Determines whether this String starts with
some prefix.
• Parameters:
prefix - the prefix
• Returns:
true if the String starts with the specified
prefix; false otherwise.
public boolean endsWith(String suffix)
Determines whether the String ends with
some suffix.
• Parameters:
suffix - the suffix
• Returns:
true if the String ends with the specified
suffix; false otherwise.
public String replace(char oldChar,
char newChar) Converts this String by
replacing all occurences of oldChar with
newChar.
• Parameters:
oldChar - the old character
newChar - the new character
Resources:
• Java API
Prepared by: A. C. Valderama – CSE- IT
Dept.
Thank you!
Prepared by: A. C. Valderama – CSE- IT
Dept.