String

0


  1. // you have two type of string just like a string is
    // normal and one string is declare using new keyword so both
    //  of string save on heap memory and without new keyword use
    //  string save in heap memory and one memory in the heap memory scp (string constant pool)


    public class string1 {
        public static void main(String[] args) {
            String a= "Rakesh";
            String b= "Rakesh";
            System.out.println(a==b);

            // out is true because a and b same same value adress
            // output is very clear true because this value store in
            // scp(string constant pool)

            String c = new String("Rakesh gupta");
            String d = new String("Rakesh gupta");
            System.out.println(c==d);

            // out put is false because stored in heap memory and heap memory can always be change  
           
        }
    }

  2. // so i will use String in java and how add in other
    // string so and why i can't add other string in previous string

    public class string2 {
        public static void main(String[] args) {
            String s1= "Rakesh";
            System.out.println(s1);

            // output is rakesh because and
            // then try add a other string in previous string
            // for adding string i will use .cancat methode

            s1.concat("Gupta");
            System.out.println(s1);
             
            // their are no changes because this string store in scp (String constent poo)

            //  and then use stringbuilder or Stringbuffer
           
            StringBuilder s2 =new StringBuilder("Rakesh");
            System.out.println(s2);

            // and then try to add a string
             s2.append ("Gupta");
             System.out.println(s2);

            //  and here output is sure add and you one thing
            // always remember concat methode not work  with String
            // builder refrences and here is append methode you follow this
            // blog offcouse i share on this blog all thing about java  
    }
       
    }

  3. // this code practise i will use .equals mehtode

    public class string3 {
        public static void main(String[] args) {

            String a = "rakesh";
            String b = new String("rakesh");
            String c = new String("RAKESH");
            System.out.println(a==b);
            // this is statement false because one string is only
            // heap memory and one string is scp so never would be same


            System.out.println(a.equals(b));
            // this statement is true because i am using equels methode
            // here no matter refrence here matter only value



            System.out.println(b.equalsIgnoreCase(c));
            // this statement is true because here i am
            // using equalsIgnoreCase methode and if i use
            // only .equal mehode so here bollean out is false
           
        }
       
    }


  4. //something inbield methode on java
    public class string4 {
        public static void main(String[] args) {
            String rakesh = "Rakesh";
            //methode of convert to uper case
            System.out.println(rakesh.toUpperCase());
            //methode of convert lower case
            System.out.println(rakesh.toLowerCase());
            //find lenght of string
            System.out.println(rakesh.length());
            // how to get character using index number
            System.out.println(rakesh.charAt(4));
            //using concat methode add string one thing this is not change of string only add
            System.out.println(rakesh.concat("Gupta"));
            //replace of string variable name not changes in the string
            rakesh = rakesh.concat("kumar");
            System.out.println(rakesh);
           
        }
       
    }
  5.  
    //introduction of mutable string using string buffer

    public class string5 {
        public static void main(String[] args) {
            StringBuffer rakesh = new StringBuffer("Rakesh");
            rakesh.append("Gupta");
            System.out.println(rakesh);
        }
       
    }

    //ouput is " RakeshGupta " and remember one thing
    //this opration perfome same object refrance
  6. //introduction of mutable string using string builder

    public class string6 {
        public static void main(String[] args) {
            StringBuilder rakesh = new StringBuilder("Rakesh");
            rakesh.append("Gupta");
            System.out.println(rakesh);
        }
       
    }

    //ouput is " RakeshGupta " and remember one thing
    //this opration perfome same object refrance
    //stringbuilder and and stringbuffer isn't same we leaen in multithreading
    //follow this java blog


  7. // we are use final keyword for string
    public class string7 {
        public static void main(String[] args) {
            //when we are use final keyword so only effect on refrance
            //here not change beacause we are use strinbuffer/stringbuilder
            final StringBuffer rakesh = new StringBuffer("rakesh");
            rakesh.append( "Gupta");
            System.out.println(rakesh);
            // but this statement is not valid beacause we are try to change
            // create new variable adress so this is always false

            // rakesh= new StringBuffer("Sony");
            System.out.println(rakesh);

            String rakesh1 = new String("amit");
            rakesh1 = new String("Rahul");
            System.out.println(rakesh1);
            // but when use final keyword its not working because
            // we are try to change variable adrees and using final keyword
            // its not possible
             final String rakesh2 = new String("preeti");
            rakesh1 = new String("farhat");
            System.out.println(rakesh2);
            //here are not changes kepp try to changes apply this code on codeeditor
        }
       
    }

Post a Comment

0Comments
Post a Comment (0)