Docsity

Java lecture notes and programming........................., Lecture notes of Java Programming

Partial preview of the text

Download Java lecture notes and programming......................... and more Lecture notes Java Programming in PDF only on Docsity!

Introduction

to the

Java Programming Language

Onur Derin ALaRI, Faculty of Informatics, USI [email protected] {2.11.2011, 7.11.2011, 24.11.2011} cc by sa 3.

Contents

Day 1 ● (^) Basics of the Java Language ● (^) Object-oriented Principles with Java ● (^) Encapsulation ● (^) Inheritance ● (^) Polymorphism ● (^) Exception Handling Day 2 ● (^) Java API ● (^) Some design patterns that are useful to understand the Java API ● (^) Iterator Design Pattern ● (^) Adapter Design Pattern ● (^) Decorator Design Pattern ● (^) Observer Design Pattern ● (^) Strategy Design Pattern ● (^) Composite Design Pattern ● (^) Abstract Factory Design Pattern ● (^) Singleton Design Pattern ● (^) Java Collections Framework ● (^) Data structures ● (^) Algorithms Day 3 ● (^) Input/Output Operations in Java ● (^) Multi-threaded Programming in Java ● (^) GUI Design in Java ● (^) Using an external library ● (^) XML processing in Java

Java Virtual Machine

● (^) Java is compiled into bytecodes ● (^) Bytecodes are high-level, machine-independent instructions for a hypothetical machine, the Java Virtual Machine (JVM) ● (^) The Java run-time system provides the JVM ● (^) The JVM interprets the bytecodes during program execution ● (^) Since the bytecodes are interpreted, the performance of Java programs slower than comparable C/C++ programs ● (^) But the JVM is continually being improved and new techniques are achieving speeds comparable to native C++ code

Major Java Technologies

● (^) J2SE (Java applications, applets) ● (^) J2EE (servlets) ● (^) J2ME (MIDlets) They have different runtime environments but they are all programmed with the Java language.

Difference of Java from C++

● (^) No typedefs, defines or preprocessor ● (^) No header files ● (^) No structures or unions ● (^) No enums ● (^) No functions - only methods in classes ● (^) No multiple inheritance ● (^) No operator overloading (except “+” for string concatenation) ● (^) No automatic type conversions (except for primitive types) ● (^) No pointers

Identifier type Convention Examples Class names Capitalize each word within identifier ConnectionManager Method names Capitalize each word except the first connectPhone Variable names Capitalize each word except the first phoneNumber Constant Capitalize each word with underscores MAX_CONNECTIONS btw words

Naming Conventions

If you comply with these conventions, you make everyone's life easier.

● (^) These slides are accompanied with a lab-skeletons.tar.gz file where all the exercises that will be done during the class are put in a directory structure along with ● (^) a test driver class for the classes you are expected to write, ● (^) Makefile s and ● (^) text files that show the correct output of programs. ● (^) make to compile ● (^) make run to run ● (^) make check to see if your implementation is correct ● (^) make clean to remove *.class files ● (^) See the README file in lab-skeletons.tar.gz for more information

Information on Exercises

// HelloWorld.java class HelloWorld { public HelloWorld() { } public static void main(String[] args) { System.out.println(“Hello World!”); } }

HelloWorld Example (ex.1)

Compiling export PATH=$PATH:/opt/java/bin javac HelloWorld.java java HelloWorld Running

● (^) Garbage collection ● (^) Primitive types: int, double, long, float, boolean ● (^) Anything else is derived from java.lang.Object (as if public class MyExampleClass extends Object) ● (^) Every variable in Java (except primitives) is like a reference in C++ ● (^) Arguments in method calls are passed always by value. (value of reference)

Some Java features

public class Main { public Main() { } public static void main(String[] args) { Point p1 = new Point(1,2); translateBy5(p1); System.out.println(p1.x + " " + p1.y); } public static void translateBy5(Point p) { p.x += 5; p.y += 5; } }

  • Analyze JavaValueReference.java Outputs p1.x=6, p1.y=

Pass-by-value(-of-reference) (ex.3)

Lab exercise (ex.5)

● (^) Convert one of the C++ exercises you have written last week into Java. ● (^) Or convert bubblesort.cpp to BubbleSort.java ● (^) Compile and run your program. ● (^) Check if it works.

Object-Oriented Principles with Java

● (^) Encapsulation : provided by private - protected - public identifiers in class members ● (^) Inheritance : ● (^) provided by extends keyword ● (^) abstract keyword declares methods and classes as abstract ● (^) super() : explicit call for constructing parent ● (^) Polymorphism : ● (^) Method overloading: same method name with different signatures ● (^) Interfaces: interface and implements keyword

Encapsulation

Visibility Modifier Alpha Beta Gamma

public Y Y Y Y

protected Y Y Y N

no modifier Y Y N N

private Y N N N

AlphaSub

For members of Alpha class: See http://www.uni-bonn.de/~manfear/javaprotection.php for a nice example

Inheritance

● (^) Inheritance: ● (^) provided by extends keyword ● (^) abstract keyword declares methods and classes as abstract ● (^) super() : explicit call for constructing parent ● (^) public abstract class Shape { protected double area; protected String name; public abstract double getArea(); public Shape(String name){ this.name = name; } } ● (^) public class Rectangle extends Shape { private double a = 5, b = 6; public Rectangle(String name){ super (name); } public double getArea() { area = a*b; return area; } } ● (^) Shape s = new Rectangle(“Rectangle A”); // up-casting done implicitly. ● (^) s.getArea(); Only single inheritance allowed