Advanced Programming in Java
description
Transcript of Advanced Programming in Java
![Page 1: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/1.jpg)
Advanced Programming in Java
Sadegh AliakbarySharif University of Technology
Fall 2012
![Page 2: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/2.jpg)
Sharif University of Technology 2
AgendaEnumerationsStatic ImportAnnotation
Fall 2012
![Page 3: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/3.jpg)
Enumerations
![Page 4: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/4.jpg)
Sharif University of Technology 4
EnumerationsSuppose you have a class with a few
instancesExample:
Student Type : <BS, MS, PhD>SMS Status : <Sent, Delivered, Not Delivered,
Not Sent>Color : <Blue, Green, Black, Red>
How do you implement it?The class should not be inheritedThe instances are limited: no further instances
Fall 2012
![Page 5: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/5.jpg)
Sharif University of Technology 5
An Implementationfinal class Color{
public static final Color Black = new Color(1);public static final Color Blue = new Color(2);public static final Color Green = new Color(3);public static final Color Red = new Color(4);
private int color;private Color(int i) {this.color = i;}
}
Fall 2012
Applications of a private constructor
![Page 6: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/6.jpg)
Sharif University of Technology 6
Java enumJava introduces enumerations for this
purposeEnumerated Data TypeA simple classenum keyword instead of class or interfaceComma seperated enum instancesenum instances are constantenum Color {
Black, Blue, Green, Red}
Fall 2012
![Page 7: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/7.jpg)
Sharif University of Technology 7
Enumenum Color {Black, Blue, Green, Red
}
final class Color{ public static final Color Black = new Color(); public static final Color Blue = new Color(); public static final Color Green = new Color(); public static final Color Red = new Color();
}Fall 2012
![Page 8: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/8.jpg)
Sharif University of Technology 8
Enum Sampleenum Shape {
Rectangle, Circle, Square}
enum StudentType{BS, MS, PhD
}
Fall 2012
![Page 9: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/9.jpg)
Sharif University of Technology 9
Using EnumsColor color = Color.Black;Shape shape = Shape.Circle;show(shape, color);...private static void show (Shape shape, Color color) {//show a shape with this color...
}Fall 2012
![Page 10: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/10.jpg)
Sharif University of Technology 10
Enum Characteristicsenum types are implicitly final
Can not be a super-classBecause they declare constants that should not
be modifiedInstances are constants
enum constants are implicitly public, static and final
No new instances can be createdobject instantiation of enum types with
operator new results in a compilation error.
Fall 2012
![Page 11: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/11.jpg)
Sharif University of Technology 11
EnumEnum can be a more complex classWith many constructorsAnd fieldsAnd methods
Fall 2012
![Page 12: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/12.jpg)
enum Shape {Rectangle(1), Circle(2), Square(3);
private int number;Shape(int i){ number= i;}public int getNumber(){ return number;}
}
Shape shape = Shape.Circle;print (shape.getNumber());
shape = Shape.valueOf("Rectangle");print(shape.getNumber());
Shape[] shapesArray = Shape.values();
for (Shape s : shapesArray) {print(s.name());
}
try{shape = Shape.valueOf("Pyramid");
}catch(Exception exp){print("Pyramid is not included in Shape");
}
Fall 2012
![Page 13: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/13.jpg)
Sharif University of Technology 13
Static ImportIn order to access static membersQualify references with the class they came from. For example:
double r = Math.sin(Math.PI * theta);static import allows unqualified access to static members without inheriting from the type containing the static
membersimport static java.lang.Math.PI;
orimport static java.lang.Math.*;double r = sin(PI * theta);
Fall 2012
![Page 14: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/14.jpg)
Sharif University of Technology 14
AnnotationAn annotation is a special form of metadata Added to Java source codeClasses, methods, variables, parameters and
packages may be annotatedUnlike Javadoc tags, Java annotations can be
reflective They can be embedded in class files (byte code)May be retained by the Java VM at run-timeExample
@Override, @Deprecated, @ SuppressWarnings, …
Fall 2012
![Page 15: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/15.jpg)
Ergonomy
![Page 16: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/16.jpg)
Sharif University of Technology 16Fall 2012
![Page 17: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/17.jpg)
Sharif University of Technology 17Fall 2012
![Page 18: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/18.jpg)
Sharif University of Technology 18Fall 2012
![Page 19: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/19.jpg)
Sharif University of Technology 19Fall 2012
![Page 20: Advanced Programming in Java](https://reader035.fdocuments.in/reader035/viewer/2022062501/56815f40550346895dce18ed/html5/thumbnails/20.jpg)
Sharif University of Technology 20Fall 2012