String:-
The string is the predefined class of JAVA which can contain a collection of char. String object value can not be changed dynamically from the actual address hence it is also called an Immutable Object.
We can Write String using two different ways:-
1) String as a reference
String s = "hello"; //reference but store data in String pool, String pool memory will be created by JVM using extern()
We can Write String using two different ways:-
1) String as a reference
String s = "hello"; //reference but store data in String pool, String pool memory will be created by JVM using extern()
String s1= "hello";
String s2= "hello";
only one String pool memory will be created.
if(s1==s2)
{
sout("equall");
}
if(s1==s2)
{
sout("equall");
}
2) String as an Object:-
it will store data under heap memory with a separate memory block. It provides safe and individual memory.
String s = new String("hello");
String s1 = new String("hello");
String s2 = new String("hello");
if(s==s1) // == is used to compare value type not reference type
sout("equall")
else
sout("not equall");
o/p not equally
if(s.equals(s1)) // equals() is used to compare the value according to address
sout("equall")
else
sout("not equall");
o/p equal because equals() will compare values using the corresponding address
It will store data in heap memory under JVM
The important question of String:-
class Stringexample
{
public static void main(String args[])
{
String s = "hello";
String s2=s.concat("world");
System.out.println(s);
System.out.println(s2);
}
}
class Stringexample2
{
public static void main(String args[])
{
String s = new String("hello");
String s2=s;
String s1="hello";
// if(s.equals(s1))
if(s1==s2)
System.out.println("equall");
else
System.out.println("not equall");
}
}
StringBuffer:-
It provides mutable and synchronized objects in java means we can change String Buffer data from actual address and it can be used in the Multithreading process.
Syntax of StringBuffer:-
String s2 = new String("hello");
if(s==s1) // == is used to compare value type not reference type
sout("equall")
else
sout("not equall");
o/p not equally
if(s.equals(s1)) // equals() is used to compare the value according to address
sout("equall")
else
sout("not equall");
o/p equal because equals() will compare values using the corresponding address
It will store data in heap memory under JVM
The important question of String:-
class Stringexample
{
public static void main(String args[])
{
String s = "hello";
String s2=s.concat("world");
System.out.println(s);
System.out.println(s2);
}
}
class Stringexample2
{
public static void main(String args[])
{
String s = new String("hello");
String s2=s;
String s1="hello";
// if(s.equals(s1))
if(s1==s2)
System.out.println("equall");
else
System.out.println("not equall");
}
}
StringBuffer:-
It provides mutable and synchronized objects in java means we can change String Buffer data from actual address and it can be used in the Multithreading process.
Syntax of StringBuffer:-
StringBuffer ref = new StringBuffer("data1");
ref.append("data");
Example of StringBuffer:-
ref.append("data");
Example of StringBuffer:-
public class Stringexample {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("hello");
s.append("world");
System.out.println(s);
}
}
StringBuidler in Java:-
It provides mutable objects in java means we can change String Builder data from the actual address and it can not be used in the Multithreading process because StringBuilder can not be Synchronized.
Syntax of StringBuilder:-
It provides mutable objects in java means we can change String Builder data from the actual address and it can not be used in the Multithreading process because StringBuilder can not be Synchronized.
Syntax of StringBuilder:-
StringBuilder ref = new StringBuilder("data1");
ref.append("data"); System.out.print(ref) ;//data1data
Example of StringBuffer Comparision:-
class Stringexample2
{
public static void main(String args[])
{
String s = new String("hello");
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("hello");
//if(s2.equals(s1))
if(s2.toString().equals(s1.toString()))
System.out.println("equall");
else
System.out.println("not equall");
}
}
most imp question of String and StringBuffer?
class Stringexample2
{
public static void main(String args[])
{
String s = new String("hello");
StringBuffer s1 = new StringBuffer("hello");
s1.append(s); //hello hello
String s2=s.concat(s1.toString()); //hello hello hello
s1.append(s2);
System.out.println(s1);
System.out.println(s);
System.out.println(s2);
}
}
..............................................................................................................
Assignment:-
Write a program to define 10 predefine methods of String?
Write a program to define 5 different methods of String Buffer and String builder?
Example of StringBuffer Comparision:-
class Stringexample2
{
public static void main(String args[])
{
String s = new String("hello");
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("hello");
//if(s2.equals(s1))
if(s2.toString().equals(s1.toString()))
System.out.println("equall");
else
System.out.println("not equall");
}
}
most imp question of String and StringBuffer?
class Stringexample2
{
public static void main(String args[])
{
String s = new String("hello");
StringBuffer s1 = new StringBuffer("hello");
s1.append(s); //hello hello
String s2=s.concat(s1.toString()); //hello hello hello
s1.append(s2);
System.out.println(s1);
System.out.println(s);
System.out.println(s2);
}
}
..............................................................................................................
Assignment:-
Write a program to define 10 predefine methods of String?
Write a program to define 5 different methods of String Buffer and String builder?
Write a program to find the largest palindrome in a paragraph?
WAP to count the total wold in a paragraph?
WAP to count total upper case, lower case, special char, and white space in a paragraph?
WAP to convert String in the opposite case?
Write a program to define 10 predefine method of String?
ReplyDeletepublic class TenMethod {
public static void main(String[] args) {
String s3="java is fun to learn";
String s;
char arr[]= {'g','u','p','t','a'};
String s2=new String(arr);
String s1=new String("prashant");
String s4="java";
s= "welcome in java";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
System.out.println(s.concat(" for beginners"));
System.out.println(s.substring(10));
System.out.println(s.substring(4,10));
System.out.println(s1.hashCode());
System.out.println(s2);
System.out.println(s3.charAt(2));
System.out.println(s3.replace("fun", "easy"));
System.out.println(s3.contains(s1));
System.out.println(s3.contains(s4));
char [] Array = s4.toCharArray();
System.out.println("size of array" +Array.length);
System.out.println(" last element of array is " + Array[3]);
}
}
class Pcount
ReplyDelete{
public static void main(String arg[])
{
String s="Kangaroo soft private Limited";
int x=s.length();
int c=0;
for(int i=0;i<x;i++)
{
if(s.charAt(i)==' ')
{
}
else
{
c++;
}
}
System.out.println(c);
}
}
Wrong count word not char
Deleteclass Method
ReplyDelete{
public static void main(String arg[])
{
String s1="Hello Ankit";
String s2="Goswami";
System.out.println(s1.toUpperCase());
System.out.println(s1.toLowerCase());
System.out.println(s1.length());
System.out.println(s1.equals(s2));
System.out.println(s1.concat(s2));
System.out.println(s1.lastIndexOf("t"));
}
}
only six method try for 10 method
Deleteclass Convert
ReplyDelete{
public static void main(String arg[])
{
String s="hello";
char ch[]=s.toCharArray();
for(int i=0;i<ch.length;i++)
{
ch[i]=(char)((int)ch[i]-32);
}
for(int i=0;i<ch.length;i++)
{
System.out.print(ch[i]);
}
}
}
It only convert lower case to upper case, try for all cases
Deleteclass Count1
ReplyDelete{
public static void main(String arg[])
{
String s=" ANkit Goswami";
int x=s.length();
int uppercase=0;
int lowecase=0;
for(int i=0;i='A'&& ch<='Z')
{
uppercase++;
}
if(ch>='a'&& ch<='z')
{
lowecase++;
}
}
System.out.println(uppercase);
System.out.println(lowecase);
}
}
Wrong try again
Delete#Program to validate Email ID
ReplyDeleteclass Email
{
public static void main(String args[])
{
String email = "xyz@gmail.com";
int dpos=0,atpos=0,i;
for(i=0;iatpos && (dpos-atpos)!=0 && (i-1)!=dpos)
System.out.println("Valid Emailid");
else
System.out.println("Invalid Emailid");
}
}
//yogesh seroke
ReplyDelete//Reverse String
class A
{
public static void main(String args[])
{
String s="abcdef";
s.reverse();
System.out.println(s);
}
class A
ReplyDelete{
public static void main(String args[])
{
StringBuffer s= new StringBuffer("abcdef");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
//yogesh seroke
ReplyDelete//Strings methods
class Stringmethods
{
public static void main(String args[])
{
String s="java";
String s1="IS";
String s2="programming";
String s3="language";
System.out.println(s.toUpperCase()+" upper case");
System.out.println(s1.toLowerCase()+" lower case");
System.out.println(s2.concat(" "+s3));
System.out.println(s.substring(1));
System.out.println(s.charAt(0));
System.out.println(s.compareTo(s1) );
System.out.println(s.codePointAt(2));
System.out.println(s2.codePointBefore(3));
System.out.println(s2.codePointCount(2,2));
System.out.println(s2.length());
}
}
//yogesh seroke
ReplyDelete//String reverse case
class StringReverse
{
public static void main(String args[])
{
StringBuffer s=new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
package stringexample;
ReplyDeletepublic class Paragraph {
public static void main(String[] args) {
String s1 = "hello world naman aaaaaaa cdfdsfdsfdsfds ssssssssssssssssssssss dkudsg hfdshgf dhjsgf hjds gfhj gfhffhhjfgfhfgjgg ";
String s = s1 + " ";
int m=0;
String word="",word1="";
for(int i=0;i=0;j--)
{
rev = rev + word.charAt(j);
}
if(rev.equals(word))
{
if(m<word.length())
{
m = word.length();
word1=word;
}
System.out.println(word);
}
word="";
}
}
System.out.println("Max is "+word1);
}
}
//concat two String
ReplyDeleteclass Shubham
{
public static void main (String args[])
{
String s ="Kshetriya";
String s1 ="Shubham";
System.out.println(s1+' '+s);
}
}