View
213
Download
0
Embed Size (px)
Citation preview
chars & Stringschars & StringsComputers process information
Not just numerical information
Human beings deal with “letters” and “words”
Java codes “letters” as the “char” type
Java codes “words” as the “String” type
ASCII – one byte (8 bits) per character
UniCode – two bytes (16 bits) per character
Under the HoodUnder the Hood
In the computer’s memory we only have bits
0000000001000001
We can interpret (semantics) the bits any way we like
The same bit pattern (syntax) can have multiple meanings
public class StringChar { public static void main(String [] args) {
char c1 = 'A';int i1 = (int) c1;System.out.println("c1 (char) == " + (char) c1);System.out.println("c1 (int) == " + (int) c1);System.out.println("i1 (char) == " + (char) i1);System.out.println("i1 (int) == " + (int) i1);
}
c1 (char) == Ac1 (int) == 65i1 (char) == Ai1 (int) == 65
public static void prnt1(char c1){for (int i = 0 ; i<10;i++) System.out.println(c1 + " + " + i +
" == " + (c1 + i )); }// output for prnt1(‘A’); ??
A + 0 == 65A + 1 == 66A + 2 == 67A + 3 == 68A + 4 == 69A + 5 == 70A + 6 == 71A + 7 == 72A + 8 == 73A + 9 == 74
public static void prnt1(char c1){for (int i = 0 ; i<10;i++) System.out.println(c1 + " + " + i +
" == " + (char) (c1 + i )); } // output for prnt1(‘A’); ??
A + 0 == AA + 1 == BA + 2 == CA + 3 == DA + 4 == EA + 5 == FA + 6 == GA + 7 == HA + 8 == IA + 9 == J
Strings – groups of charsStrings – groups of chars
String s1 = “Hello CSC 171”;
String s2 = new String(“Hello CSC 171”);
Strings as ArraysStrings as Arrays
String name = “Teddy”;
T e d d y
0 1 2 3 4
2352
2354
2356
2358
2360
name
String lengthString lengthString name = “Teddy”,
str1 = “Ted”,
str2 = new String(),
str3;
name.length();
str1.length();
str2.length();
str3.length();
53
0
Error – str3 is null
Strings are objectsStrings are objects
String st1; // declares a reference
st1 = new String(“Teddy”);
//constructs a new object
// sets st1 to refer to the new object
st1 String
“Teddy”
Comparing StringsComparing Strings
Is one string equal to another?
It is import to know what we are comparing
“= =“ compares the reference
“equals” compares the contents of the objects
The “reference”The “reference”
String st1; // declares a reference
st1 = new String(“Teddy”);
//constructs a new object
// sets st1 to refer to the new object
st1 String
“Teddy”
Memory address
String st1;
st1 = new String(“Teddy”);
String st2;
st2 = new String(“Teddy”);st1 String
“Teddy”
st2 String
“Teddy”
String st1; st1 = new String(“Teddy”);
String st2; st2 = new String(“Teddy”);
boolean b1 = (st1 = = st2);
// false because different locst1 String
“Teddy”
st2 String
“Teddy”
String st1; st1 = new String(“Teddy”);
String st2; st2 = st1;
boolean b1 = (st1 = = st2);
// true because same loc
st1 String
“Teddy”
st2
String st1; st1 = new String(“Teddy”);
String st2; st2 = new String(“Teddy”);
boolean b1 = (st1.equals(st2));
// true because contentsst1 String
“Teddy”
st2 String
“Teddy”
String st1; st1 = new String(“Teddy”);
String st2; st2 = new String(“Ted”);
boolean b1 = (st1.equals(st2));
// false because contentsst1 String
“Teddy”
st2 String
“Ted”
Primitive & ReferencePrimitive & Reference
Primitive
int
byte
char
short
long
float
double
boolean
Reference
Object
String
Applet
myRational
public class myClassTest {
public static void main(String[] args) {
myClass mc1 = new myClass();
mc1.value1 = 3;
int x1 = 3;intChange(x1);
System.out.println("x1 == " + x1);
public static void intChange(int intVal){
intVal = 5;
}
myClass mc1 = new myClass();mc1.value1 = 3;myClassRefChange(mc1);System.out.println("mc1.value1 == " +mc1.value1);
public static void myClassRefChange(myClass myClassRef){ myClassRef = new myClass();
myClassRef.value1 = 7;}
myClassValChange(mc1);System.out.println("mc1.value1 == " +
mc1.value1);
public static void myClassValChange(myClass myClassRef){myClassRef.value1 = 9;
}
mc1 = myClassRefReturn(mc1);System.out.println("mc1.value1 == " + mc1.value1);
public static myClass myClassRefReturn(myClass myClassRef){myClassRef = new myClass();myClassRef.value1 = 11;return myClassRef ;
}
Formatting NumbersFormatting Numbers
int quarters = 2;
int dollars = 3;
double total = dollars + quarters * 0.25;
final double TAX_RATE = 8.5;
double tax = total * TAX_RATE / 100;
System.out.println("Total: $"+total);
System.out.println("Tax : $"+tax);
Total: $3.5Tax : $0.2975
Formatting NumbersFormatting Numbers
NumberFormat formatter =
NumberFormat.getNumberInstance();
formatter.setMaximumFractionDigits(2);
System.out.println("Total: $"+total);
System.out.println("Tax : $"+
formatter.format(tax));
Total: $3.5Tax : $0.3
Formatting NumbersFormatting Numbers
NumberFormat formatter =
NumberFormat.getNumberInstance();
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println("Total: $"+total);
System.out.println("Tax : $"+
formatter.format(tax));Total: $3.5Tax : $0.30
Strings are immutableStrings are immutable
They don’t change
We simulate change by making new strings
StringBuffers are used to change
SubString – creates newSubString – creates new
Two ways of getting substrings
String letters = “abcdefghijklmabcdefghijklm”;
System.out.println(letters.substring(20));
// form index 20 to end “hijklm”
System.out.println(letters.substring(0,6));
// form index 0 up to 6 “abcdef”
FRAME WINDOWSFRAME WINDOWSApplets are programs that run inside a
browser.– Limited access to local machine– Good graphics/UI support
Applications run outside of a browser– Access to local machine– Often (so far) console/text based
Frame based applications– Access to local machine– Good graphics/UI
import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;
public class FrameTest {
public static void main(String [] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel iconLabel = new JLabel(new ImageIcon ("sierpins.gif")); JLabel textLabel = new JLabel("Hello, World!");
JPanel panel = new JPanel(); panel.add(iconLabel); panel.add(textLabel); frame.setContentPane(panel); frame.pack(); frame.show(); }}
Better DesignBetter Design
public class FrameAloneTest {
public static void main(String[] args) { FrameAlone myFrame = new FrameAlone(); myFrame.runFrameAlone();
}}
FrameAloneFrameAlone
import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;
public class FrameAlone extends JFrame{
public FrameAlone() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel iconLabel = new JLabel(new ImageIcon ("sierpins.gif")); JLabel textLabel = new JLabel("Hello, World!");