690 likes | 1.02k Views
Oracle Certified Professional, Java SE 6 Programmer Oracle Certified Professional, Java SE 7 Programmer. Jiafan Zhou. Introduction and Java Basics. Jiafan Zhou. Introduction.
E N D
Oracle Certified Professional, Java SE 6 ProgrammerOracle CertifiedProfessional, Java SE 7 Programmer Jiafan Zhou
Introduction and Java Basics Jiafan Zhou
Introduction • Hello everybody, welcome. My name is Jiafan Zhou, and I will be your teacher for this Java course for the next 12 weeks. • I have been a senior lead developer for many years. I am here not only to teach you how to program in Java, but also many real-life development techniques and good common practice. • Of course, to help you pass the Oracle Certified Professional Java Programmer exam. • Class is from 9:30 to 3:30 every Saturday unless bank holiday week. Lunch break from 12:00 to 1:00. Don’t miss any classes or lunch!! You need to remember the following information: • My email address is zhoujiafan@gmail.com • The course slides are available at: http://jiafanz.no-ip.org/groups/scjp • The source code used in this class is available at the following link:http://jiafanz.no-ip.org/repos/Java/SCJP(NB: upper case SCJP, both username/password: dorset) • This is my personal server, so don’t hack it please!
Before we start… • OCPJP is a really tough professional IT certification exam. • Get yourself certified does not make you a good Java software developer.Sad news, but it is true. • I will teach everything you need to know to pass the certification. • In addition, I will also show you many techniques used in real-life development • I will guide you to the door and you are the one who have to walk in. • Practice, practice and practice. • Mock exam is a must for certification. • You will see a lot of code snippet and write a lot of code. • Think in Java, sleep with Java and probably eat with Java.
Meet the father of Java… • https://www.youtube.com/watch?v=r19P3y1VBiw Any maybe you haven’t seen the father of Linux… • https://www.youtube.com/watch?v=iYWzMvlj2RQ
Oracle (Sun) Java Certification Map • From now on, I will refer Oracle Certified Programmer as SCJP. (I am so used to that name. Many Java programmers know this acronym better) • Once you get certified, you certified for life.
OCP6 or OCP7 • OCP 7 (1Z0-804) • This course targets at both certifications. • You need to decide which one to take. • OCP7 has more topics to cover. (Those are good stuff to learn anyway even you plan to take OCP 6) • OCP 6 (1Z0-851)
Course Syllabus – Week1Introduction and Java Basics • Introduction • Computer Basics • Java Basics • Comments • Javadocs • Java identifiers • Capitalization • Variables • Primitive Types/Object Types • Constant • Interactive program (Scanner) • Casting • Escape sequences
Course Syllabus – Week2OOP – Using objects and methods • Primitive type vs. Object type • Classes vs. Objects • Object Reference • Garbage Collection • Null reference • Method • Variable scope • Overloading • Constructor • Instance member vs. Static member • Order of Initialisation • Static/non-static block • Pass by value • Variable Arguments
Course Syllabus – Week3 • Arrays/Vector • Wrapper • Autoboxing • Method Invocating Order • if /for/while/switch • Shortcut circuit • Bitwise and Bit shift operator
Course Syllabus – Week4 • OOP. • Access Control • Encapsulation Inheritance • Super vs. this • Overriding • JavaBeans • import vs. static import • enumeration
Course Syllabus – Week5 • Exception • Checked vs. Unchecked exception • Multi-catch exception (1.7) • Try-with-resource (1.7) • Polymorphism • Abstract class • Abstract method • Upcast/Downcast
Course Syllabus – Week6 • Interfaces • Pure Abstraction • Inner classes • Anonymous Inner classes
Course Syllabus – Week7 • Threads • To create and start thread • Thread states • start() • yield() • sleep() • join() • Raise condition • Monitor/Synchronisation • Wait/Notify
Course Syllabus – Week8 • Design Patterns • Cohesion/Coupling • Singleton • Factory (Abstract factory, Factory method) • Adapter • Composite • Decorator • Observer • DAO
Course Syllabus – Week9 • I/O framework • java.nio • Serialization • Jar utility • Collections framework • List • Set • Map
Course Syllabus – Week10 • Generics • Generics and Inheritance • Wildcard • ? extends X, ? super Y • Diamond Expression (1.7) • Assertions
Course Syllabus – Week11 • Database application with JDBC • Process SQL with JDBC • RowSet Types • RowSet features • Transaction • Transaction Isolation Level • Advanced Data
Course Syllabus – Week12 • Date • Number • Currency • Exam Preparation
What is computer? • ComputerPerforms computations making logical decisions billions times faster than human beings • HardwarePhysical devices of computer system.CPU, Memory (RAM), Secondary storage devices, input and output devices • SoftwarePrograms that runs on computer system.Operating systems, application software • AlgorithmPrograms runs as per their algorithm, which is a step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem.E.g. Take two input values and add them together. Then display the result on the system monitor. • Measurement unitA byte is 8 bits. A bit is either 0 or 1. Computer only understands 0s and 1s.Binary vs. Decimal vs. Hexadecimal
Any programming language you know? • Basic/Visual Basic • COBOL • Pascal • C (1972) by Dennis Richie • C++ (1985) by Bjarne Stroustrup • C# by Microsoft .NET • Ruby • Perl • Python • PHP • Javascript/AJAX • XML • Java
Where is Java used? • Originally designed for intelligent consumer-electronic devices, such as remote control or TV setup-box • Then used for creating web pages with dynamic content. (Applet) • Nowadays, Java is everywhere: • Develop large-scale enterprise application (online banking) • Enhance WWW server functionality (J2EE, servlet JSP) • Develop video games • Provide applications for mobile phones (Android) • Develop Credit card chips • Java is more than just a programming language, it is a platform.
Key Java Features • Platform Independence • Full featured Object oriented Programming language • Rich Standard Library • Familiar C/C++ like syntax • Garbage Collection • Multithreaded • Built-in Security Many more… Java Slogan: Compile once, run anywhere!
Our first Java program • Lots of concepts, boring. Let’s make our hands dirty to write our first Java program. • The goal is to display "Hello World!" in the output. • You will learn how to compile/run this program from the command line and Eclipse. • Most of the further code will be developed in Eclipse. • I will not cover the installation of Java (JDK/JRE/Javadocs) or Eclipse (Subclipse plugin). So please do it in your own time as a homework.
HelloWorld.java Comments • /** • * This is my first Hello World Java program. • * @author jiafanz • * • */ • publicclass HelloWorld • { • publicstaticvoid main(String[] args) • { • System.out.println("Hello World!"); • } • } • Enter this program manually by your favourite editor and saves it as a file HelloWorld.java (notice the extension and upper/lower cases for the file name) • Open a command line to compile the Java code under the command line:javac HelloWorld.java • Run this Java program under the command line:java HelloWorld NB: Class name begins with an uppercase letter. Class name: classes are the basic components in Java. Everything must be inside a class. NB: Method name begins with an lowercase letter. main() method: entrance door of a Java program,every Java program must have a main() method String literals
Main() is an entrance door Java Program JVM main()
Eclipse IDE • Eclipse is the best IDE in the marketplace to develop a Java application. • Eclipse has many features. The cert does not require you to know these features. But I’ll try to cover all the good techniques I know. (You’ll love this beast) • Shortcut keys...
How does Java work? Compiler (javac HelloWorld.java) HellWorld.java ____________ _______ ____ ___________ ______ JVM(java HelloWorld) 001101010100… HellWorld.class ____________ _______ ____ ___________ ______ Question: Can you see why Java is platform independent?
Another example • /** • * This is another example of Java program. • * To display message of a system log in. • * @author ejiafzh • * • */ • publicclass SystemLogin { • // main() method, entrance of a Java program • publicstaticvoid main(String[] args){ • System.out.println("You are logging to a Cisco router, authorised users only"); • System.out.println("Provide your username/password"); • System.out.println("Violators will be prosecuted to the " • +"fullest extent of the law"); • } • } • Each Java statement ends with a semi-colon, which delimits next statement. • Java application is required to have a "public static void main()" method. • Keywords (like public static void) cannot be used as identifiers. • String literals must be encoded inside double quotes. • It is possible to concatenate two string literals. • Use whitespace, empty line, comments and indentation (ignored by compiler). The curly brackets can either be at the same line or new line. To closing bracket to match the open one.
Keywords • Keywords are reserved words in Java that cannot be used as identifiers. • Keywords are all lower cases.
Identifiers • Identifiers are custom-defined names for classes, methods or variables. • Keywords cannot be used as identifiers. • Class name begins with an uppercase letter (e.g. HelloWorld) • Methods or variable name begins with a lower case letter(e.g. radius, width, height) • Good identifiers will help programmers to understand • Constant uses all uppercases (e.g. PI) • Use camel cases. • Rules to remember:1. Identifier must start with a letter, or a $ character oran underscore.2. Identifier cannot start with a number!3. After the first character, identifiers can contain anycombination of characters
Capitalization • Java is extremely strict on capitalization. • Case sensitive. • Different from some programming languages(like VB) • Same as others C or C++ • Which one do you think is better? • Public != PUBLIC != public • Only public is considered as keyword, theoretically speaking, you can use Public or PUBLIC as identifiers, but DON’T.
10 4.3 20 10.5 x y x y Variables • We have seen classes and methods. Let’s see a variable example. int x =10; What does this Java statement mean? • The variable name is x • The variable type is int. • Visualisation in memory • This defines an integer variable with value of 10 • It can only hold integer values, in other words, it cannot store 4.3(To store floating values, use another type such as double) double y =4.3 • The value of a variable can be changed later on. x =20; y =10.5;
Variable – Strongly typed • Java is a strongly typed programming language • Once the variable type is determined, it is NOT possible to change its type. • int x =10; • int x =20; • double x =10.5; • double x =10.6; • The above code is not valid and will give you a compiling error. • Consider the following code: • int a =2; • int aSquared = a * a; • int i =0; • i = i +1; • int rating;// use default value if it is valid • rating =400;
Primitive Variable Types • Java has 8 primitive types: • float • double • boolean • char • byte • short • int • long • float takes 4 bytes space and double takes 8 bytes of space • boolean (true, false) is useful to determine a condition • char represents a single character, must be encoded inside a single quote. Don’t be confused with String(e.g. char c = ‘a’; String str = “a”) floating point numbers (e.g. 10.3) true or false a single character (e.g. a, b, c) integer numbers (e.g. 5, 20)
How Java stores integer in RAM • Consider a byte:byte b =69; • 64 + 4 + 1 = 69 • 1 byte = 8 bits, each bit has two possibilities, 0 or 1 • 28 = 256Thus, a byte can have 256 values, ranging from -27 to 27 - 1 (-128 to 127) • C/C++ has unsigned data types, Java does not
Constant • Consider the following: finaldouble PI =3.1415926; • The value of PI is supposed to be a constant. • The value of PI can NEVER be changed • Java uses the final keyword to define constants • Unlike non-constant variable naming convention, we use all the UPPER cases and underscores.e.g. MILES_PER_KILOMETER • Try yourself to change this value and what happens?
Self increment/decrement • ++/--Increments/decrement a number variable by 1 • i++ with a statementExecute the statement, then increment • ++i with a statementIncrement the value first, then execute the statement • Consider the following: • publicclass IncrementDecrementDemo { • publicstaticvoid main(String[] args){ • int i1 =100; • System.out.println("i1="+ i1); • i1++; • System.out.println("i1="+ i1); • i1 =100; • System.out.println("i1="+ i1++); • System.out.println("i1="+ i1); • i1 =100; • System.out.println("i1="+++i1); • System.out.println("i1="+ i1); • } • } print increment and print reset, print and then increment reset, increment and then print
boolean • The boolean type has only two values: truefalse • There are some boolean-specific operators • Logical AND is && • Logical OR is || • Logical NOT is ! • Consider this: • publicclass BooleanOperators { • publicstaticvoid main(String[] args){ • boolean b1 =true; • boolean b2 =false; • System.out.println(b1 && b2); • System.out.println(b1 || b2); • System.out.println(!b1); • } • }
Java operators • The following are the common operators for int: • + - / * % • Division is integer division • 6 / 2 yields 3 • 7 / 2 yields 3, not 3.5 • Because everything is an int, the answer is an int • Modulus is % • Returns the remainder • 7 % 2 yields 1 • 6 % 2 yields 0 • Floats and doubles use the same first four operators • + - / * • 7.0 / 2.0 yields 3.5 • 7.0 / 2 yields 3.5 • 7 / 2.0 yields 3.5 • 7 / 2 yields 3
System.out.println() • System.out.println (“result: “ + 3/5); • What does it print? • result: 0 • System.out.println (“result: “ + 5 % 3); • What does it print? • result: 2 • System.out.println (“result: “ + 3/5.0); • What does it print? • result: 0.6 • System.out.println (“result: “ + 3+4.0); • What does it print? • result: 34.0 • System.out.println (“result: “ + (3+4.0)); • What does it print? • result: 7.0
Code Example • Consider this example: • publicclass MathDemo • { • publicstaticvoid main(String[] args) • { • System.out.println(Math.sin(90.0)); • System.out.println(Math.cos(80.0)); • System.out.println(Math.max(80, 90)); • } • } • Math is a JDK utility class allows us to perform useful math calculations. • Think of mathematical functions:sin(), cos(), tan() • Meaning from the Math library, call/invoke the sin() method.
Code Example • Consider this example: • // Purpose: Convert a Celsius temperature to Fahrenheit • publicclass CelsiusToFahrenheit • { • // main(): application entry point • publicstaticvoid main(String[] args) • { • // set Celsius temperature of interest • int celsius =28; • // convert to Fahrenheit equivalent • int fahrenheit =32+((9* celsius)/5); • // display result • System.out.println("Celsius temperature"); • System.out.println(" "+ celsius); • System.out.println("equals Fahrenheit temperature"); • System.out.println(" "+ fahrenheit); • } • }