Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Object Oriented Programming

CS F213
J. Jennifer Ranjani
email: jennifer.ranjani@pilani.bits-pilani.ac.in
BITS Pilani
Pilani Campus
String Tokenizer
User defined
immutable classes
BITS Pilani
Wrapper classes
Pilani Campus
BITS Pilani
Pilani Campus

String Tokenizer
String Tokenizer
• java.util.StringTokenizer class allows you to break a
string into tokens
• default delimiter set, which is " \t\n\r\f" : the space character, the tab character,
the newline character, the carriage-return character, and the form-feed character.

Constructor Description
StringTokenizer(String str) creates StringTokenizer with
specified string.
StringTokenizer(String str, String creates StringTokenizer with
delim) specified string and delimeter.
StringTokenizer(String str, String creates StringTokenizer with
delim, boolean returnValue) specified string, delimeter and
returnValue. If return value is true,
delimiter characters are considered
to be tokens. If it is false, delimiter
characters serve to separate tokens.

BITS Pilani, Pilani Campus


Methods

Public method Description


boolean hasMoreTokens() checks if there is more tokens available.

String nextToken() returns the next token from the


StringTokenizer object.

String nextToken(String delim) returns the next token, after switching to


the new delimiter.

boolean hasMoreElements() same as hasMoreTokens() method.

Object nextElement() same as nextToken() but its return type


is Object.

int countTokens() returns the total number of tokens.

BITS Pilani, Pilani Campus


String Tokenizer - Example
import java.util.StringTokenizer;
public class test{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is \t khan \n");
int i=0,j;
j = st.countTokens();
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
i++;
Output:
} my
System.out.println("i: "+i+"and j: "+j); name
System.out.println( st.countTokens()); is
} khan
i: 4 and j: 4
} 0

BITS Pilani, Pilani Campus


String Tokenizer - Example
import java.util.StringTokenizer;
public class Test{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name/ is \t khan \n");
int i=0,j;
j = st.countTokens();
while (st.hasMoreTokens()) {
System.out.println(st.nextToken("/"));
i++; Output:
} my name
is khan
System.out.println("i: "+i+"and j: "+j);
System.out.println( st.countTokens()); i: 2and j: 4
} 0
}

BITS Pilani, Pilani Campus


Review Questions

• StringTokenizer stuff = new StringTokenizer("abc,def,ghi");


System.out.println(stuff.nextToken() );

Output:
abc,def,ghi

• StringTokenizer stuff = new StringTokenizer("abc,def,ghi", ",");


System.out.println(stuff.nextToken());

Output:
abc

BITS Pilani, Pilani Campus


Review Questions

• StringTokenizer stuff = new StringTokenizer( "abc+def+ghi", "+", true );


System.out.println( stuff.nextToken() );
System.out.println( stuff.nextToken() );
Output:
abc
+

• StringTokenizer stuff = new StringTokenizer( "abc def+ghi", "+");


System.out.println( stuff.nextToken() );
System.out.println( stuff.nextToken() );
Output:
abc def
ghi

BITS Pilani, Pilani Campus


Review Questions

• StringTokenizer st = new StringTokenizer( "abc+def:ghi", "+:",


true );
while(st.hasMoreTokens()){
System.out.println(st.nextToken()); }
Output:
abc
+
def
:
ghi

BITS Pilani, Pilani Campus


BITS Pilani
Pilani Campus

Mutable and Immutable


Objects
Immutability and Instances

• Mutable Objects: Contents of an instance that can be


modified.

• Eg: Immutable: java.lang.String


Mutable: Account

• When the contents of the String instance are modified, a


new string object is created.

BITS Pilani, Pilani Campus


How to create an Immutable
class?
• Class must be declared as final
• So that child classes can’t be created

• Data members in the class must be declared as final


• So that we can’t change the value of it after object creation

• A parameterized constructor

• Getter method for all the variables in it

• No setters
• To not have option to change the value of the instance variable

BITS Pilani, Pilani Campus


Immutable Class - Example
final class Account{
final int acc;
final String name;
final float amount;

Account(int acc,String name,float amt){


this.acc = acc;
this.name = name;
this.amount = amt; }

int getAcc(){
return acc;}
String getName() {
return name; }
float getAmount() {
return amount; }

BITS Pilani, Pilani Campus


Immutable Class - Example
public String toString(){
return "Acc: "+getAcc()+" Name: "+name;
}
class TestAccount{
public static void main(String[] args) {

Account a= new Account(111,"Ankit",5000);

System.out.println(a);

Output:
a.amount = 1000;
Exception in thread "main" java.lang.Error:
}}
Unresolved compilation problem:
The final field Account.amount cannot be
assigned

BITS Pilani, Pilani Campus


BITS Pilani
Pilani Campus

Wrapper Classes
Wrapper Class

• Converts a primitive into object (Autoboxing) and object


into a primitive (unboxing)

Primitive Type Wrapper class

boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

BITS Pilani, Pilani Campus


BITS Pilani
Pilani Campus

Autoboxing and
Unboxing (Java 5)
Wrapper Class & Boxing -
Example
int a = 50;
Integer i = Integer.valueOf(60); Output:
Integer j = a; // auto boxing -1
System.out.println(j.compareTo(i));

Integer i = new Integer(50); Output:


int a = i; // unboxing 26
int b = Integer.numberOfLeadingZeros(i);
System.out.println("Unboxing"+a+"Int Value"+b);

Note: Binary value of 50 is 0b110010; int is 32 bits long

BITS Pilani, Pilani Campus


Review Questions
static void print(int i,int j){System.out.println("int");}
static void print(Integer i,Integer j) {System.out.println("Integer");}
static void print(Integer... i){System.out.println("Var Integer");}
public static void main(String args[]){
short s=30,t=50;
Integer a=30,b=50,c=70;
// Place any of the following statements }

Which version of the print method will be invoked?


• print(s)
• print(s,t)
• print(a,b)
• print(a,b,c)

BITS Pilani, Pilani Campus


Review Questions
static void print(int i,int j){System.out.println("int");}
static void print(Integer i,Integer j) {System.out.println("Integer");}
static void print(Integer... i){System.out.println("Var Integer");}
public static void main(String args[]){
short s=30,t=50;
Integer a=30,b=50,c=70;
// Place any of the following statements }

Which version of the print method will be invoked?


• print(s) // Error
• print(s,t) // int
• print(a,b) //Integer
• print(a,b,c) //Var Integer

BITS Pilani, Pilani Campus


Rules

• Widening and boxing cant be performed at the same


time

• Widening > Boxing > Varargs

• Widening between wrapper classes is not allowed

BITS Pilani, Pilani Campus

You might also like