00:00
/* lenght + isEmpty + trim + validation + equals + equal ignore case*/ public class M02 { public static void main(String[] args) { /* lenght, empty object*/ String s2 = new String();//sop(s2.lenght) : 0 /* lenght, non empty object */ String s3 = new String("raj");//sop(s3.lenght) : 3 /* mutable - immutable*/ StringBuffer s4 = new StringBuffer("meerut"); String s5 = new String(s4); /* mutable - immutable*/ StringBuilder s6 = new StringBuilder("meerut"); String s7 = new String(s6); /* string ct + byte array, byte - character */ byte [] b = {101,102,103}; String s8 = new String(b);//sop(s8) : efg , /* char array - Character */ char [] c = {'a','b','c'}; String s9 = new String(c);//sop(s9) : abc /* password use */ char[] c1 = new char[]{'x','y','z'};//print obj., sop(c1) : [C@1dbd16a6 String s10 = new String("xyz"); //print string, sop(s10) : xyz
String s2a = " "; String s3a = " bull shit "; String s4a = ""; String s5a = null; String s6a = "deepak"; String s7a = "amit"; String s8a = "Deepak"; /* lenght : counts number of characters */ System.out.println("S3 = "+s2a.length()); // 2 System.out.println("S4 = "+s3a.length()); // 13 System.out.println("S4 = "+s4a.length()); // 0 System.out.println("S2 = "+s5a.length()); // java.lang.NullPointerException: /* Empty : JDK 1.6, returns true if string is empty */ System.out.println("S2 = "+s2a.isEmpty()); // false System.out.println("S3 = "+s3a.isEmpty()); // false System.out.println("S4 = "+s4a.isEmpty()); // true System.out.println("S4 = "+s5a); // Exception /* trim */ System.out.println("S2 = "+s2a.trim()); // System.out.println("S3 = "+s3a.trim()); //bull shit System.out.println("S4 = "+s4a.trim()); // System.out.println("S5 = "+s5a.trim()); //Exception /* equals : all characters matched, returns true */ System.out.println(s6a.equals(s7a)); // false System.out.println(s6a.equals(s8a)); // false /* equals Ignore Case */ System.out.println(s6a.equalsIgnoreCase(s8a)); // true /* validation - lenght */ if(s3a.trim().length()==0){ System.out.println("enter the details"); } /* validation - isEmpty */ if(s3a.trim().isEmpty()){ System.out.println("enter the details"); } /* validation - trim */ if(s3a.trim().length()==0){ System.out.println("enter the details"); }difnano /* validation - equals */ System.out.println(s2a.equals("")); // false } }
/* compair + compairTo, lexicographically compair string -> converted Unicode */ public class M02 { public static void main(String[] args) { /* compair to A=65, a=97 */ String s1 = "a"; // 97 String s2 = "A"; // 65 System.out.println(s1.compareTo(s2)); // 32 System.out.println(s2.compareTo(s1)); // -32 /* compair to A=65, a=97 */ String s3 = "abc"; String s4 = "Abc"; System.out.println(s3.compareTo(s4)); // 32 /* compair to B=66, b= 98*/ String s5 = "abc"; String s6 = "aBc"; System.out.println(s5.compareTo(s6)); // 32 /* compair to ignore case */ System.out.println(s5.compareToIgnoreCase(s6)); // 0 /* finding the lenght */ String s7 = "b"; String s8 = ""; System.out.println(s7.compareTo(s8)); // 1, always /* chart link https://www.w3schools.com/charsets/ref_html_ascii.asp */ } }
/* concatinate, substring, subsequence, replace, replaceAll, repalceFirst */ public class M02 { public static void main(String[] args) { String s1 = "- - -"; String s2 = "CCC"; String s3 ="1234567890"; String s4 ="12345 12345"; /* + mt */ System.out.println(s1+s2); //- - -CCC System.out.println(s1+10); //- - -10 System.out.println(s1+10+20); //- - -1020 System.out.println(10+20+s1); //30- - - System.out.println(10+s1+20); //10- - -20 System.out.println(s1+20/10); //- - -2 System.out.println(s1+20*10); //- - -200 System.out.println(s1+10-5); // java.lang.Error: Unresolved compilation problem /* concat */ System.out.println(s1.concat(s2)); //- - -CCC /* join m/T, static m/T,JDK 1.8, delimiter is null -> java.lang.NullPointerException*/ System.out.println(String.join(",",s1,s2)); // - - -,CCC /* subSequence , return character */ System.out.println(s3.subSequence(2, 4)); // 34 System.out.println(s1.subSequence(0, -1)); // outof bound System.out.println(s3.subSequence(0, 0)); // /* subSequence , return string */ System.out.println(s4.substring(3)); // 45 12345 System.out.println(s4.substring(2,6)); // 345 /* replace , return string */ System.out.println(s4.replace("23", "XY")); // 1XY45 1XY45 /* replaceFirst , return string */ System.out.println(s4.replaceFirst("23", "--")); // 1--45 12345 System.out.println(s4.replaceAll("23", "**")); // 1**45 1**45 /* replaceall with regex */ System.out.println(s4.replaceAll("3(.)", "#")); // 12#5 12#5 System.out.println(s4.replaceAll("4(.*)", "^")); // 123^ /* indexOf() */ System.out.println(s4.indexOf('2')); // 1 /* lastindexOf() */ System.out.println(s4.lastIndexOf('2')); // 7 /* charAt() */ System.out.println(s4.charAt(2)); // 3 /* contains() */ System.out.println(s4.contains("3")); // true /* startsWith() */ System.out.println(s4.startsWith("1")); // true /* endswith() */ System.out.println(s4.endsWith("5")); // true /* toupperCase() */ String s5 = "momoj"; String s6 = "KAPIL"; String s7 = "meerut"; System.out.println(s5.toUpperCase()); /* toLowerCase */ System.out.println(s6.toLowerCase()); /* valueOf */ int a =10; String x = String.valueOf(a); // static m/T System.out.println(x); //10 /* toCharArray */ char[]ss = s7.toCharArray(); // string to character conversion System.out.println(ss); // meerut System.out.println(ss[1]); // e } }
/* StringBuffer c/L m/T */ public class M02 { public static void main(String[] args) { /* it create an obj, which can store 16 characters*/ StringBuffer s1 = new StringBuffer(); // sop s1.capacity() : 16 /* with string argu. = capacity + string-length */ StringBuffer s2 = new StringBuffer("Rajat"); // sop s2.capacity() : 21 /* pre defind capacity */ StringBuffer s3 = new StringBuffer(1000); // sop s3.capacity() : 1000 StringBuffer s4 = new StringBuffer(); /* append less than 16 char */ s4.append("java"); // sop s4.capacity() : 16 /* append = 16 char */ StringBuffer s5 = new StringBuffer(); s5.append("his is java code"); System.out.println(s5.capacity()); //16 /* append more than 16 char, (old capacity + 1 ) * 2 */ StringBuffer s6 = new StringBuffer(); s6.append("This is java code"); System.out.println(s6.capacity()); //34 s6.append("This is java code"); System.out.println(s6.capacity()); //34 /* append more than 32 char, (old capacity + 1 ) * 2 */ StringBuffer s7 = new StringBuffer(); System.out.println(s7.capacity()); // 16 s7.append("This is java code This is java code"); System.out.println("s7.capacity= "+s7.capacity()); // 35 s7.append("Rajat"); System.out.println(s7.capacity()); // 72 // wo jaise he default capacity exceed karta hai , wo nahe capacity banayega jiska // formula hoga (old capacity + 1) * 2 // (16+1)*2 /* lenght m/T */ StringBuffer sl1 = new StringBuffer("rajat"); System.out.println(sl1.length()); //5 /* append m/T */ StringBuffer sl2 = new StringBuffer("rajat"); System.out.println(sl2.append("book")); //rajatbook System.out.println(sl2.charAt(3)); //a /* delete m/T */ StringBuffer sd1 = new StringBuffer("rajat is my name"); System.out.println(sd1.delete(2, 4)); //rat is my name /* delete char at */ StringBuffer sd2 = new StringBuffer("rajat is my name"); System.out.println(sd2.deleteCharAt(3)); //rajt is my name /* equals */ // StringBuffer class does not override equals method of Object class but // String class override the equals method of Object class StringBuffer se1 = new StringBuffer("rajat is my name"); StringBuffer se2 = new StringBuffer("rajat is my name"); System.out.println(se1.equals(se2)); // diff. obj , false StringBuffer se3 = se1.append("hi"); System.out.println(se1.equals(se3)); // same obj., true /* indexOf + lastIndexOf */ StringBuffer si1 = new StringBuffer("rajat is my name"); System.out.println(si1.indexOf("j")); //2 System.out.println(si1.lastIndexOf("m")); //14 /* insert */ StringBuffer si2 = new StringBuffer("rajat is my name"); System.out.println(si2.insert(3,"bok")); //rajbokat is my name /* replace */ StringBuffer sr = new StringBuffer("rajat is my name"); System.out.println(sr.replace(3,4,"X")); //rajXt is my name /* reverse */ StringBuffer sr1 = new StringBuffer("rajat is my name");//sr1.reverse()) : eman ym si tajar /* subSequence */ StringBuffer ss1 = new StringBuffer("rajat is my name"); System.out.println(ss1.subSequence(3,5)); //at /*substring*/ StringBuffer ss2 = new StringBuffer("rajat is my name"); System.out.println(ss2.substring(5)); // is my name /*substring */ StringBuffer ss3 = new StringBuffer("rajat is my name"); System.out.println(ss3.substring(3,6)); //at StringBuffer ss4 = new StringBuffer(); System.out.println(ss4.capacity()); //16 ss4.ensureCapacity(1000);//sop-ss4.capacity() : 1000 /* setCharAt */ StringBuffer ss5 = new StringBuffer("this is java code"); ss5.setCharAt(3,'A'); System.out.println(ss5); //thiA is java code /* set lenght */ StringBuffer ss6 = new StringBuffer("this is java code"); ss6.setLength(5); //fixed lenght of ss6 System.out.println(ss6); //this /* ensure capacity */ StringBuffer ss7 = new StringBuffer(); ss7.ensureCapacity(100); /* append */ ss7.append("book"); //fixed lenght of ss6 System.out.println(ss7.capacity()); //100 /* trim to size */ ss7.trimToSize(); System.out.println(ss7.capacity()); // 4 } }
/* M03- code Flow */
K12.png
M02.png public class M03 { public static void main(String[] args) { String s1 = new String("Rajat"); // 2 object heap , SCP // create s1 name variable points rajat object in heap. // another object in SCP for future reference String s2 = "Amit"; // create Amit in SCP only String s3 = new String( "Deepak"); // create Deepak in heap and in SCP, unique String s4 = new String( "Rajat"); // create Rajat in heap and Not in SCP, already, 1 obj String s5 = "monika"; // create monika in SCP only, s5 points to it String s6 = "Rajat"; // create 0 object, points obj Rajat in SCP only, JVM Stop refer internally String s7 = "Rajat"; // create 0 object, again points obj Rajat in SCP only, // SCP will not create same name literals in it. // if user create it referes to the same literals. } }
/* M03 - string imuutibility ( Cannot Change) */
M03.png
public class M03 { public static void main(String[] args) { String s1 = new String("Deepak"); System.out.println(s1); // Deepak System.out.println(s1.concat("java")); // Deepak java, s1=s1.concat("python"); //same s1 points to another object System.out.println(s1); // Deepak python } } /* 1. Immutability concept -> is used for "String Objects" i.e. String objects are immutable. -> Once Created, its data/state can't be changed but a -> new string object is created. 2. String s1 = new String("Deepak"); // 2 object, heap,scp, JMV refer - SCP's object 3. s1.concat("java"); -> Will not change the value. Create new literals -> save = "Deepak java" in heap -> and java in SCP area, -> original value will not change 4. s1 = s1.concat("python"); -> create python in SCP, JVM's internal variable refers it -> create "Deepak python" in heap area -> and s1 now pointing to Deepak python , -> but original value ("Deepak") will not change. 5. what is the use String city1 = "delhi"; String city2 = "delhi"; String city3 = "delhi"; String city4 = "delhi"; String city5 = "delhi"; 6. all points to delhi in SCP's area. let person 1 is move to goa , tab keywal ek ki city he change hogi baki logo ki city par koi farak naeh padna chaheye. isee karan se string immutable hote hai. Strings are Immutable in Java because String objects are cached in String pool. Since cached String literals are shared between multiple persons there is always a risk, where one persons's action would affect all another persons For example, if one person changes its city from "Mohali" to "Delhi" all then other persons will also get affected. */
/* M05 diff. == and equalto*/ public class M05 { public static void main(String[] args) { String s1 = new String("rajat");// in heap and SCP String s2 = new String("rajat");// in heap area only System.out.println(s1==s2);// false // s1 refer to first rajat in heap // s2 refer to second rajat // == checkes the address, reference comparision String s3="manoj"; // in SCP area String s4="manoj"; // try to make but already present then System.out.println(s3==s4);// true, coz both same location // equalsto is the object class m/T, string overrides it System.out.println(s1.equals(s2));// true System.out.println(s3.equals(s4));// true System.out.println(s1.equals(s3));// false } } /* 1. == -> reference/address comparison. -> means both objects point to the same memory location or not. 2. equals -> content comparison (in String class). -> means check object value. 3. equals (of object class ) -> compare the reference/address of two objects -> i.e if two objects point to the same memory location or not. 4. equals (of string class ) -> equals method is override by string class Object { public boolean equals (Objects obj) { return (this==obj); } } class String extends Object { public boolean equals (Objects obj){ // statements } } */
Home PREVIOUS NEXT