1 / 137

Overview

Overview. The Java Tutorials: Learning the Java Language http://docs.oracle.com/javase/tutorial/index.html Quick Review (Language Basics, Classes, Objects) Interfaces Numbers and Strings Generics Packages What’s Next?. References. The Java Tutorials

len
Download Presentation

Overview

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. Overview • The Java Tutorials: Learning the Java Language • http://docs.oracle.com/javase/tutorial/index.html • Quick Review (Language Basics, Classes, Objects) • Interfaces • Numbers and Strings • Generics • Packages • What’s Next?

  2. References • The Java Tutorials http://docs.oracle.com/javase/tutorial/index.html • Java SE 7 JDK (Windows, Unix/Linux, and Mac) http://www.oracle.com/technetwork/java/javase/downloads/index.html • Java Platform SE 7 Documentation http://docs.oracle.com/javase/7/docs/ • Eclipse IDE for Java EE Developers http://www.eclipse.org/downloads/ • Java Language Specification http://docs.oracle.com/javase/specs/ • Java SE API http://www.oracle.com/technetwork/java/javase/documentation/api-jsp-136079.html

  3. Reading Materials • All materials are based on The Java Tutorials http://docs.oracle.com/javase/tutorial/index.html • Interfaces http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html • Numbers and Strings http://docs.oracle.com/javase/tutorial/java/data/index.html • Generics http://docs.oracle.com/javase/tutorial/java/generics/index.html • Packages http://docs.oracle.com/javase/tutorial/java/package/index.html

  4. Quick Review(Language Basics, Classes, Objects)

  5. Language Basics • Java Reserved Words • Variables • Operators • Expressions • Statements • Blocks • Control Flow Statements

  6. Java Reserved Words • abstract assertboolean break bytecase catch char class const continuedefault do doubleelse enum extendsfalse final finally float forgotoif implements import in instanceof int interfacelongnative new nullpackage private protected publicreturnshort static strictfp super switch synchronizedthis throw throws transient try truevoid volatilewhile

  7. Variables • Instance Variables (Non-Static Fields) • int speed = 0; • Class Variables (Static Fields) • static int numGears = 6; • Local Variables (used in method) • int count = 0; • Parameters (used in method) • public static void main(String[] args)

  8. Naming • Variable names are case-sensitive. • The name must not be a keyword or reserved word. • One word • all lowercase letters (e.g. ratio) • Multiple words • capitalize the first letter of each subsequent word, aka, Camel Case (e.g. currentGear) • Constant value • capitalizing every letter and separating subsequent words with the underscore character (e.g. static final int NUM_GEARS = 6)

  9. Primitive Data Types

  10. Operators

  11. Operators

  12. Expressions An expression is a construct made up of variables, operators, and method invocations. int result = 1 + 2; The data type of the value returned by an expression depends on the elements used in the expression. String helloWorld = “hello” + “world”; You can specify exactly how an expression will be evaluated using ( and ). int result = (x + y) / 100;

  13. Statements • A statement forms a complete unit of execution terminated with a semicolon (;). • Expression statements • Assignment expression: speed = 0; • Method invocations: System.out.println(“hello”); • Object creation expressions Point originOne = new Point(23, 94); • Declaration statements int speed = 0; • Control flow statements if (x > y) { ... }

  14. Blocks • A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. if (x > 1) { System.out.println(“true.”); } else { System.out.println(“false.”); }

  15. Control Flow Statements • if • switch • while • do • for • break • continue • return • try/throw

  16. Object-Oriented Programming (OOP)

  17. Object-Oriented Programming (OOP) • Classes • Objects • Inheritance • Interfaces • Abstract Classes • Packages

  18. Object-Oriented Programming (OOP) • A fundamental principle of object-oriented programming: • Encapsulation • Inheritance • Polymorphism

  19. Classes • A class is a blueprint or prototype from which objects are created. • Class header • Class body class MyClass extends MySuperClass implements YourInterface{ // fields (variables) // constructors // methods }

  20. Objects • An object is a software bundle of related behavior (methods) and state (fields). • Referencing an object’s fields. objectReference.fieldName; bike.speed; • Calling an object’s methods. objectReference.methodName(argumentList); bike.setSpeed(10);

  21. Creating Objects • A class provides the blueprint for objects; you create an object from a class. Point originalOne = new Point(23, 40); • The above statement has three parts: • Declaration: variable declarations • Instantiation: the new keyword operator creates the object. • Initialization: the constructor initializes the new object.

  22. Garbage Collector • Garbage collector: The Java VM deletes objects when it determines that they are no longer being used. • An object is eligible for garbage collection when there are no more references to that object. Or, you can explicitly drop an object reference by setting the variable to null.

  23. Nested Classes

  24. Classes • Classes declared outside of any class are known as top-level classes. • Classes declared as members of other classes are called as nested classes.

  25. Nested Classes • There are two kinds of nested classes: • Static member class: declared static • Inner class: non-static member class, anonymous class and local class class OuterClass { static class StaticNestedClass { … } class InnerClass { … } }

  26. Static Member Classes • Static member class is a static member of an enclosing class. • Although enclosed, it does not have an enclosing instance of that class, and cannot access the enclosing class’s instance fields and call its instance methods. • It can access or call static members of the enclosing class, even those members that are declared private.

  27. Non-Static Member Classes • A non-static member class is a non-static member of an enclosing class. • Each instance of the non-static member class’s instance methods can call instance method in the enclosing class and access the enclosing class instance’s non-static fields.

  28. Anonymous Classes • An anonymous class is a class without a name. • It is not a member of it’s enclosing class. • Instead, an anonymous class is simultaneously declared and instantiated any place where it is legal to specify an expression.

  29. Local Class • A local class is a class that is declared anywhere that a local variable is declared. • It has the same scope as a local variable. • A local class instance can access the surrounding scope’s local variables and parameters. • However, the local variable and parameters that are accessed must be declared final.

  30. Why Use Nested Classes? • It’s a way of logically grouping classes that are only used in one place. Nesting such “helper classes” makes their package more streamlined. • It increases encapsulation. class OuterClassA { private int aVar; class InnerClassB { private int bMethod() { return aVar; } } }

  31. Why Use Nested Classes? • Nested classes can lead to more readable and maintainable code. class OuterClassA { private int aVar; class InnerClassB { private int bMethod() { return aVar; } } }

  32. Question • The following program doesn’t compile: public class Problem { String s; static class Inner { void testMethod() { s = "Set from Inner"; } } } • What do you need to do to make it compile?

  33. Answer • Delete static in front of the declaration of the Inner class. An static inner class does not have access to the instance fields of the outer class.

  34. Enum Types • The constructor for an enum type must be package-private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself. public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

  35. Enum Types • An enum type is a type whose fields consist of a fixed set of constants. • The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods, e.g. values method, when it creates an enum. public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

  36. Annotations • Provide data about a program that is not part of the program itself. • Apply to program’s declarations of classes, fields, methods, and other program elements. • Appear first and may include elements with named or unnamed values. @SuppressWarnings(value = “unchecked”) @SuppressWarnings(“unchecked”) @Override @Deprecated

  37. Defining Annotation Type import java.lang.annotation.*; @Documented @interface ClassPreamble { String author(); } @ClassPreamble ( author = "John Doe" } Public class MyClass { }

  38. Interfaces

  39. Interfaces • An interface is a contract between a class and the outside world. • When a class implements an interface, it promises to provide the behavior published by that interface. • An interface is not a class. • Writing an interface is similar to writing to a class, but they are two difference concepts: • A class describes the attributes and behaviors of an object. • An interface contains behaviors that a class implement.

  40. Interfaces vs Classes • An interface is not a class. • Writing an interface is similar to writing to a class, but they are two difference concepts: • A class describes the attributes and behaviors of an object. • An interface contains behaviors that a class implement.

  41. Interfaces and Classes: Similarities • An interface can contain any number of methods. • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. • The bytecode of an interface appears in a .class file. • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

  42. Interfaces and Classes: Differences • You cannot instantiate an interface. • An interface does not contain any constructors. • All of the methods in an interface are abstract. • An interface cannot contain instance fields. • The only fields that can appear in an interface must be declared both static and final – constant. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces.

  43. Interfaces • Interface can contain only constants, method signatures, and nested types. There are no method bodies. • All methods declared in an interface are implicitly public. • All constants defined in an interface are implicitly public, static, and final. • An interface declaration consists of modifiers, the keyword interface, the interface name, a comma-separated list of parent interfaces (if any), and the interface body.

  44. Define Interface public interface Bicycle { // constant int MAX_GEARS = 20; // wheel revolutions per minute void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); void changeCadence(int newValue); }

  45. Implements Interface public interface Bicycle { // wheel revolutions per minute void changeGear(int newValue); . . . } class MountainBike implements Bicycle { void changeGear(int newValue) gear = newValue; } . . . } Bicycle Mountain Bike Road Bike Tandem Bike

  46. Rewriting Interfaces • Developed An Interface Public interface DoIt { void doSomething(int i); } • Want to Add A Method Public interface DoIt { void doSomething(int i); int doSomethingElse(String s); } • Add A Method Public interface DoItPlus extends DoIt{ boolean doSomethingElse(String s); }

  47. Interfaces & Multiple Inheritance • The Java programming language does not permit multiple inheritance, but interfaces provide an alternative. • In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

  48. Multiple Interface public interface GroupedInterface extends Interface1, Interface2, Interface3 { // constant declarations // base of natural logarithms double E = 2.718282; // method signatures void doSomething (int i, double x); int doSomethingElse(String s); }

  49. Summary of Interfaces • A protocol of communication between two objects • Contains signatures and constant but not method implementation • A class implements an interface must implement all methods • An interface name can be used anywhere a type can be used

More Related