String in JAVA - The java.lang.String class is used to create a string object.
- It is possible to create string object in two ways
- //creating a string obj without using new operator.
String str1=“Ratan”; String str2=“bharath”; String str3=“Ratan”; str1,str3 …….. Ratan str2 ……….Bharath - In scp memory just before object creation it is always checking previous objects.
If the previous object is not available it will create the new object. If the previous object is available with the same content then the reference variable pointing to existing object. //creating a string obj by using new operator. - //creating a string obj by using new operator.
String str1=“Ratan”; String str2=“bharath”; String str3=“Ratan”; When we create object in heap area instead of checking previous objects it creates directly new object str1 …….. Ratan str2 ……….Bharath str3 ………Ratan Example of “String” Program 10 - class StringExample{
- public static void main(String args[]){
- String s1=“bharath";
- System.out.println(s1);
- String s2=new String(“bharath”);
- System.out.println(s2);
- }
- }
- The Java String class length() method finds the length of a string. For example,the length of a string can be found with length() method.
- public class Length{
- public static void main(String args[]){
- String s1=“Hello";
- String s2=“Helloworld";
- System.out.println(s1.length());
- System.out.println(s2.length());
- }
- }
Example of “String Length” Program 11
Do'stlaringiz bilan baham: |