OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd Java Characters: Normally, when we work with characters, we use primitive data types char. Example: char ch ='a'; Character Methods: isLetter( ) Determines whether the specified char value is a letter. isDigit( ) Determines whether the specified char value is a digit. isWhitespace( ) Determines whether the specified char value is white space. isUpperCase( ) Determines whether the specified char value is uppercase. isLowerCase( ) Determines whether the specified char value is lowercase. toUpperCase( ) Returns the uppercase form of the specified char value. toLowerCase( ) Returns the lowercase form of the specified char value. toString( ) Returns a String object representing the specified character value that is, a one-character string. All these Methods call after character.Method public class Test { public static void main(String args[]) { System.out.println(Character.isLetter('c')); System.out.println(Character.isLetter('5')); } } This produces the following result: true false public class Test { public static void main(String args[]) { System.out.println(Character.isDigit('c')); System.out.println(Character.isDigit('5')); } } This produces the following result: false true public class Test{ public static void main(String args[]){ System.out.println(Character.isWhitespace('c')); System.out.println(Character.isWhitespace(' ')); System.out.println(Character.isWhitespace('\n')); Page 34 OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd System.out.println(Character.isWhitespace('\t')); } } This produces the following result: false true true true public class Test{ public static void main(String args[]){ System.out.println( Character.isUpperCase('c')); System.out.println( Character.isUpperCase('C')); System.out.println( Character.isUpperCase('\n')); System.out.println( Character.isUpperCase('\t')); } } This produces the following result: false true false false public class Test{ public static void main(String args[]){ System.out.println(Character.isLowerCase('c')); System.out.println(Character.isLowerCase('C')); System.out.println(Character.isLowerCase('\n')); System.out.println(Character.isLowerCase('\t')); } } This produces the following result: true false false false public class Test{ public static void main(String args[]){ System.out.println(Character.toUpperCase('c')); System.out.println(Character.toUpperCase('C')); } } This produces the following result: C C Page 35 OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd public class Test{ public static void main(String args[]){ System.out.println(Character.toLowerCase('c')); System.out.println(Character.toLowerCase('C')); } } This produces the following result: c c Java Strings: Strings which are widely used in Java programming are a sequence of characters. In the Java programming language, strings are objects. Creating Strings: The most direct way to create a string is to write: String greeting ="Hello world!"; String Length: length ( ) method returns the number of characters contained in the string object. publicclassStringDemo{ public static void main(String args[]){ String s ="second class"; int len = s.length(); System.out.println("String Length is : "+ len ); } } This would produce the following result: String Length is :12 String Methods: Page 36 char charAt (int index) Returns the character at the specified index. String concat(String str) Concatenates the specified string to the end of this string. int indexOf(int ch) Returns the index within this string of the first occurrence of the specified character. int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. int length( ) Returns the length of this string. String replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. String toLowerCase( ) Converts all of the characters in this String to lower case using the rules of the default locale. String toUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale. OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd public class Test { public static void main(String args[]) { String s = "Strings are immutable"; char result = s.charAt(8); System.out.println(result); } } This produces the following result: a public class Test { public static void main(String args[]) { String s = "Strings are mutable"; s = s.concat(" all the time"); System.out.println(s); } } This produces the following result: Strings are mutable all the time public class Test { public static void main(String args[]) { String Str = "Tutorials "; System.out.print("Found Index :" ); System.out.println(Str.indexOf( 'o' )); } } This produces the following result: Found Index :4 public class Test{ public static void main(String args[]){ String Str = "Welcome to Tutorialspoint.com"; System.out.print("Return Value :" ); System.out.println(Str.replace('o', 'T')); System.out.print("Return Value :" ); System.out.println(Str.replace('l', 'D')); } } This produces the following result: Return Value :WelcTme tT TutTrialspTint.cTm Return Value :WeDcome to TutoriaDspoint.com Page 37 OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd public class Test{ public static void main(String args[]){ String Str = "Welcome to Tutorialspoint.com"; System.out.print("Return Value :"); System.out.println(Str.toLowerCase()); } } This produces the following result: Return Value :welcome to tutorialspoint.com public class Test{ public static void main(String args[]){ String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.toUpperCase() ); } } This produces the following result: Return Value :WELCOME TO TUTORIALSPOINT.COM Object Oriented Concepts 1.1. Mutability All objects either are immutable or mutable. the state of an immutable object never changes, while the state of a mutable object can change. String object are mutable , for example String methods have a concatenation operator +, but is does not modify either of its argument, instead, it returns a new string whose state is the concatenation of the states of its arguments. If we did the following assignment to t with the state shown in Figure (b). t=t + "g"; the results as shown in figure (c) is that t now refers to an a new String object whose state is "abcdefg" and the object referred to by s is unaffected. Mutable object Page 38 OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd On the other hand, arrays are mutable. The assignment a[i]=e/// where e element is equal 6 causes the state of array a to change by replacing its ith element with the value obtained by evaluating expression e ( the modification occurs only if i is in bounds for a, an exception is thrown otherwise). Discussion Note If a mutable object is shared by two or more variables, modifications made through one of the variables will be visible when the object is used through the other variable. Example Suppose the shared array in figure (b) is modified by b[0]=i; // i= 6 before this causes the zeroth element of the array to contain 6 ( instead of 1 it used to contain ) , as shown in figure (c) Mutable object 1.2. Method Call Semantic An attempt to call a method as in this form : e. m (), // where e is a representative of a class and m is a method first evaluates e to obtain the class of object whose method is being called . Then the expression for the arguments are evaluated to obtain actual parameter values المتغيرات الفعلية. This evaluation happen left to right. Next an activation record is created for the call and pushed onto the stack; the activation record contains room for the formal parameters المعامالت الشكليةof the method ( the formals are the variables declared in the method header) and any other local storage the method required. Page 39 OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd When the actual parameter are assigned to the formals , this kind of parameters passing is called "call by value" and Finally control is dispatched to the called method e.m. Note: if the actual parameters value is a reference to an object, that reference is assigned to the formal. this means that the called procedure shares object with its caller. Furthermore, if these object are mutable, and the called procedure change their state, these changes are visible to the caller when it returns. Example 1 And in the main class, we define And we call the method The results in the Stack storage are: After return form Method Method Call Page 40 After return form multiples Method OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd Example 2 Example 3 public class CallsByRefers { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub CallsByRefers cc=new CallsByRefers(); int [] b= {0,2,3,4}; int kl=9; Page 41 OOP with Java Dr. Ahmed M. Al-Salih University of Babylon/ College of IT class – First Semester- Department of Software 2nd cc.change(b, kl); for (int i=0; i<b.length;i++) { System.out.println(b[i]); } System.out.println("The Value of kl after return from method "+kl); } public int change (int [] a, int j){ int i = j*2; System.out.println("The value of i inside the methdo "+i); for (int k=0; k<a.length;k++) { a[k]=a[k]*2; } return j; }// End Main Method }// End CallsByRefers Class The Output for Example 2 is :The value of i inside the method 18 0 4 6 8 The Value of kl after return from method 9 Quiz #2 Page 42