Search This Blog

save java programs with".java" extension

examples :

helloworld.java
sample.java
match.java
send-mail.java

quick links

Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

stringtokenizer

import java.util.StringTokenizer;

class stringtokenizertest {

public static void main(String[] args) {
String range = "123456789";
StringTokenizer str= new StringTokenizer (range, "5");
while (str.hasMoreTokens())
System.out.println(str.nextToken());
}

}

string append


class stringtest {

public static void main(String[] args) {

String s= new String("hello "); // immutable string created.
String str= s.concat("how are you");
System.out.println(s);
System.out.println(str);

StringBuffer sb= new StringBuffer("hello"); //mutable string created.
sb.append("how are you");
System.out.println(sb);

StringBuilder sbuilder= new StringBuilder("hello"); //mutable string created
sbuilder.append("how are you");
System.out.println(sbuilder);
}

}

equality

class A1
{
int a;
int b;
A1 (int a, int b)
{
this.a=a;
this.b=b;
}
boolean equals(A1 r)
{
boolean flag = false;
if(this.a == r.a && this.b == r.b)
flag = true;
return flag;
}// overriding equals method to make the objects.equals if their state is the same.
}
class equality {

public static void main(String[] args) {

A1 a1 = new A1(10,2);//change value here to check
A1 a2= new A1(10,2);//change value here to check

System.out.println("object1 : "+a1.hashCode());
System.out.println("object2 : "+a2.hashCode());
if(a1.equals(a2))
System.out.println("both object are equal");
else
System.out.println("not equal");
}

}