Introduction to Java Strings
What are Strings?
A String simply refers to a set of characters.....isn't it simply array of characters then?Yes, it is! But in java String is basically an object that represents a sequence of characters.An array of String works just like a string object
is as same as
Why use Strings?
One of the main objectives of a computer is to process real world data which includes name,address etc which is nothing but a sequence of characters. To handle these kinds of data and execute operations on them a Java programmer must know the inbuilt string class
String Classes
Java provides three String classes to handle Strings
i)java.lang.String //Immutable
ii)java.lang.StringBuffer //Mutable
iii)java.lang.StringBuilder //Mutable
We will see the properties of this class later.Let us now see how we can create a String in java.
There are many ways by which we can create a String in java
i)By using a String Literal
ii)By using another String Object
iii)By using new operator
iv)By concatenation(using + operator)
By using a String Literal :
A String literal is simply a set of characters enclosed within " ". We can create a String by assigning a String literal to a String instance.
What are Strings?
A String simply refers to a set of characters.....isn't it simply array of characters then?Yes, it is! But in java String is basically an object that represents a sequence of characters.An array of String works just like a string object
char[] ch={'j','a','v','a'}; String s=new String(ch);
String s="java";
Why use Strings?
One of the main objectives of a computer is to process real world data which includes name,address etc which is nothing but a sequence of characters. To handle these kinds of data and execute operations on them a Java programmer must know the inbuilt string class
String Classes
Java provides three String classes to handle Strings
i)java.lang.String //Immutable
ii)java.lang.StringBuffer //Mutable
iii)java.lang.StringBuilder //Mutable
We will see the properties of this class later.Let us now see how we can create a String in java.
There are many ways by which we can create a String in java
i)By using a String Literal
ii)By using another String Object
iii)By using new operator
iv)By concatenation(using + operator)
By using a String Literal :
A String literal is simply a set of characters enclosed within " ". We can create a String by assigning a String literal to a String instance.
String str1 = "Hello"; String str2 = "World";
By using another String Object:
String str1 = "Hello"; String str2 = new String(str
By using new operator:
String str3 = new String("Java");
By concatenation(using + operator)
String str1="Hello" String str2="World" String str3=str1+str2;
No comments:
Post a Comment