SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz)
CHARACTER ENCODING On digital systems, each character is represented by a specific number. The character set (short charset) determines, which number represents a character. There are numerous different character sets in use: US-ASCII: 128 characters (7 bit) one of the oldest standards; the first 128 characters of most character sets are identical to ASCII Windows ANSI: 256 characters (8 bit) commonly encountered on Windows systems ISO 8859-1: 256 characters (8 bit) common for western websites UTF-8, UTF-16,... (Unicode): variable character width (e.g. UTF-8 has 8-32 bit). Supports pretty much any language on the planet. Recommended by most international institutions. Java works internally always with characters (char) in UTF-16 format. During input / output, characters need to be converted. Software Development 1 // 2018W // 2
CHARACTER SEQUENCES :: STRINGS In Java, character sequences are contained in the class String. String Overview (see API documentation for more details): class String { // Constructors public String() { /* Create a new empty String */ public String(char value[]) { /* String from character array */ public String(byte[] bytes, String charsetname) { /* String from byte array, with specified character set */ // Methods public int length() { /* number of characters */ public char charat(int index) { /* single char at position */ public int indexof(int ch) { /* first position of a char */ public String substring(int beginindex) { /* part of the String */ public String concat(string str) { /* join 2 Strings */ public int compareto(string anotherstring) { /* compare Strings */ public static String valueof(double d) { /* convert a double to String */ Software Development 1 // 2018W // 3
CONSTRUCTORS OF CLASS STRING public String(char value[]) Convert array of characters to a String. public String(byte[] bytes, String charsetname) Convert array of bytes to a String. The specified character encoding is used for conversion. public String(byte[] bytes) Convert with system default character set. public String(StringBuffer buffer) Convert a StringBuffer to String. Beispiele: char[] c = {'F', 'r', 'o', 'g', ' ', 's'; String sc = new String(c); byte[] b = {0x20, 0x6c, 0x65, 0x67, 0x73; String sb = new String(b, "US-ASCII"); System.out.println(sc + sb); // Output: Frog s legs Software Development 1 // 2018W // 4
CREATING STRINGS String from a literal: String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Convert from arrays of characters, bytes... String s1 = new String( new char[] {'A', 'B', 'C', 'D', 'E' ); String s2 = new String(s1); // copy of s1 Convert primitive data types: // int, double, long, float, boolean, short... String s3 = String.valueOf( x == 2.4 ); String pi = String.valueOf(3.141592653589793238); Concatenate 2 Strings with the + operator: String firstname = new String("Walter"); String lastname = "Wazinger"; // short form (literal) String name = firstname + lastname; // new String object System.out.println("Age: " + age + " years"); Software Development 1 // 2018W // 5
String rector = "Hagelauer"; rector.length(); // 9 "Hagelauer".length(); // 9 length() is a method, not a field as it is with arrays! rector.charat(2); // 'g' rector.indexof("a"); // 1 first index of "a" rector.indexof("a", 6); // -1 next index of "a", starting at index 6 rector.indexof("x"); // -1 "x" is not found rector.indexof('g'); // 2 'g' is character, not String String s4 = rector.substring(0, 2); // "Ha" substring [0..2[ String s5 = rector.substring(3); // "elauer" substring [3...] String s = s4 + s5; // "Haelauer" boolean error = s.startswith("hagel"); Software Development 1 // 2018W // 6
EXAMPLE :: CHARACTER INDEX class Alphabet { protected static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; protected static int numberinalphabet(char c) { return alphabet.indexof(c) + 1; Get index of character in alphabet (1-26) public static void main(string[] args) { for (int i = 0; i < args.length; i++) { char c; c = args[i].charat(0); System.out.println("Character " + c + " is number " + numberinalphabet(c)); SWE1.10 / Alphabet.java Software Development 1 // 2018W // 7
CONVERTING TO/ FROM STRINGS Converting to primitive data types int i = Integer.parseInt("666"); int j = new Integer("666").intValue(); double pi = Double.valueOf("3.1415").doubleValue(); Converting from primitive data types int three = 3; String s = Integer.toString(three); String number = String.valueOf(666); Converting to character arrays char[] alphabet; alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); Converting from characters or character arrays String t = String.valueOf('i'); String lettersequence = new String(alphabet); Software Development 1 // 2018W // 8
EXAMPLE :: CONVERTING STRINGS TO INT //Display sum of all application arguments public class Convert { public static void main(string[] args) { //convert String arguments to int int[] a = new int[args.length]; for (int i = 0; i < args.length; i++) { // convert with Integer object Integer number = new Integer(args[i]); a[i] = number.intvalue(); // convert directly to int a[i] = Integer.parseInt(args[i]); // build sum and display int sum = 0; for (int i = 0; i < a.length; i++) sum += a[i]; System.out.println("Sum: " + sum); SWE1.10 / Convert.java Software Development 1 // 2018W // 9
COMMON METHODS public String tolowercase() public String touppercase() Transforms letters to lower / upper case. Also works with local letters (e.g. ö, ü, ß) touppercase converts "ß" to "SS"! It can not be converted back with tolowercase. Example: String a = "Schoßhündchen"; System.out.println(a.toLowerCase()); // Output: schoßhündchen System.out.println(a.toUpperCase()); // Output: SCHOSSHÜNDCHEN System.out.println(a.toUpperCase().toLowerCase()); // Output: schosshündchen Software Development 1 // 2018W // 10
COMMON METHODS public boolean equals(object anobject) Overwrites equals method from class Object. Checks if String content is identical. Example: String name1 = "Max"; String name2 = "Max"; String name3 = new String("Max"); String name4 = "max"; System.out.println(name1 == name2); System.out.println(name1 == name3); name1 and name2 do not only have the same contents, they are referencing the same object! Compare the references, not the content! // true // false! System.out.println(name1.equals(name3)); // true System.out.println(name1.equalsIgnoreCase(name4)); // true Compare the String content. Software Development 1 // 2018W // 11
COMMON METHODS public int compareto(string anotherstring) Performs a lexical comparison of 2 Strings. Returns 0, in case the Strings are identical, a value lower than 0, in case the String lexicographically precedes the argument, and a value greater than 0, in case the String lexicographically follows the argument. Example: String name = "Max"; System.out.println(name.compareTo("Franz")); // result > 0 System.out.println(name.compareTo("Max")); // result = 0 System.out.println(name.compareTo("Peter")); // result < 0 Software Development 1 // 2018W // 12
CLASS STRINGBUFFER AND STRINGBUILDER String objects can not be changed once they are created. To edit character sequences, create an instance of either StringBuffer or StringBuilder. Both have identical interface and functionality, but StringBuilder is not thread safe. If only a single thread is accessing the character sequence, StringBuilder should be used since it is slightly faster. Constructors: public StringBuffer() Create a new (empty) character sequence. public StringBuffer(int length) Create a new (empty) character sequence with an initial capacity. The capacity it automatically expanded when necessary. public StringBuffer(String str) Create a new character sequence and initialize it with a String. Software Development 1 // 2018W // 13
STRINGBUFFER :: METHODS StringBuffer a = new StringBuffer("Bazinga!"); int i = a.length(); // Number of characters in a a.append("!!!"); a.append(3); a.append(3.14); // a = "Bazinga!!!!" // append is overloaded for // all primitive data types a.delete(8, a.length()); // a = "Bazinga!" // Delete from index 11 to the end a.insert(0, "3 x "); // a = "3 x Bazinga!" // Insert at index 0 Software Development 1 // 2018W // 14
STRINGBUFFER :: METHODS a.replace(0,1,"100"); // a = "100 x Bazinga!" // Replace index [0..1[ a.deletecharat(2); // a = "10 x Bazinga!" // Delete char at index 2 a.setlength(4); // a = "10 x" // Set to new length; cut off rest a.reverse(); // a = "x 01" // Reverse sequence String s1 = a.tostring(); // Convert buffer to String String s2 = a.substring(0,1); // Only take part of the buffer Software Development 1 // 2018W // 15
STRINGBUFFER :: EXAMPLE // Read int numbers from standard input public class ReadInt { // read a line from standard input and convert to number private static int readint() throws IOException { // read line StringBuilder buffer = new StringBuilder(); // new StringBuilder int c = System.in.read(); // read first char while (c >= 0 && c!= '\n') { // while not end of line buffer.append((char)c); // add char to buffer c = System.in.read(); // parse number in line and return int return Integer.parseInt(buffer.toString()); // test our method public static void main(string[] args) throws IOException { int i = readint(); System.out.println("Your number: " + i); // read next char It is much more efficient to use a StringBuilder than concatenating Strings ( s = s + (char)c; ) Exceptions are not handled SWE1.10 / ReadInt.java Software Development 1 // 2018W // 16
ENUMERATIONS One way to define enumerations is a list of constants: public static final int DAY_MONDAY = 0; public static final int DAY_TUESDAY = 1; public static final int DAY_WEDNESDAY = 2; public static final int DAY_THURSDAY = 3; public static final int DAY_FRIDAY = 4; public static final int DAY_SATURDAY = 5; public static final int DAY_SUNDAY = 6; int dayofevent = DAY_WEDNESDAY; Define all weekdays as constant integers. Assign a weekday to a variable. This approach has several disadvantages: That a constant is part of the enumeration has to be indicated with a name prefix. No type safety: Any int value may be assigned to variables that should hold a weekday. The compiler can only check the type int. Outputting a weekday only shows a numeric value. Complicated and long-winded declaration. Software Development 1 // 2018W // 17
ENUMERATIONS Declaration: enum Name { CONSTANT_1, CONSTANT_2,... All enumerations are subclasses of java.lang.enum Coding conventions: Since enumerations are classes, their name should be formatted like a class name: first letter of each word in uppercase. The values are constants (static final) and therefore should be in all uppercase, with underscores to separate words. Example: enum DaysOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY Declaration of enumeration references: DaysOfWeek dayofevent = DaysOfWeek.WEDNESDAY; Software Development 1 // 2018W // 18
ENUMERATIONS Internally Java translates enumerations in classes, that look like this: enum DaysOfWeek { MONDAY, TUESDAY,... class DaysOfWeek extends Enum { public static final DaysOfWeek MONDAY = new DaysOfWeek(); public static final DaysOfWeek TUESDAY = new DaysOfWeek();... The enumeration values are static final references to a new instance of the class DaysOfWeek This should also explain the following code pieces: DaysOfWeek dayofevent = DaysOfWeek.WEDNESDAY; import static DaysOfWeek.*;... DaysOfWeek dayofevent = WEDNESDAY; The object WEDNESDAY was instantiated once at program start. No further new required! import static allows us to reference enumeration values directly. Software Development 1 // 2018W // 19
ENUMERATIONS Since enumerations are actually classes, they can also contain fields, methods and constructors: enum DaysOfWeek { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7); This would translate to something like this: class DaysOfWeek extends Enum { public static final DaysOfWeek MONDAY = new DaysOfWeek(1); public static final DaysOfWeek TUESDAY = new DaysOfWeek(2);... private int number; DaysOfWeek(int number) { this.number = number; Private object field. Constructor public String tostring() { return super.tostring() + "(" + number + ")"; Overwrite the default tostring method. Software Development 1 // 2018W // 20
ENUMERATIONS :: EXAMPLE public enum IntSize { BYTE, SHORT, INT, LONG; public static IntSize getsize(long i) { // find min. size for number if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) return BYTE; if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) return SHORT; if (i >= Integer.MIN_VALUE && i <= Integer.MAX_VALUE) return INT; return LONG; // try with some numbers public static void main(string[] args) { long[] nums = {34567, 280, -10, 3456789012L; for (long n : nums) { IntSize minsize = IntSize.getSize(n); System.out.print(n + ": "); switch (minsize) { case BYTE: System.out.println("min size is 8 Bit"); break; case SHORT: System.out.println("min size is 16 Bit"); break; case INT: System.out.println("min size is 32 Bit"); break; case LONG: System.out.println("min size is 64 Bit"); Method to find the smallest primitive numeric data type that can store the number. Enumerations can also be used in switch-statements Software Development 1 // 2018W // 21
SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz)