1 / 36

STRINGS

STRINGS. CSC 171 FALL 2004 LECTURE 17. chars & Strings. Computers 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

Télécharger la présentation

STRINGS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. STRINGS CSC 171 FALL 2004 LECTURE 17

  2. chars & Strings Computers 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

  3. Under 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

  4. 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) == A c1 (int) == 65 i1 (char) == A i1 (int) == 65

  5. 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 == 65 A + 1 == 66 A + 2 == 67 A + 3 == 68 A + 4 == 69 A + 5 == 70 A + 6 == 71 A + 7 == 72 A + 8 == 73 A + 9 == 74

  6. 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 == A A + 1 == B A + 2 == C A + 3 == D A + 4 == E A + 5 == F A + 6 == G A + 7 == H A + 8 == I A + 9 == J

  7. Strings – groups of chars String s1 = “Hello CSC 171”; String s2 = new String(“Hello CSC 171”);

  8. name Strings as Arrays String name = “Teddy”;

  9. String length String name = “Teddy”, str1 = “Ted”, str2 = new String(), str3; name.length(); str1.length(); str2.length(); str3.length(); 5 3 0 Error – str3 is null

  10. st1 String “Teddy” Strings are objects String st1; // declares a reference st1 = new String(“Teddy”); //constructs a new object // sets st1 to refer to the new object

  11. Comparing 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

  12. st1 String “Teddy” The “reference” String st1; // declares a reference st1 = new String(“Teddy”); //constructs a new object // sets st1 to refer to the new object Memory address

  13. st1 st2 String String “Teddy” “Teddy” String st1; st1 = new String(“Teddy”); String st2; st2 = new String(“Teddy”);

  14. st1 st2 String String “Teddy” “Teddy” String st1; st1 = new String(“Teddy”); String st2; st2 = new String(“Teddy”); boolean b1 = (st1 = = st2); // false because different loc

  15. st1 st2 String “Teddy” String st1; st1 = new String(“Teddy”); String st2; st2 = st1; boolean b1 = (st1 = = st2); // true because same loc

  16. st1 st2 String String “Teddy” “Teddy” String st1; st1 = new String(“Teddy”); String st2; st2 = new String(“Teddy”); boolean b1 = (st1.equals(st2)); // true because contents

  17. st1 st2 String String “Teddy” “Ted” String st1; st1 = new String(“Teddy”); String st2; st2 = new String(“Ted”); boolean b1 = (st1.equals(st2)); // false because contents

  18. Primitive int byte char short long float double boolean Reference Object String Applet myRational Primitive & Reference

  19. Passing Objects to Methods public class myClass { public int value1; }

  20. public class myClassTest { public static void main(String[] args) { myClass mc1 = new myClass(); mc1.value1 = 3;

  21. int x1 = 3; intChange(x1); System.out.println("x1 == " + x1); public static void intChange(int intVal){ intVal = 5; }

  22. 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; }

  23. myClassValChange(mc1); System.out.println("mc1.value1 == " + mc1.value1); public static void myClassValChange(myClass myClassRef){ myClassRef.value1 = 9; }

  24. mc1 = myClassRefReturn(mc1); System.out.println("mc1.value1 == " + mc1.value1); public static myClass myClassRefReturn(myClass myClassRef){ myClassRef = new myClass(); myClassRef.value1 = 11; return myClassRef ; }

  25. Technical Aside • FORMATTING NUBMERS

  26. Formatting Numbers Total: $3.5 Tax : $0.2975 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);

  27. Formatting Numbers NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(2); System.out.println("Total: $"+total); System.out.println("Tax : $"+ formatter.format(tax)); Total: $3.5 Tax : $0.3

  28. Formatting 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.5 Tax : $0.30

  29. Strings are immutable They don’t change We simulate change by making new strings StringBuffers are used to change

  30. SubString – 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”

  31. FRAME WINDOWS • Applets 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

  32. 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!");

  33. JPanel panel = new JPanel(); panel.add(iconLabel); panel.add(textLabel); frame.setContentPane(panel); frame.pack(); frame.show(); } }

  34. Better Design public class FrameAloneTest { public static void main(String[] args) { FrameAlone myFrame = new FrameAlone(); myFrame.runFrameAlone(); } }

  35. FrameAlone 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!");

  36. JPanel panel = new JPanel(); panel.add(iconLabel); panel.add(textLabel); this.setContentPane(panel); } public void runFrameAlone(){ this.pack(); this.show(); } }

More Related