1 / 14

Programming in Java

Programming in Java. main concepts classes objects and values variables methods constructors parameters statements other key concepts casting operators keywords packages, imports declaration etc. Main Concepts: Class and Object. a class represents a category (type) of objects

xue
Télécharger la présentation

Programming in 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 in Java • main concepts • classes • objects and values • variables • methods • constructors • parameters • statements • other key concepts • casting • operators • keywords • packages, imports • declaration • etc.

  2. Main Concepts: Class and Object • a class represents a category (type) of objects • defines what instance variables (properties) and methods (capabilities) an object has • an object is an instance of a class • has individual values in the instance variables • the methods defined in its class can be called • must be constructed before its methods can be used and the values of its properties can be accessed • an object is constructed or instantiated • using new followed by the name of its class • e.g.: new BallApplication(); • Java program is a collection of classes • execution starts in the main class

  3. Main Concepts: Values and Types • Java computes with values • either objects • or primitive values • e.g. integer number 45, floating point number 3.14, character 'A' • each value has a type • either the class of the object • or a primitive type • e.g. int (integer numbers) • float (floating point), • double (double precision floating point numbers) • char (characters) • etc.

  4. Main Concepts: Variable • a variable holds a value • variable references the place in memory where the value is stored • variable has a name, type and value • think of a house, the house is the value, the variable holds its address • a variable must be declared • its type, name and possibly initial value, e.g.: • String name; • double pi = 3.14; • a variable declared directly in the class (outside methods) is an instance variable • it holds the values of object's properties

  5. Main Concepts: Variable's Values • a variable has initially a default value • e.g. 0 or 0.0 • variable's value is retrievedsimply by using its name • System.out.println(name); writes the value in name • variable can be assigned another value using = • the value of the expression on the right-hand side of = is stored in the variable on the left-hand side • e.g. name = "Doublja"; then name will have value "Doublja" • assignment overwrites the old value

  6. Main Concepts: Methods • a method tells an object to do something • methods are declared within a class of the object • they can be called on every object of this class • method call is preceded by object reference and dot . • e.g. System.out.println(); • where System.out is the variable referencing the object • and println(); is its method • method declaration has a return type, name, parameter list and body • if the method doesn't return any value, its return type is void • parameter list is enclosed in parentheses ( and ); it can be empty • body is enclosed in braces { and }; • method's body is a sequence of statements • the most common statements are assignments and method calls • local variables (declared in the body) hold temporary values

  7. Main Concepts: Constructors • a constructor is a special case of a method • constructordeclaration is like a method declaration • except constructor has the same name as the class • and constructor doesn't have a return type • constructor is called when an object is created using new • e.g. new Ellipse(); • if a class has no constructor, Java inserts a default one • default constructor has no parameters • constructor is mostly used to initialize the object

  8. Main Concepts: Parameters • a method (or constructor) can have parameters • parameters in the method call (actual parameters)and in the method declaration (formal parameters)must correspond, i.e. • in their number • in the type of each parameter • parameters form a comma-separated list enclosed in parentheses ( and ) • a formal parameter in method declaration must be declared i.e. with their type and name • e.g. String substring (int from, int to) {} • when method is called, the formal parameter will be assigned the value of the actual parameter • e.g. when substring (2, 5); is called, the parameter from will be assigned 2 and to will be assigned 5

  9. Main Concepts: Program and Execution • an application program is set of classes written as text files • the name of the file must be the class name and extension .java • Eclipse does this for us • a compiler translates the text into bytecode • the name of the bytecode file is the class name and extension .class • bytecode is an intermediate language • bytecode is the same forany hardware • e.g. .class files from PC can be used on Macs • Eclipse does this for us, compiles whenever a java file is saved • we run (or execute) the program • the Virtual Machine (VM) interprets bytecodeon yourhardware • program control decides what to do next • a program (or application) starts in main method • method's statements are (mostly) executed in sequence • when method is called, control branches to the called method • its statements are executed • when the called method returns control goes back to calling method

  10. Other Concepts: Assignment and Casting • the expression on the right-hand side of = must be compatible withthevariable on the left-hand side of = • their types must match • either be the same • or the expression's type must be converted to the variable's type • some conversions are automatic, e.g.: • doubleprice = 4000; // converted to 4000.0 • String king = "Henry " + 8; // converted to "Henry 8" • casting is a manual kind of type conversion • to cast an expression proceed it with the type enclosed in(and) • e.g.: int rounded = (int) (number + 0.5); • typically, casting is necessary if information may be lost • watch out: double fraction = 5 / 9; // is 0! • 5/9 is integer division, discards fractional part!

  11. Programming Errors • syntax errors • each class must be syntactically correct • i.e. obey the grammatical rules of the Java programming language • are discovered by the compiler • easy to correct • Eclipse highlights syntax errors for us continuously • Eclipse suggests error corrections • programming "by error" saves typing time • we can make a conscious error and let Eclipse do the "correction" • run-time errors (exceptions) • occur when program's execution attempts an illegal operation • e.g. division by 0 • harder to correct • logical errors • program runs correctly, but the results are wrong • hardest to correct

  12. Debugging • debugging = correcting run-time and logical errors • to find the place where things went wrong • we must where a variable gets a wrong value • or where a method didn't get called as expected (or did) • either by writing variables' values • to write the value of any variable xto the console, use: • System.out.println ("x=" + x); • this works for any type! • disadvantages: • writing and deleting the statements takes time • program can't be stopped • or with the debugger • included in Eclipse • we can step through the program in smaller or larger steps • defied breakpoints • we can see all variables • disadvantages: • variables are hard to find • starting debugger, stepping through takes time • we can overstep

  13. Class String • represents the category of simple strings of characters • a String object is: • is a sequence of letters, digits, other characters • has implicit numbering: 1st character has index 0 • constant String is enclosed in quotes • e.g. "How'zit Brah" • operator + concatenates two strings • e.g. "How'zit" + "Brah" is "How'zitBrah" • if one operand of + is not a String,it will be converted to a String • some usefull methods: • length(): returns the number of characters • "How'zit Brah".length()returns 12 • toUpperCase(): returns a String where all letters are in uppercase • "How'zit".toUpperCase()returns "HOW'ZIT" • substring(m,n): returns a String of the characters numbered m to n-1 • "How'zit Brah". substring(2,5)returns "w'z"

  14. System, PrintStream, InputStream, Scanner • class System provides objects out and in for console I/O • out is of type PrintStream, which provides methods: • print(String string) writes the string to the console • println()starts a new line on the console • println(String string) writes the string to the console and then starts a new line • inis of type InputStream, which provides methods for reading from console • too primitive to be useful (reads just one character) • class Scanner provides for reading from a stream • use Scanner scanner = new Scanner (System.in); to read fromSystem . in • nextLine()reads a line, returns it as a String • nextInt()reads an integer number, returns it as an int • nextDouble()reads a decimal number, returns it as a double • hasNextInt()returns true if integer can be read, false otherwise • hasNextDouble()returns true if number can be read, false otherwise

More Related