1 / 12

Java Intro

Java Intro. A grab-bag of tools you should know!. Basic Topics. Arrays Strings and String operations Parsing Strings File I/O. Arrays. Arrays are allocated and managed via pointer-type operations allocated with “new” disposed of (automatically) by garbage-collection

Download Presentation

Java Intro

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. Java Intro A grab-bag of tools you should know!

  2. Basic Topics • Arrays • Strings and String operations • Parsing Strings • File I/O

  3. Arrays Arrays are allocated and managed via pointer-type operations allocated with “new” disposed of (automatically) by garbage-collection Many ways to declare. You need not master all of them. Elements 0..(n-1) DECLARATIONS int x = new int[10]; // allocates 10 integer elements int x[]; // a “pointer” for array elements x = new int[10]; // allocates the memory and assigns // address to x (pointer) int x[] = {10,20,30,40}; // determines size as in “c”

  4. Strings Character as in “c” set of methods for testing isLetter, isDigit, etc. String similar to “c” lots of methods concat, substring, length, etc. Can change (contrary to some texts) but only by “replacement” StringBuffer similar to String Can change by modifying internal values

  5. Character A Class in java as is the case with other basic “types”. char for declaring the variable Character for the class/methods char c, d; Character.isLetter(c); Character.isDigit(c); Character.isLetterOrDigit(c); Character.isUpperCase(c); d = Character.toUpperCase(c); d = Character.toLowerCase(c);

  6. String String s, s1, s2, output; s = new String( "hello" ); // use String constructors s1 = new String(); s2 = new String( s ); s +=“ there”; // append Strings to output output = "s1 = " + s1 + "\ns2 = " + s2 + "\ns = " + s + “\n”; Output : s1 = s2 = hello s = hello there

  7. Other String Functions String output,s,s1,s2; s = new String( "hello" ); // show the functionality output = “Length: “+s.length()+”\n”; output += “Position 2: “+s.charAt(2)+”\n”; output += “e at position: “+s.indexOf(‘e’)+”\n”; output += “2 chars at pos 3: “+s.substring(3,2)+”\n”; String newstr = s.replace(‘l’,’L’); // CREATES NEW STRING output += newstr+”\n”; newstr.concat(“ YOU”); output += newstr+”\n”; s1 = new String( “ hello ” ); s2 = s1.trim(); output += “s1 Length: “+s1.length()+” s2 Length: “+s2.length(); Output: Length: 5 Position 2: l e at position: 1 2 chars at pos 3: lo heLLo heLLo YOU s1 Length 8 s2 Length 5

  8. String Comparison Equality using == actually checks to see if the reference is the same s1 == s2 using equals compares the contents of the objects. s1.equals(s2) to ignore case s1.equalsIgnoreCase(s2) <,>,= use compareTo, = :0, <: negative, >: positive s1.compareTo(s2) Prefixes and suffixes use startsWith(s), startsWith(s,startpos), endsWith(s) s1.endsWith(“:”)

  9. StringBuffer Similar to String Technically Strings are said to be unchangeable, but you really can make limited numbers of changes like +, etc. The focus is that + actually creates a whole new String and reassigns that value destroying the old one. StringBuffer will let you modify it internally StringBuffer x = new StringBuffer (“Hi There.”); x.setCharAt(8,’!’); // x is now “Hi There!” Not allowed with String Can append basic (numeric) data types and conversion is implicit: x.append(2.5); // x is “Hi There!2.5”

  10. Grabbing Tokens! Hi there. How are you today? “ \n\t\r” whitespace definition(default) StringTokenizer Java has a class called StringTokenizer for this purpose. Hi there. How are you today? Constructor takes the String and an optional whitespace definition and parses it internally. Use iterator to access (next)

  11. StringTokenizer example String test = new String(“Hi there. How are you today?”); // create and parse StringTokenizer x = new StringTokenizer(test); // iterate over items while (x.hasMoreTokens() ) { … x.nextToken() } // or use x.countTokens()

  12. Reading from files Java has a number of tools for reading/writing streams. Typically these should be buffered for performance. A FileReader class will read from a file and a BufferedReader class will subclass it for performance. String filename= new String(“myfilename”); BufferedReader input = new BufferedReader( new FileReader (filename) ); String theline; while (( theline = input.readLine()) != null ) { // process theline which does NOT have ‘\n’ on end .. E.g Tokenize it!}

More Related