In Java, strings are class objects and implemented using two classes,
namely, String and StringBuffer.
A Java string is an instantiated object of the String class
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "compile online";
Object objVal = str;
// returns the string representation of the Object argument
System.out.println("Value = " + str.valueOf(objVal));
boolean bol = true;
String s1 = String.valueOf(bol);
}
}
String str[] = {"Mumbai","Delhi","Jaipur"};
String temp=null;
int size=str.length;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
System.out.println(str[j]+" "+str[i]+" "+ str[j].compareTo(str[i]));
if(str[j].compareTo(str[i])<0)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
Delhi Mumbai -9
} Jaipur Delhi 6
} Jaipur Mumbai -3
for(int i=0;i<str.length;i++) Delhi
} { Jaipur
System.out.println(str[i]); Mumbai
While String creates strings of fixed length, StringBuffer creates
strings of flexible length that can be modified in terms of both length
and content.
We can insert characters and substrings in the middle of a string, or
append another string to the end.
StringBuffer sb = new StringBuffer("object language");
for(int i=0;i<sb.length();i++)
o
System.out.println(sb.charAt(i));
b
j
String str= new String(sb.toString()); e
c
int pos=str.indexOf(" language"); t
sb.insert(pos, " oriented");
l
System.out.println(sb);
a
n
sb.setCharAt(6,'-'); g
u
System.out.println(sb); a
g
e
sb.append(" Java"); object oriented language
System.out.println(sb); object-oriented language
object-oriented language Java
Vector class is contained in the java.util package.
This class can be used to create a generic dynamic array
known as vector that can hold objects of any type and any
number.
A major constraint in using vectors is that we cannot directly store simple
data type in a vector; we can only store objects.
We need to convert simple types to objects.
The vector class supports a number of methods that can be used to
manipulate the vectors created.
import java.util.Vector;
class VecTest
{
public static void main(String args[])
{
Vector list = new Vector() ; Input
Python
int length = args. length;
C++
PHP
for (int i= 0; i < length; i++)
Output
list.addElement(args[i] ) ; Python
C++
list.insertElementAt( "Java" , 2) ; Java
PHP
int size =list.size();
String listArray[]= new String[size];
list.copyInto(listArray);
for (int i =0; i<size;i++)
System.out.println(listArray[i]);
Vectors cannot handle primitive data types like int, float, long,
char, and double.
Primitive data types may be converted into object types by using
the wrapper classes contained in the java.lang package.
Class Test{
public static void main(String args[])
{
Double myDouble = new Double(0);
String val1="7.55";
myDouble=Double.valueOf(val1);
System.out.println(myDouble);
7.55
99
int myInt=0;
String val2="99";
myInt=Integer.parseInt(val2);
System.out.println(myInt);
}
}
Autoboxing and Unboxing
The autoboxing and unboxing feature facilitates the process of handling primitive data
types in collections.
We can use this feature to convert primitive data types to wrapper class types
automatically.
The compiler generates a code implicitly to convert primitive type to the corresponding
wrapper class type and vice versa.
Without autoboxing and unboxing
import java.util.Stack;
class StackTest {
public static void main (String args[])
{
Stack myS=new Stack();
myS.push(new Integer(5));
myS.push(new Integer(10));
Integer s1=(Integer) myS.pop();
Integer s2=(Integer) myS.pop();
int sum=s1.intValue()+s2.intValue();
System.out.println(sum);
15
}
}
import java.util.Stack;
import java.util.Stack;
public class StackTextAutobox {
class StackTest {
public static void main (String args[]) public static void main (String args[])
{ {
Stack myS=new Stack(); Stack<Integer> myS=new Stack<Integer>();
myS.push(new Integer(5)); myS.push(5);
myS.push(new Integer(10)); myS.push(10);
Integer s1=(Integer) myS.pop(); int sum=myS.pop()+myS.pop();
Integer s2=(Integer) myS.pop();
System.out.println(sum);
int sum=s1.intValue()+s2.intValue();
}
System.out.println(sum);
}
}
}
With autoboxing and unboxing
import java.util.Stack;
public class StackTextAutobox {
public static void main (String args[])
{
Stack<Integer> myS=new Stack<Integer>();
myS.push(5); //autobox
myS.push(10); //autobox
int sum=myS.pop()+myS.pop(); //unbox
System.out.println(sum);
}
15
}
Enumerated Types
class EnumTest {
enum Days {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
public static void main(String[] args) {
for (Days d:Days.values())
{
if( d.equals( Days.Sunday ) )
System.out.println(d+" is a Holiday");
} Sunday is a Holiday
}
}