Data Structures using Java Part 1: String objects and I/O in Java


Objects and methods
———————–

String s1; //declare a String variable
s1 = new String(); // assign it a value
String s2 = new String(); // both the steps combined

s1 = "Hello"; // assign the value into the object s1
s2 = s1; // s1 and s2 points to the same object, same value
s2 = new String(s1); // creates a new String object and copies the value of s1 into s2

The 3 String constructors
—————————-
1. new String() constructs a empty string, ie no characters

String s1; //declare a String variable
s1 = new String(); // assign it a value
String s2 = new String(); // both the steps combined

2. Stick something in quotes.

s1 = "Hello"; // assign the value into the object s1

3. Takes a parameter.

s2 = new String(s1); // creates a new String object and copies the value of s1 into s2; the constructor parameter is s1

NOTE: Constructors always have the name of the class

 

String Methods
—————–
1. Conversion into uppercase

s2 = s1.toUpperCase();

2. To concatenate two Strings

String name = s2.concat(" says hello!");
String say = "*".concat(name).concat("*");

Unlike in C, in Java String objects are immutable, ie their contants never change.

 

Java I/O Classes
——————
Objects in System classes for interacting with user

1. System.out
a PrintStream object that outputs to the screen.

2. System.in
a InputStrem object thats reads stuff from the keyboard.

3.readLine method
is defined on a BufferedReader objectm is used to read a whole line.

How do we construct a BufferReader?
With a InputStreamReader.
How do we construct a InputStreamReader?
We need an InputStream.
How do we construct a InputStream?
System.in is one.

The InputStream objects, their job is to read raw data from the keyboard. An unformatted bit stream. The InputStreamReader object takes the raw data and compose them into characters. Java usually uses unicode 2 byte long characters. The ufferedReader object takes the raw characters and converts them into text. This is done in 3 steps for modularity reasons.

import java.io.*;

class SimpleIO{

public static void main( String args[] ) throws Exception{

BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in)
);

System.out.println(keyboard.readLine());
}

}

To use the Java library, other than java.lang, you need to import that library. You need to import the java.io for the ISR and the BR. The java program always begins with the “main”  method.

Advertisements
Data Structures using Java Part 1: String objects and I/O in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s