Share with others

Assignment No 01


 AIM:

To perform String operations using java such as calculating length of string, lowercase and uppercase conversions, concatenation of strings, reverse the string etc. Accept the String from user.

THEORY:


Features Of Java



Java is an object-oriented, multi-threaded programming language developed by Sun Microsystems in 1991. Java Development Environment consists of Java complier and a Java interpreter. Java complier generates byte code instead of machine code and Java interpreter executes the Java program. The disadvantage of using byte code is the execution speed. In order to write Java program, an editor, a java complier and a java runtime Environment are needed.


The important features of java are

  • Simple and powerful

  • Secure and Portable

  • Robust

  • Multithreaded

  • Distributed and dynamic

  • Object-oriented


String Operations



A combination of characters is a string. Strings are instances of the class String. The addition operator(+) can be used to create and concate the strings.

String names[ ] ={“anand”, ”Rahul”, ”sachin”};


Calling the default constructor with no parameters can create an empty string.

String s= new String( );


In order to create a string initialized with characters, we need to pass an array of char to the constructor.

char chars [ ] ={‘a’, ’b’, ’c’);

String s= new String (chars);

System.out.println(s);


Some String methods are :



Method

Use

s.Length( )

Length of the string

s.charAt( index)

Character at a location given by index

s.equals(s1), s.equalsIgnoreCase(s1 )

An equality checking for two strings

s.compareTo(s1)

Result is negative, positive or zero depending on the lexicographical ordering

s.indexOf(char c), s.lastIndexOf(char c )

Returns the index or –1 if the character not found.

s.substring( )

Returns a new string object containing the required character set.

s.concat( )

Returns a new string object  with the concatenation done.

s.replace( )

Returns a new string object  with the replacement done.

s.toLowercase( ), s.toUpperCase( )

Returns a new string object with the

Lowercase case or uppercase change.

s.trim( )

Returns a new string object  with the white spaces removed from each end.


Strings represent fixed length character sequences and StringBuffer represents variable length character sequences.


StringBuffer sb=new StringBuffer(“abc”);

sb.append (“xyz”);

//the argument gets appended to the end of the current StringBuffer.


Casting is used to convert the value of an object or primitive type into another type. Conversion of primitive types cannot be done explicitly.


CONCLUSION


Various String operations such as calculating the length of the string, reverse the string, palindrome, concatenation of the string, finding the substring are successfully created. 



PROGRAM





Share with others