- // 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}}
- // so i will use String in java and how add in other// string so and why i can't add other string in previous stringpublic 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 methodes1.concat("Gupta");System.out.println(s1);// their are no changes because this string store in scp (String constent poo)// and then use stringbuilder or StringbufferStringBuilder s2 =new StringBuilder("Rakesh");System.out.println(s2);// and then try to add a strings2.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}}
- // this code practise i will use .equals mehtodepublic 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 sameSystem.out.println(a.equals(b));// this statement is true because i am using equels methode// here no matter refrence here matter only valueSystem.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}}
- //something inbield methode on javapublic class string4 {public static void main(String[] args) {String rakesh = "Rakesh";//methode of convert to uper caseSystem.out.println(rakesh.toUpperCase());//methode of convert lower caseSystem.out.println(rakesh.toLowerCase());//find lenght of stringSystem.out.println(rakesh.length());// how to get character using index numberSystem.out.println(rakesh.charAt(4));//using concat methode add string one thing this is not change of string only addSystem.out.println(rakesh.concat("Gupta"));//replace of string variable name not changes in the stringrakesh = rakesh.concat("kumar");System.out.println(rakesh);}}
- //introduction of mutable string using string bufferpublic 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
- //introduction of mutable string using string builderpublic 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
- // we are use final keyword for stringpublic 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/stringbuilderfinal 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 possiblefinal 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}}
String
0
Tags