Object Oriented Programming With Java: Department of Ce/It Unit-2 Array & String OOPJ (01CE0403)
Object Oriented Programming With Java: Department of Ce/It Unit-2 Array & String OOPJ (01CE0403)
CE/IT
Java
Arrays
type var-name[ ]; // OR type[ ] var name;
int month_days[];
array-var = new type[size];
One-
month_days = new int[12];
Dimensional
Arrays Example:
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };
1. int array1[] = {5,10,15,20,25};
2. int array1[] = new int[5];
for(j=0;j<5;j++)
array1[j] = j+5;
Initialization 3. int array1[] = new int[5];
of Array array1[0]=5;
array1[1]=10;
array1[2]=15;
array1[3]=20;
array1[4]=25;
class Testarray{
public static void main(String args[]){
Multi-
dimensional
Arrays
Jagged Array
Three
Dimensional
Array
The java.lang.String class provides a lot of methods to work
on string.
By these methods, we can perform operations on string such
as trimming, concatenating, converting, comparing,
replacing strings etc.
Java implements strings as objects of type String class
Once a String object is created it cannot be changed. Stings
are Immutable.
String Class
To get changeable strings use the class called StringBuffer.
String anyString = null;
Array of String:
String names[ ] = new String[5];
Initialization of array of String:
String names[ ]= {“Red”, “Orange”, “Yellow”, “Green”,
“Blue”, “Indigo”, “Violet”};
1. Default Constructor:
String str = new String( );
String Class
Constructors 2. String object using an array of Characters
String(char charArray[ ] )
char Name[ ] = {‘J’, ‘A’, ‘V’, ‘A’, ‘P’, ‘R’, ‘O’, ‘G’};
String strName = new String(Name);
3. String Object Initialized by range from char array
String(char charArray[ ], int startindex, int
no_of_char)
char Name[ ] = {‘J’, ‘A’, ‘V’, ‘A’, ‘P’, ‘R’, ‘O’, ‘G’};
String strName = new String(Name, 3, 3);
String Class
Constructors 4. String object using another String Object:
String (String strObject);
char Name[ ] = {‘J’, ‘A’, ‘V’, ‘A’, ‘P’, ‘R’, ‘O’, ‘G’};
String strName = new String(Name);
String strName2 = new String(strName);
String Literals:
char Name[ ] = {‘J’, ‘A’, ‘V’, ‘A’, ‘P’, ‘R’, ‘O’, ‘G’};
String Class String strName = new String(Name);
OR
String strName = “JAVAPROG”;
The length() method returns the length of the string.
Ex: System.out.println(“Hello”.length()); // prints 5
The + operator is used to concatenate two or more strings.
String Eg: String myname = “Harry”;
Operations String str = “My name is” + myname+ “.”;
For string concatenation the Java compiler converts an
operand to a String whenever the other operand of the + is a
String object.
Characters in a string can be extracted in a number of ways.
public char charAt(int index)
char ch;
ch = “abc”.charAt(1); // ch = “b”
getChars() - Copies characters from this string into the
destination character array.
public void getChars(int srcBegin, int srcEnd, char[] dst,
int dstBegin)
String Buffer
length() - Returns the length of this string buffer.
boolean Boolean
byte Byte
char Character
short Short
Wrapper Class
int Integer
long Long
float Float
double Double
void Void
Each wrapper class implements methods specific to each
data type except void class.
Common methods implemented in all wrapper class:
ClassType(type)
Wrapper Class type typeValue( )
int hashCode( )
String toString( )
boolean equals(Object obj)
static boolean valueOf(String s)
1. ClassType(type):
Ex. Character c1 = new Character(‘x’);
2. typeValue( ):
Ex. char c2 = c1.charValue( );
Common
3. toString( ):
Methods in Ex. System.out.println(c1.toString( ));
Wrapper Class 4. equals( ):
Ex. S1.equals(s2);
5. valueOf( ):
Ex. Byte b1= valueOf(“101”,2);
Boolean Class:
static boolean getBoolean(String name)
Source: javapoint.com
Thank you