1 / 18

Java Programming

Java Programming. Why Java?. Platform independence Object Oriented design Run-time checks (fewer bugs) Exception handling Garbage collection some drawbacks (speed, RAM ). Object Oriented Design. software is built from objects each object is an instance of a class

eldora
Download Presentation

Java Programming

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 Programming Java Programming

  2. Why Java? • Platform independence • Object Oriented design • Run-time checks (fewer bugs) • Exception handling • Garbage collection • some drawbacks (speed, RAM) Java Programming

  3. Object Oriented Design • software is built from objects • each object is an instance of a class • classes are arranged in a hierarchy • each class defines fields and methods Java Programming

  4. Types • basic (primitive) types • boolean (true/false) • char (16 bits), byte (8 bits) • short (16), int (32), long (64) • float (32 bits), double (64 bits) • Objects • “structured” types • must be “instantiated” Java Programming

  5. Instantiation • basic types (space allocated automatically)int n; • arrays(must allocate space explicitly)int[] col = new int[5]; • ObjectsGnome g; (allocates reference only) g = new Gnome(...); (calls constructor method) • several constructors may be defined for the same class, with different parameters. Java Programming

  6. arrays • array of Objects = array of “references” • two-step allocationGnome[] party = new Gnome[5]; for( int i=0; i < party.length; i++ ) { party[i] = new Gnome(...); } • 2-dimensional array = “array of arrays”int table[][] = new int[10][20]; • triangular arrays (allocate each row separately)int[][] table = new int[10][]; for( int i=0; i < table.length; i++ ) { table[i] = new int[i]; } Java Programming

  7. Number Objects (“wrappers”) • basic type “wrapped” inside an ObjectInteger N = new Integer(1045); int n = N.intValue(); Double X = new Double( 3.934 ); double x = X.doubleValue(); • these classes provide useful methodsInteger.parseInt( String s ) Double.isInfinite( double v ) Java Programming

  8. String class • concatenationString s = “kilo” + “meters” • several useful methodsint length() char charAt( int index ) int compareTo( String other ) • see also StringBuffer Java Programming

  9. Methods • return types • parameters • constructor methods • main method • local variables Java Programming

  10. Point class public class Point{ private double x; private double y; public double getX() { return( x ); } public double getY() { return( y ); } public void translate( double dx, double dy ) { x += dx; y += dy; } } Java Programming

  11. Modifiers (class/field/method) • Special purpose • static (all instances share same value) • final (can’t reassign/override/subclass) • abstract (can’t instantiate) • Access control • public (anyone can access) • protected (same package or subclass) • friendly (default - only same package) • private (only same class can access) Java Programming

  12. Expressions • Literals • null • true, false • integer 42,176L, -52I • Floating point 3.14159 • Character ’\t’ • String “status quo ante” Java Programming

  13. Operator Precedence • ++ -- ~ ! (postfix, prefix, type conversion) • * / % (multiply/divide) • + - (add/subtract) • << >> >>> (shift) • < <= > >= (comparison) [also instanceof] • == != (equality) • & (bitwise-and) • ^ (bitwise-xor) • | (bitwise-or) • && (and) • || (or) • ? : (conditional) • = += -= *= /= %= >>= <<= >>>= &= ^= |= (assignment) Java Programming

  14. Casting • Numberical conversionint ifrac, i=3, j=4;double dfrac, d=3.2;dfrac = i / d; // ( i automatically cast to double)dfrac = i / (double) j ; // ( i again cast to double) ifrac = i / d; // (will cause a compilation error)ifrac = i / (int) d;//(loss of precision made explicit) • String conversionString s = “” + 22; // (correct, but ugly)String s = Integer.toString(22); // (much better) Java Programming

  15. Control Flow • if( ) { ... } else { ... } • switch • loops • for( ; ; ) { ... } • while( ) { ... } • do { ... } while( ); • return • break / continue Java Programming

  16. Input / Output • Output - very simpleSystem.out.println(“size:\t” + x ); • Input - a bit more complicatedBufferedReader InputStreamReader System.in(see, for example Copy.java) Java Programming

  17. Packages • classes can be collected in a package • standard packages are provided, or you can create your own • import a single classimport java.io.InputStream; • import an entire packageimport java.io.*; Java Programming

  18. Next Time • Object Oriented design • Inheritance/Polymorphism • Exception handling • Interfaces & abstract classes • Casting • Design Patterns • Javadoc Java Programming

More Related