18CS653 Module 5
18CS653 Module 5
18CS653
MODULE-5
ENUMERATION , APPLET ,
STRING
TOPICS
5 Enumerations
5.1 Enumeration Fundamentals
5.1.1The values( ) and valueOf( ) Methods
5.1.2 Java Enumerations Are Class Types
5.2 Type Wrappers
5.3 I/O Basics
5.3.1 Streams
5.3.2 Byte Streams and Character Streams
5.4 Reading Console Input
5.4.1 Reading Characters
5.4.2 Reading Strings.
TOPICS
5 Enumeration:
Example:
constant
if ( ap = = Apple.GoldenDel)
An enumeration value can also be used to control a switch statement. Of
course, all of the case statements must use constants from the same enum
as that used by the switch expression.
For example, this switch is perfectly valid:
// Use an enum to control a switch statement.
switch(ap)
{
case Jonathan:
// ...
case Winesap:
// ...
When an enumeration constant is displayed, such as in a println( )
statement, its name is output. For example, given this statement:
System.out.println(Apple.Winesap);
Example of Enumeration
5.1.1 The values( ) and valueOf( ) Methods:
The values( ) method returns an array that contains a list of the enumeration
constants.
The valueOf( ) method returns the enumeration constant whose value
corresponds to the string passed in str.
Example for values( ) and valueOf( ) Methods:
5.1.2 Java Enumerations Are Class Types:
The type wrappers are Double, Float, Long, Integer, Short, Byte,
Character, and Boolean.
Wrapper classes provide a way to use primitive data types (int, boolean,
etc..) as objects.
The table below shows the primitive type and the equivalent wrapper class:
Character:
Here, ch specifies the character that will be wrapped by the Character object
being created.
To obtain the char value contained in a Character object, call charValue( ),
shown here:
char charValue( )
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )
Example:
Example:
5.3 I/O Basics
Java provide strong, flexible support for I/O as it relates to files and
networks.
Java I/O (Input and Output) is used to process the input and produce the
output.
Example:
In java we can perform file handling using java io API
5.3.1 Streams
The abstract classes Reader and Writer define several key methods that the
other stream classes implement. Two of the most important methods are
read( ) and write( ), which read and write characters of data, respectively.
5.4 Reading Console Input
In older version of java the only way to perform console input was to use a
byte stream and older code that uses this approach persists.
Today, using a byte stream to read console input is still technically possible,
but doing so is not recommended.
The preferred method of reading console input is to use a character-oriented
stream, which makes your program easier to internationalize and maintain.
There are three classes using which we can take console input:
BufferedReader class
Scanner class
Console class
Using command line argument
In Java, console input is accomplished by reading from System.in.
To obtain a character based stream that is attached to the console, we need wrap
System.in in a BufferedReader object.
Its most commonly used constructor is shown here:
BufferedReader(Reader inputReader)
To obtain an InputStreamReader object that is linked to System.in, use the
following constructor:
InputStreamReader(InputStream inputStream)
Putting it all together, the following line of code creates a BufferedReader
that is connected to the keyboard:
import java.io.*;
class BRRead {
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
String Handling
The String Constructors
String Length
Special String Operations
String Literals
String Concatenation
String Concatenation with Other Data Types
String Conversion and toString( )
Character Extraction
charAt( )
getChars( )
getBytes( )
toCharArray( )
String Comparison
equals( ) and equalsIgnoreCase( )
regionMatches( )
startsWith( ) and endsWith( )
equals( ) Versus ==
compareTo( )
Searching Strings
Modifying a String
substring( )
concat( )
replace( )
trim( )
Data Conversion Using valueOf( )
Changing the Case of Characters Within a String
Additional String Methods
StringBuffer
StringBuffer Constructors
length( ) and capacity( )
ensureCapacity( )
setLength( )
charAt( ) and setCharAt( )
getChars( )
append( )
insert( )
reverse( )
delete( ) and deleteCharAt( )
replace( )
substring( )
Additional StringBuffer Methods
StringBuilder
Strings
Example:
char chars[ ] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = new String(chars, 2, 3);
This initializes s with the characters cde.
Strings Constructor
You can also construct a String object that contains the same character
sequence as another String object using this constructor:
String(String strObj)
Here, strObj is a String object.
Example:
// Construct one String from another.
class MakeString
{
public static void main(String args[ ])
{
char c[ ] = {'J', 'a', 'v', 'a'};
String s1 = new String(c);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}
Strings length
String length is nothing but, number of characters that string contains. To get
the length of the string simply call the method length() as shown below:
int length();
Example:
class StringLength
{
public static void main(String args[])
{
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());
}
}
Special String Operations
We can perform various operation using string such as:
create a new string object using string literal.
Concatenate multiple String objects by use of the + operator
Convert other data types to a string representation.
class StringLiteral
{
public static void main(String args[ ])
{
String s1 = "abcd";
System.out.println(s1);
System.out.println("abcd".length());
}
}
String Concatenation: We can concatenate two strings using +
operator which produce string object as the result
Example:
class StringConcat
{
public static void main(String args[ ])
{
String s1 = "22";
String s2 = "Currently I am" + s1 + "years old ";
System.out.println(s2);
}
}
String Concatenation with other Data types: We can
concatenate strings with other data types.
Example:
class StringConcatOtherDatatype
{
public static void main(String args[ ])
{
int age = 22;
String s2 = "Currently I am " + age + "years old ";
System.out.println(s2);
String s3 = “four:“ + 2+2;
System.out.println(s2);
}
}
Output:
Currently I am 22 years old
four: 22
String Conversion and toString(): We can concatenate strings
with other data types.
Example:
class StringConcatOtherDatatype
{
public static void main(String args[ ])
{
int age = 22;
String s2 = "Currently I am " + age + "years old ";
System.out.println(s2);
String s3 = “four:“ + 2+2;
System.out.println(s2);
}
}
Output:
Currently I am 22 years old
four: 22