0% found this document useful (0 votes)
58 views16 pages

Strings: - String Is Sequence of Character. - in Java String Is A Class Objects and Implemented Using Two Classes Namely

- String is a sequence of characters that is implemented as a class in Java. StringBuffer is another class that allows modification of strings. - The document discusses string and string buffer methods like append, insert, deleteCharAt and compares their usage. It also covers string arrays, sorting strings, and extracting characters from strings. - Other topics summarized include wrapper classes for primitive types, interfaces, implementing and extending interfaces, and examples comparing the calculation of areas for shapes implementing a common interface.

Uploaded by

irshad17
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views16 pages

Strings: - String Is Sequence of Character. - in Java String Is A Class Objects and Implemented Using Two Classes Namely

- String is a sequence of characters that is implemented as a class in Java. StringBuffer is another class that allows modification of strings. - The document discusses string and string buffer methods like append, insert, deleteCharAt and compares their usage. It also covers string arrays, sorting strings, and extracting characters from strings. - Other topics summarized include wrapper classes for primitive types, interfaces, implementing and extending interfaces, and examples comparing the calculation of areas for shapes implementing a common interface.

Uploaded by

irshad17
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Strings

• String is sequence of character.


• In java string is a class objects and
implemented using two classes namely,
1. String
2. StringBuffer.
String class
Syntax :-

String stringname= new String (“strings”);

Example :-
String b = new String (“how are you?”);
String array Syntax:-
String arrayname[ ]=new String [size];
example:-
String k[ ] = new String[3];
public class string1
{
static String name[ ]={"Dubai","Tehran","Delhi","Bangkok","Singapur","Abu-
dhabi"};
public static void main(String u[])
{
int n=name.length;
String t=null;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if(name[j].compareTo(name[i]) <0 )
{
t=name[i];
name[i]=name[j];
name[j]=t;
}
}
}
for (int i = 0; i <n; i++)
{
System.out.println(name[i]);
}
} }
public class string2
{
public static void main(String p[])
{
/*1) Find the 5th character in the string " MOHAMMED HAMZA"
2) Find the index of character "K"in string "KALAMULLAH".
3) Convert the string "hello world"to uppercase.
4) Replace character 'h'with 'b'in the string"humble */
String no1 = "MOHAMMED HAMZA";
String no2 = "KALAMULLAH";
String no3 = "hello world";
String no4 = "humble";
//remember java indicies start at 0
System.out.println("\nCurrent String: " + no1);
System.out.println("The 5th character: " + no1.charAt(4));
System.out.println("\nCurrent String: " + no2);
System.out.println("The index of character k: " + (no2.indexOf("K") + 1) );
System.out.println("\nCurrent String: " + no3);
System.out.println("Convert string to uppercase: " + no3.toUpperCase());
System.out.println("\nCurrent String: " + no4);
System.out.println("Replace character h with b: " + no4.replace('h', 'b'));
}

}
public class StringArray {
    public static void main(String args[]) {
       String arr[ ] = {"Zero", "One", "Three"};  //initialization String Arrays

        System.out.println("String Array arr ...");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
        String arr1[] = new String[]{"Zero", "One", "Two"};
        System.out.println("\nString Array arr1...");
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
       String arr2[ ] = new String[3]; // This allocates the array but they are all null
         arr2[0] = "Zero";
        arr2[1] = "One";
        arr2[2] = "Two";
        System.out.println("\nString Array arr2...");
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}
• StringBuffer class:- It is peer class of String.
While String class creates strings of fixed
length,
• StringBuffer creates strings of flexible length
that can be modified in term of both length and
content.
• We insert character and substring in the middle
of the string or append another string to the
end.
// Stringbuffre
public class stringbuffre1
{
public static void main(String k[ ])
{
StringBuffer sb1 = new StringBuffer("welcome to ");
String b = "YIU";
sb1.append(b);
System.out.println(sb1);
char c1[ ] = new char[ ] {'Y','e','s'};
StringBuffer sb2 = new StringBuffer("Are you well ");
sb2.append(c1);
System.out.println(sb2);
boolean c = true;
StringBuffer sb3 = new StringBuffer("Hello World");
sb3.insert(6,c);
System.out.println(sb3);
StringBuffer sb4 = new StringBuffer("Hello World");
sb4.deleteCharAt(0);
System.out.println(sb4);
}
}
• In java Vector is a class of java.util package,
that is commonly used instead of array,
because they expand automatically when new
data is added to them.
• Vector can we declared without specified any
size explicitly.
• It can keep only objects.
• It can hold objects of any type and any
number
• Vector possess number of advantage over
array.
1. It is convenient to use vectors to store objects.
2. It can be used to store a list of objects that may vary in
size.
Wrapper classes
• As we know that vector in java can hold only
object; but can't directly store primitive data
types, like int, float, char, long, double.
therefore we need to convert simple data type
to object. This can we done using wrapper
classes.

• Wrapper classes are classes that are used


to make primitive data types into objects,
and to make wrapped objects into
primitives types
Primitive data type Wrapper class

byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Interface New Chapter
• An interface is similar to a class, but without
instance and static variables (static final
constants are allowed), and without method
bodies.
• Interface define abstract methods and final
field.
• Interfaces cannot be instantiated—they can
only be implemented by classes or extended by
other interfaces.
• It start with interface key word.
• When a class specifies that it implements an
interface, it must define all methods of that
particular interface.
• A class can be implement many different
interfaces. If a class doesn't define all methods
of the interfaces it agreed to define (by the
implements clause), the compiler gives an error
message
interface interfaceName
{
variable declaration;
method declaration;
}
Extending interfaces:- like class, interface may also
extends an other interface by using extends keyword.
interface abc extends xyz
{
body of abc interface;
}

interaface first1
{ int a=100;
String k=“book”;
}
interface second extends first1
{
void result();
}
• Subinterface can’t define the methods
declared in the superinterface.
• Interface can’t extends classes.
Implementing interfaces
• A class can inherit interfaces by using
implement keyword
class ABC implements intefacename
{
body of the ABC
}
A class can extends supereclass and also
can implement one or more than one
interfaces
class classname extends superclass
implements interface1,interface2,…
{
body of classname
}
// interfaces public class face1
interface Area2 {
public static void main(String k[])
{
{
final static float pi=3.14f;
rectangle ob1=new rectangle();
float process1(float a, float b); circle ob2=new circle();
} Area2 t; //interface object
class rectangle implements Area2 t=ob1;
{ float h1=t.process1(9, 20);
public float process1(float a, float b) System.out.println("Area of rectangle =
"+h1);
{
t=ob2;
return (a*b); float h2 =t.process1(7, 0);
} System.out.println("Area of Circle =
} "+h2);
class circle implements Area2 }
{ }

public float process1(float a, float b)


{
return (pi*a*a);
}
}

You might also like