2. In java, string is basically an object that represents
sequence of char values. An array of characters works
same as java string. For example:
char[] ch={'j','a','v','a','_','p','o','i','n','t'};
String s=new String(ch);
is same as:
String s="java_point";
Java String class provides a lot of methods to perform
operations on strings such as compare(), concat(), equals(),
split(), length(), replace(), compareTo(), intern(), substring() etc.
3. PROGRAMMING CODES
•Char-character
•Public-is a java keyword which declares a
member's access as public. Public members are
visible to all other classes.
•Public members can be accessed from the child class
of the same package. Private members cannot be
accessed from the child class of the same package.
4. PROGRAMMING CODES
• Void is a return type used for functions in C++.It is used when the
function is not going to return any value. For example:-
lets design a program that just print numbers 1 to n.
Void printnaturalnos(int n){
for(int i=0;i<n;i++){
cout<<i<<" ";
}
}
since ,this function is not returning any value therefore,we have takes its
return type as void.
5. WHAT IS STRING IN JAVA?
• generally, string is a sequence of characters. but in java,
string is an object that represents a sequence of
characters. the java.lang.string class is used to create a
string object.
• Strings are used for storing text.
A String variable contains a collection of characters
surrounded by double quotes:
6. In Java, a string is a sequence of characters. For
example, "hello" is a string containing a sequence of
characters 'h', 'e', 'l', 'l', and 'o'.
We use double quotes to represent a string in Java.
For example,
// create a string
String type = "Java programming";
7. EXAMPLES
Example
Create a variable of type String and assign it a value:
String greeting = "Hello";
public class Main {
public static void main(String[] args) {
String greeting = "Hello";
System.out.println(greeting);
}
}
8. STRING LENGTH
A String in Java is actually an object, which contain
methods that can perform certain operations on strings.
For example, the length of a string can be found with
the length() method:
Example
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " +txt.length());
9. public class main {
public static void main(string[] args) {
string txt = "abcdefghijklmnopqrstuvwxyz";
system.out.println("the length of the txt string
is: " + txt.length());
}
}
10. THERE ARE TWO WAYS TO CREATE STRING
OBJECT:
1. by string literal - java string literal is created by using double
quotes. for example:
string s="welcome";
2. by new keyword
string s=new string("welcome");//creates two objects and one
reference variable
11. EXAMPLE
class Main {
public static void main(String[] args) {
// create strings
String first = "Java";
String second = "Python";
String third = "JavaScript";
// print strings
System.out.println(first);// print Java
System.out.println(second); // print Python
System.out.println(third); // print JavaScript
}
}
12. EXAMPLE
public class Example{
public static void main(String args[]){
//creating a string by java string literal
String str = "Beginnersbook";
char arrch[]={'h','e','l','l','o’};
//converting char array arrch[] to string str2
String str2 = new String(arrch);
//creating another java string str3 by using new keyword
String str3 = new String("Java String Example");
//Displaying all the three strings
System.out.println(str);
System.out.println(str2);
System.out.println(str3);
}
}