1 / 19

Programming with Java

Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java . Mid-Term Review. Programming with Java . Static and Final. Static Variables. private static int variableName ;. Static variable is still variable (mutable)

judah
Download Presentation

Programming with Java

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. Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor

  2. Programming with Java Mid-Term Review

  3. Programming with Java Static and Final

  4. Static Variables private static intvariableName; Static variable is still variable (mutable) Sometimes called a class variable One variable shared with all objects of the class (belongs to the class, not the object) Default Initialization applies Initialized before object creation and before any static method of the class runs Improves compiler optimization

  5. Static Reference Variables Point to a single object Can’t be altered to point to another object Object pointed to works as expected

  6. Static Variable Example Public class Duck { private int size; private static in duckCount = 0; public Duck () { duckCount++; } public void setSize(int s) { size = s; } public intgetSize() { return size; }

  7. Static Method public static void methodName() { . . . } Good for utilities that don’t rely on instance variables Belongs to the class, not the object Can call any other static method Can’t call non-static methods Can’t use non-static instance data Can act on variables passed in parameters Called using the class name: Math.abs(i); Remember: every program has a public static void main(String[] args)

  8. Static Class Only available to nested classes We aren’t going there in this course

  9. Final Variables private final int VARIABLE_NAME = 0; public static final double PI = 3.14159 Not really variable (immutable) Must be initialized at definition or in constructor Can only be assigned once Static final variable = constant

  10. Initialize Static Final public static final double PI = 3.14159 public static final double SEED; static { SEED = Math.random(); } Initialize at definition or Initialize in static

  11. Final Method class Stuff { public final void things () { // do important things // that can’t be overridden } } Can’t be overridden

  12. Final Class final class Stuff { public final void things () { // do important things // that can’t be overridden } // more stuff } Can’t be extended

  13. Programming with Java Math

  14. Math Class int index = Math.random() * range; Provides a set of math functions Similar to but, less extensive than C/C++ math libraries Included in java.lang, no import required Methods are all static No instance variables Private constructor therefore can’t be instanced Employs overloading

  15. Math Members • Exp • Expm1 • Hypot • Log • Log10 • Log1p • Nextafter • Nextup • Scalb • getExponent • Random • Round • Min • Max • Abs • Sqrt • Cbrt • Pow • Floor • Ceil • copySign • Sin • Sinh • Cos • Cosh • Tan • Asin • Acos • Atan • Atan2 • ToDegrees • toRandom

  16. Programming with Java Wrapping Primitives

  17. Wrapper Classes Boolean Character Byte Short Integer Long Float Double • One to one match to the primitives • Spelling isn’t always the same • Have some static methods intaValue = 999; Integer wrappedValue = new Integer(aValue); intunWrapped = wrappedValue.intValue();

  18. Autoboxing Use either a primitive or its wrapper type virtually anywhere either is expected Method arguments Return values Boolean expressions Operations on numbers Assignments Compiler takes care of the accounting Not the same as assigning different number formats or casting

  19. Next Week: Exceptions Packages, Jars and Deployment. Oh, my!

More Related