Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.

Similar presentations


Presentation on theme: "CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start."— Presentation transcript:

1 CS 225 Java Review

2 Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start the application

3 main class Every application needs a main class (or driver) which contains a main method public class Driver { public static void main( String [] args) { }

4 Classes A class is a template for creating objects A class contains –Data declarations for representing the properties or attributes of an object created from the class –Method definitions for defining the behavior or operations of an object created from the class

5 Structure of a Java Class /** javadoc comment describing class */ public class ClassName { // Data declarations private DataType dataName; // method definitions /** javadoc comment describing method */ public returnType methodName( ) { }

6 Objects Objects are created (instantiated) from a class ClassName objectName = new ClassName(); There can be any number of objects created from a single class –Each object has its own data –A method has access to the data of the object is was called for

7 Java Statements Declarations Assignment Method calls Control Statements –if, switch –while, do, for

8 Declarations Use a declaration to create one or more variables varName1, varName2; Variables instance variables belong to an object static variables belong to a class local variables are declared inside a method

9 Data Types Primitive Types –integer types: byte, short, int, long –real types: float, double –character type: char –boolean has values true and false Reference Types –used for objects, arrays –declaration creates only the reference varaible –objects must be instantiated separately

10 Operators Arithmetic (numeric types only) : + - * / % Relational (numeric and char) : = > Relational (any type) : == != Logical (boolean): && || ! Assignment: = ++ -- += et. al

11 Control Statements Sequence (block)- done in order { } Selection - select between different sequences if switch Repetition - do a sequence multiple times while do-while for

12 Methods Method definition public returnType methodName ( ) { } Method call objectName.methodName( )

13 Static Members Data and methods can be declared static –call static methods using the name of the class public static int objectCount –static methods need don't have access to object data –all objects share static data

14 Java API Java comes with an extensive collection of classes that are already written –Look at the documentation for these classes online Use a class from the Java library by importing it import java.packageName.ClassName;

15 Math class A collection of static methods and constants Math.PI Math.sqrt( double x) : double Math.sin( double x) : double Math.pow( double num, double power) : double … and many more

16 System Class Data and methods needed to interact with the OS System.in : InputStream for keyboard input System.out : PrintStream for console output System.exit( int exitCode) : stops the program

17 Handling Text String and StringBuffer can b used for text data –String is immutable StringTokenizer can be used to separate text into smaller pieces

18 Stream Classes for I/O Input –InputStream -> InputStreamReader - > BufferedReader (or Scanner ) –FileReader -> BufferedReader Output –OutputStream -> OutputStreamWriter -> PrintWriter –FileWriter -> PrintWriter

19 Arrays An array is an indexed collection of objects of the same type –arrays are objects To create an array: –declare it: dataType [] arrayName; –instantiate it: arrayName = new dataType[size] –assign or instantiate each element Get a particular element by index: arrayName[index]


Download ppt "CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start."

Similar presentations


Ads by Google