Page 256 - IT-802_class_12
P. 256
3.14 Java LIBrarIES
Some methods are provided by Java for making coding easy. These in-built methods are available in the library class which
can be imported into the code dynamically at runtime. This library further contains packages.
Packages are a group of similar type of classes, interfaces and sub-packages, which contains some built-in classes
containing methods, that are required during coding. Packages can be categorized into two, namely, built-in packages
and user-defined packages.
There are many built-in packages that are commonly used such as java, lang, awt, javax, swing, net, io, util, sql, etc.
To include a package in a program, the syntax is
import java.<packagename>.<classname>;
For examples:
import java.util.*;
import java.io.*;
The ”lang” package is the default package in Java and when we use it, we do not need to use the ‘import’ command
as it is automatically executed.
3.14.1 String Manipulation
As explained earlier, a string is a combination of a set of characters written within double quotes. To assign a sentence
or a word to a String variable, the following syntax is used.
String (variable)= “String Literal”;
For example:
String str1=”Computer Science”;
String str2=”Kolkata – 700115”;
Important String Methods
There are several useful String methods that come in handy while writing programs. Let us learn about them one by one.
The int length() method
This method returns the number of characters present in a string. It returns an integer type of value. The length() is the
only function where the counting starts from 1. The syntax is:
int <variable> = String_datatype_Variable.length();
Let us see the below example:
String sub= “i am studying Computer Science “;
int len= sub.length();
System.out.println(sub + “contains “ + len + “ characters”);
Output:
I am studying Computer Science contains 31 characters
The char charAt(int index) method
For a string variable, this method returns the character at the given index. It returns character type data. The syntax is:
char <variable> = String_datatype_Variable.charAt(int index);
Let us see the below example:
String str= “Class 12 iSC”;
char ch= str.charAt(2);
System.out.println(“The character is: “ +ch);
254 Touchpad Information Technology-XII

