THE CONCEPT CLASSES ...initiated by DEYS IIT- JEE Pre for Classes VII ~ X (ICSE) Java String Manual String Constructor 1) String s; // s is declared as a variable of String class, but object not created. s = “ABC”; // objected created and initialized 2) String s = new String(); 3) String s1= “ABC”, s2= new String(); 4) String s = new String(“ABC”); 5) String s1 = “abcxyz”; String s2 = new String(s1); = > s2 =”abcxyz”; common error String s = new String(„a‟); // Error String Concatenation 1) String s = “abc”+”xyz”; = > s = “abcxyz” 2) String s1 = “abc”; String s = s1+“xyz”; = > s = ”abcxyz” 3) String s = “abc”+2+2; = > s = “abc22”; 4) String s = “abc”+(2+2); = > s = “abc4” 5) String s1 = “One”; String s2 = “Ten”; s2=s1.concat(s2); = > s2 = “OneTen” 6) String s = new String( ) ; s = s + “abc”; // No Error as object s has already created and stores null value. CONCEPT common error String s; s = s + “abc” // No object has been created for s // Error charAt() ->char charAt(int index) String s = “abc”; char c= s.charAt(0); String Comparison 1) equals() => c = ‟a‟ -> boolean equals(String) 2) euualsIgnoreCase() -> boolean equals(String) String s1 = “abc”, s2 = “AbC”; s1.equals(s2) = > false s2.equalsIgnoreCase(s1) = > true 3) startsWith() / endsWith() - > boolean startsWith(String s) - > boolean startsWith(String s, int startIndex) - > boolean endsWith(String s) String s = “The Concept Classes …initiated by DEYS”; s.startsWith(“The Con”) = > true s.startsWith(“the”) = > false s.startsWith(“Concept”,4) s.endsWith(“DEYS”) 4) equals() // Compares characters String s1 =”abc”; String s2 = new String(s1); String s3 =”ABC”; String s4 =s1; s1.equals(s2) s1.equals(s3) s1.equals(s4) s1 = = s4 s1 = = s2 = > true = > true == // Compares object references CONCEPT = > true = > false = > true = > true = > false //Compares characters //Compares characters //Compares characters //Compares object reference //Compares object reference 5) compareTo() - > int compareTo(string s) String s1 = “ABC” , s2 =”ABCxyz” , s3 =”ABCxyz7”; String s4 =”ABcxyz7” , s5 =”Adgaf”; s1.compareTo(s2) = > -3 // Difference in number of characters s1.compareTo(s3) = > - 4 // Difference in number of characters s2.compareTo(s1) = > 3 // Difference in number of characters s1.compareTo(s4) = > -32 // Difference in UNICODE of characters of 1st diff. s4.compareTo(s1) = > 32 // Difference in UNICODE of characters of 1st diff. s4.compareTo(s5) = > -34 // Difference in UNICODE of characters of 1st diff. s1.compareTo(s1) = > 0 // Difference in UNICODE of characters of 1st diff. 6) compareToIgnoreCase() - > int compareToIgnoreCase(String s) Completely same as compareTo() only ignores any change in case. 7) indexOf() -> int indexOf( int ch) // if ch is char implicit type casting will take place to int -> int indexOf( String str) // searches the first occurrence of the str -> int indexOf( int ch, int startIndex) -> int indexOf(String str, int startIndex) String s=”The Concept Classes …initiated by DEYS”; s.indexOf(„e‟) = >2 s.indexOf(67) = >4 s.indexOf(„e‟,4) = >8 s.indexOf(“once”) = >5 s.indexOf(“once”,4) = >5 s.indexOf(“once”,6) = >-1 // if search fails -1 is returned s.indexOf(“once”,5) = >5 s.indexOf(“Once”) = >-1 8) lastIndexOf() -> int lastIndexOf( int ch) // if ch is char implicit type casting will take place to int -> int lastIndexOf( String str) // searches the last occurrence of the str -> int lastIndexOf( int ch, int startIndex) -> int lastIndexOf(String str, int startIndex) String s=”The Concept Classes …initiated by DEYS”; s.lastIndexOf(„C‟) = >12 s.lastIndexOf(67,5) = >4 s.lastIndexOf(„o‟,4) = >-1 // if search fails -1 is returned s.lastIndexOf(“once”) = >5 s.lastIndexOf(“once”,3) = >-1 s.lastIndexOf(“once”,5) = >5 Modifying Strings 1) substring() -> String substring(int startIndex) // Note : s for string in substring is small -> String substring(int startIndex, int endIndex) String s = “Delhi is the capital of India”; s.substring(2) = >”lhi is the capital of India” s.substring(6,10) = >”is t” // endIndex is not included 2) replace() - > String replace(char old, char new) String s1 = “Java is platform independent language”; String s2 = s1.replace(„a‟,‟e‟); = > s2 = “Jeve is pletform independent lenguege” 3) length() -> int length() String s =”India and Asia”; int len = s.length() 4) trim() -> String trim() String s =” India “; int len = s.length(); s=s.trim(); len = s.length(); = > len = 14 => len = 11 = > s = “India” => len = 5 5) toUpperCase() / toLowerCase() - > String toUpperCase() - > String toLowerCase() String s = “aVg+e-H*g/5! #$%^&*()”; String s1 =s.toUpperCase(); => s1= AVG+E-H*G/5! #$%^&*()” String s2 =s.toLowerCase(); => s2 = “avg+e-h*g/5! #$%^&*()” 6) valueOf() Vs toString() valueOf( ) is a static method of String class which can be used to converts primitive data type to string. toString( ) are static methods of all wrapper classes that can be used to converts primitive data type to string. -> String valueOf(anyPrimitiveLiteral/Variable) -> String toString(correspondingPrimitiveLiteral/Variable) int x =89; String s1 = String.valueOf(x); = > s1 = “89” String s2 = String.valueOf(123); = > s2 = “123” String s3 = Integer.toString(x); = > s3 = “89” String s1 = Integer.toString(123); = > s4 = “123” Every object implements toString( ) method. It is a method which returns a string of the object. class Test { int x,y,z; Test (int a, int b) { x=a; y=b; } void cal() { z=x+y; } CONCEPT public String toString() { return ” Sum of “ + x +” and “ + y + “ is “ + z; } } CONCEPT class TestToString { public static void main(String arg[]) { Test t1 = new Test(3,4); t1.cal( ); Test t2 = new Test(7,8); t2.cal( ); String s = “”+ t2; //* System.out.println(t1); System.out.println(s); } } Output: Sum of 3 and 4 is 7 Sum of 7 and 8 is 15 * Note: Directly t2 can‟t be assigned to String s as it will give reference to String object which does not match on type. IIT JEE Selections 2014 Harsh Sahu Ashish Singh IIT Delhi – Mechanical IIT Dhanbad – Mining Kapil Singh Aadil Hayat Pallavi Sahu IIT Roorkee – Computer Science IIT K – Aero Space IIT Roorkee – Geo Physics Gautam Pratap Singh (Jhansi Topper) Gurjyot Singh Sethi IIT K - Mechanical IIT K - Civil Vaibhav Agarwal (Jhansi Topper) Himanshu Sahu Ayush Nigam IIT D – Mechanical IIT Raj – Mechanical ISM Dhanbad – Petro Chemical 2013 2012 2011 IIT JEE Pre for classes VII ~ X Physics, Chemistry, Mathematics, Biology & Computer Address : The Concept Classes …initiated by DEYS, Near Ashik Chauraha, Basement Archana Kirana Store, Antia Tal Road, Jhansi. Mr. A.K. DEY(MCA) Mob: 9451520537
© Copyright 2024