Using Library Classes and Packages

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Using Library Classes and

Package
Packages
• A java package is a group of similar types of classes,
interfaces and sub-packages.
• Package in java can be categorized in two form, built-in
package and user-defined package.
• There are many built-in packages such as
java.lang- default
java.awt
javax.swing
java.applet
java.net
Java.io
Java.util
Java.sql
Wrapper Classes

• Wrapper classes are those whose objects wraps a


primitive data type within them.

• Wrapper classes provide methods to, convert


primitive datatypes within them to String objects
and, to compare them with other objects etc.

• In the java.lang package java provides a separate


class for each of the primitive data types namely
Byte, Character, Double, Integer, Float, Long,
Short, Boolean
Working with Strings
String:
String is a sequence of characters. In java, objects of String are
immutable which means a constant and cannot be changed
once created.

Java offers three classes to work with character data:


1) Character Class-
Its instance or object can hold single character data
2) String class-
Its instance or object can hold unchanging string (immutable
string)
3) StringBuffer Class-
Its instance or object can hold changeable string (mutable string)
• Java provides a wrapper class Character in java.lang package.

• An object of type Character contains a single field, whose


type is char.

• Creating a Character object :


Character ch = new Character('a');
char ch=‘a’;

char ch=sc.nextLine().charAt(0);
char ch=sc.next().charAt(0);
char ch=br.readLine().charAt(0);
Character Based Library Functions
boolean isLetter(char ch) :
This method is used to determine whether the specified char value(ch) is a letter or not.
The method will return true if it is letter([A-Z],[a-z]), otherwise return false.

Syntax : boolean isLetter(char ch)


        System.out.println(Character.isLetter('A')); 
        System.out.println(Character.isLetter('0')); 
true
false

boolean isDigit(char ch) :


This method is used to determine whether the specified char value(ch) is a digit or not.
Here also we can pass ASCII value as an argument.
Syntax : boolean isDigit(char ch)
System.out.println(Character.isDigit(‘1')); 
         System.out.println(Character.isDigit(‘A')); 
true
false
boolean isWhitespace(char ch) :
It determines whether the specified char value(ch) is white space. A whitespace
includes space, tab, or new line.
Syntax : boolean isWhitespace(char ch)
        System.out.println(Character.isWhitespace(‘ ')); 
        System.out.println(Character.isWhitespace(‘\t')); 
        System.out.println(Character.isWhitespace('\n')); 
true
true
true

boolean isUpperCase(char ch) : It determines whether the specified char value(ch)


is uppercase or not.
Syntax : boolean isUpperCase(char ch)
System.out.println(Character.isUpperCase('A'));
System.out.println(Character.isUpperCase(65));
true
false
boolean isLowerCase(char ch) :
It determines whether the specified char value(ch) is lowercase or not.
Syntax : boolean isLowerCase(char ch)
System.out.println(Character.isLowerCase('A')); 
     System.out.println(Character.isLowerCase('a')); 
     System.out.println(Character.isLowerCase(97));
false
true
true

char toUpperCase(char ch) :


It returns the uppercase of the specified char value(ch). If an ASCII value is passed, then the ASCII value
of it’s uppercase will be returned.
Syntax : char toUpperCase(char ch)
System.out.println(Character.toUpperCase('a')); 
str=“abc”;
str.toUpperCase()
System.out.println(Character.toUpperCase(97)); 
System.out.println(Character.toUpperCase(48));
A
65
48
char toLowerCase(char ch) :
It returns the lowercase of the specified char value(ch).
Syntax : char toLowerCase(char ch)
System.out.println(Character.toLowerCase('A')); 
      System.out.println(Character.toLowerCase(65)); 
      System.out.println(Character.toLowerCase(48)); 
a
97
48

toString(char ch) :
It returns a String class object representing the specified character value(ch)
i.e a one-character string. Here we cannot pass ASCII value.
Syntax : String toString(char ch)
System.out.println(Character.toString('x')); 
x
Accessor Method
• Method used to obtain information about an
object are known an accessor method.
• Class string provides many accessor functions
that are used to perform operations on string
String Based Library Functions
int length(): 
Returns the number of characters in the String.
“Computer".length(); 
8

char charAt(int i): 


Returns the character at ith index.
“Computer".charAt(3);
p
String substring (int i): 
Return the substring from the ith  index character to end.
"Computer".substring(3);
“puter”

String substring (int i, int j): 


Returns the substring from i to j-1 index.
"Computer".substring(3, 6);
“put”

String concat( String str): 


Concatenates specified string to the end of this string.
String s1 = ” Computer”;
String s2 = ”Science”;
String s3 = s1.concat(s2);
S3= “ComputerScience”

+ operator (concatenation operator)


String s1 = ” Computer”;
String s2 = ”Science”;
String s3 = s1+s2;
S3= “ComputerScience”
int indexOf (String s): 
Returns the index within the string of the first occurrence of the specified string.
String s = ”Screenr”;
int output = s.indexOf(“r”);
2

int indexOf (String s, int i): 


Returns the index within the string of the first occurrence of the specified string,
starting at the specified index.
String s = ”eScreen”;
int output = s.indexOf(“e",2);
4

Int lastIndexOf( String s): 


Returns the index within the string of the last occurrence of the specified string.
String s = ”eScreen”;
int output = s.lastIndexOf(“e");
5
boolean equals( Object otherObj): 
Compares this string to the specified object.
Boolean out = “book”.equals(“book”);
// returns true
Boolean out = “BOOK”.equals(“book”);
// returns false

boolean  equalsIgnoreCase (String anotherString): 


Compares string to another string, ignoring case considerations.
Boolean out= “book”.equalsIgnoreCase(“book”);
// returns true
Boolean out = “BOOK”.equalsIgnoreCase(“book”);
// returns true
 int compareTo( String anotherString):
 Compares two string lexicographically.
int out = s1.compareTo(s2); 
aaa
aaa
where s1 and s2 are strings to be compared
This returns difference s1-s2. If :

out < 0 // s1 comes before s2


out = 0 // s1 and s2 are equal.
out > 0 // s1 comes after s2.
String toLowerCase(): Converts all the characters in the String to lower case.
String s1 = “HeLLo”; String s2= s1.toLowerCase();
“hello“

String toUpperCase(): Converts all the characters in the String to upper case.


String s1 = “HeLLo”; String s2 = s1.toUpperCase();
“HELLO”

String trim(): Returns the copy of the String, by removing whitespaces at both


ends. It does not affect whitespaces in the middle.
String s1 = “ computer science “; String s2 = s1.trim();
“computer science”

 String replace (char oldChar, char newChar): Returns new string by replacing all
occurrences of oldChar with newChar.
String s1 = “eScreen“; String s2 = “eScreen”.replace(‘e’ ,’#’);
#Scr##n
• String endsWith(): It checks whether the String ends with a specified suffix. This method returns a boolean value true or false. If
the specified suffix is found at the end of the string then it returns true else it returns false.
Syntax: public boolean endsWith(String suffix)
String s1 = “How are you?”; System.out.println(s1.endsWith(“?”)
String s1 = “How are you!”; System.out.println(s1.endsWith(“?”)
True
false

• String startsWith(): It checks whether the String starts with a specified prefix. This method returns a boolean value true or
false. If the specified prefix is found at the beginning of the string then it returns true else it returns false.
Syntax: public boolean startsWith(String prefix)
String s1 = “How are you?”; System.out.println(s1.startsWith(“How”)
String s1 = “How are you!”; System.out.println(s1.startsWith(“What”)
True
false

String valueOf():
 The java string valueOf() method converts different types of values into string. By the help of string valueOf() method, you can
convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to string and
char array to string.
•  String valueOf(char c)  
• String valueOf(int i)  
• String valueOf(long l)  
• String valueOf(float f)  
• String valueOf(double d)  
String s1=String.valueOf(30);  
   String s2 = String.valueOf(12.5d); 
   String s3 = String.valueOf(26.00f); 
StringTokenizer Class
• It is located into java.util package.

• In Java, StringTokenizer is used to break a string into tokens


based on provided delimiter.

• Delimiter can be specified either at the time of object creation


or on a per-token basis.

• Its object internally maintains a current position within the


string to be tokenized.

• In string, tokenizer objects are maintained internally and


returns a token of a substring from the given string.
Following are the constructors in string tokenizer

1. StringTokenizer(String str)
-creates StringTokenizer with specified string.
2. StringTokenizer(String str, String delim)
-creates StringTokenizer with specified string and delimeter.
3. StringTokenizer(String str, String delim, boolean returnValue)
-creates StringTokenizer with specified string and delimeter.

String str=“India is my, country,”;


StringTokenizer st=new StringTokenizer(str, ”,”, true);
India is my
,
Country
,
Following are the methods in stringtokenizer
1. int countTokens()
-returns the total number of tokens.
2. String nextToken()
-returns the next token from the StringTokenizer object.
3. boolean -checks if there is more tokens available.

4. String nextToken(String delim)


-returns the next token based on the delimeter.
5. boolean hasMoreElements()
-same as hasMoreTokens() method.
6. Object nextElement()
-same as nextToken() but its return type is Object.

You might also like