Learning Java 1: Introduction Christopher Swenson Center for Information Security University of Tulsa 600 S. College Ave T...
Learning Java <ul><li>Based on  Learning Java , by  Niemeyer and Knudsen </li></ul><ul><li>Quick and Dirty overview of Jav...
Overview (Ch. 1–5) <ul><li>Java </li></ul><ul><li>Hello World! </li></ul><ul><li>Using Java </li></ul><ul><li>Java Languag...
Java <ul><li>Modern, Object-Oriented (OO) Language with C-like syntax </li></ul><ul><li>Developed at Sun Microsystems (Jam...
Let’s Go! <ul><li>Standard CLI utilities, although IDEs and GUIs exist (Eclipse, Netbeans) </li></ul><ul><li>Windows: Star...
HelloJava.java <ul><li>public class HelloJava </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args)...
Some Notes <ul><li>public class HelloJava  { … </li></ul><ul><ul><li>Everything in Java must be in a class (container for ...
Comments <ul><li>/* */  C-style comments are also supported </li></ul><ul><li>/** */  are special JavaDoc comments </li></...
Variables <ul><li>String s = “string”; </li></ul><ul><li>int a = -14; </li></ul><ul><li>long b = 5000000000l; </li></ul><u...
Operators <ul><li>Standard arithmetic operators for ints, longs, shorts, bytes, floats and doubles </li></ul><ul><ul><li>+...
Reference Types <ul><li>Reference Types are non-primitive constructs </li></ul><ul><ul><li>Includes  String s </li></ul></...
String <ul><li>Strings are a reference type, but  almost  a primitive in Java </li></ul><ul><li>String s = “this is a stri...
Coercion <ul><li>Coercion = automatic conversion between types </li></ul><ul><li>Up is easy ( int  to  double , anything t...
Expressions and Statements <ul><li>An statement is a line of code to be evaluated </li></ul><ul><ul><li>a = b; </li></ul><...
if-then-else  statements <ul><li>if (bool) stmt1; </li></ul><ul><li>if (bool) stmt1 else statement2; </li></ul><ul><li>if ...
Do-while loops <ul><li>while (bool) stmt; </li></ul><ul><li>do stmt; while (bool); </li></ul><ul><li>boolean stop = false;...
for  loops <ul><li>for  ( prestmt ;  bool ;  stepstmt ;)  stmt ; </li></ul><ul><ul><li>=  {  prestmt ; while ( bool ) {  s...
switch  statement <ul><li>switch  ( int expression ) </li></ul><ul><li>{ </li></ul><ul><li>case   int_constant :   stmt ; ...
Arrays <ul><li>int []  array =  { 1,2,3 } ; </li></ul><ul><li>int []  array =  new int[ 10 ]; </li></ul><ul><li>for (int i...
enum s <ul><li>What about something, like size? </li></ul><ul><ul><li>Pre-Java 1.5,  int small = 1, medium = 2, … </li></u...
Loop breaking <ul><li>break  breaks out of the current loop or switch statement </li></ul><ul><li>while (!stop) { </li></u...
Loop continuing <ul><li>continue  goes back to the beginning of a loop </li></ul><ul><li>boolean stop = false; </li></ul><...
Objects <ul><li>class  Person </li></ul><ul><li>{ </li></ul><ul><li>String name; </li></ul><ul><li>int age; </li></ul><ul>...
Subclassing <ul><li>class Professor  extends  Person </li></ul><ul><li>{ </li></ul><ul><li>String office; </li></ul><ul><l...
Abstract Classes <ul><li>If not all of its methods are implemented (left abstract), a class is called abstract </li></ul><...
Interfaces <ul><li>class MyEvent  implements  ActionListener </li></ul><ul><li>{ </li></ul><ul><li>public void actionPerfo...
Static and final <ul><li>A  static  method or variable is tied to the class, NOT an instance of the class </li></ul><ul><l...
Special functions <ul><li>All classes extend the Object class </li></ul><ul><li>Classes have built-in functions: </li></ul...
Packages <ul><li>Classes can be arranged into packages and subpackages, a hierarchy that Java uses to find class files </l...
Using Packages <ul><li>Compile like normal </li></ul><ul><li>Packages = a directory </li></ul><ul><li>Java has a “classpat...
Permissions <ul><li>public  – accessible by anyone  </li></ul><ul><li>protected  – accessible by anything in the package, ...
Coding Style <ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul...
Tools: Eclipse <ul><li>Popular </li></ul><ul><li>Auto Complete </li></ul><ul><li>Fancy </li></ul><ul><li>Multi-language </...
Tools: NetBeans <ul><li>Another good GUI </li></ul><ul><li>Full-featured </li></ul><ul><li>Java-centric </li></ul>
Tools: JSwat <ul><li>Debugger </li></ul>
emacs
vi
TextPad
Homework <ul><li>Download Java </li></ul><ul><li>Download a GUI and learn it (e.g., Eclipse) </li></ul><ul><li>Implement a...
Upcoming SlideShare
Loading in...5
×

Learning Java 1 – Introduction

4,154

Published on

Basic introduction to Java.

Published in: Technology
2 Comments
7 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,154
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
539
Comments
2
Likes
7
Embeds 0
No embeds

No notes for slide

Learning Java 1 – Introduction

  1. 1. Learning Java 1: Introduction Christopher Swenson Center for Information Security University of Tulsa 600 S. College Ave Tulsa, OK 74104
  2. 2. Learning Java <ul><li>Based on Learning Java , by Niemeyer and Knudsen </li></ul><ul><li>Quick and Dirty overview of Java fundamentals and advanced programming </li></ul><ul><ul><li>Introduction </li></ul></ul><ul><ul><li>Objects and Classes </li></ul></ul><ul><ul><li>Generics, Core Utilities </li></ul></ul><ul><ul><li>Threads, Synchronization </li></ul></ul><ul><ul><li>I/O, Network Programming </li></ul></ul><ul><ul><li>Swing </li></ul></ul>
  3. 3. Overview (Ch. 1–5) <ul><li>Java </li></ul><ul><li>Hello World! </li></ul><ul><li>Using Java </li></ul><ul><li>Java Language </li></ul><ul><li>Objects </li></ul>
  4. 4. Java <ul><li>Modern, Object-Oriented (OO) Language with C-like syntax </li></ul><ul><li>Developed at Sun Microsystems (James Gosling, Bill Joy) in the early 1990s </li></ul><ul><li>Virtual Machine </li></ul><ul><ul><li>Intermediate bytecode </li></ul></ul><ul><ul><li>Extremely portable </li></ul></ul><ul><ul><li>Surprisingly fast (comparable to C++) </li></ul></ul><ul><ul><li>Features HotSpot on-the-fly native compiling </li></ul></ul><ul><li>Safe and Clean </li></ul><ul><ul><li>Dynamic Memory Management </li></ul></ul><ul><ul><li>Robust Error Handling </li></ul></ul><ul><li>http://java.sun.com </li></ul><ul><ul><li>http://java.sun.com/j2se/1.5.0/docs/api/ </li></ul></ul>
  5. 5. Let’s Go! <ul><li>Standard CLI utilities, although IDEs and GUIs exist (Eclipse, Netbeans) </li></ul><ul><li>Windows: Start  Run  “ cmd ” </li></ul><ul><li>UNIX / OS X: Open up a command terminal </li></ul><ul><li>“ javac HelloJava.java ” </li></ul><ul><ul><li>Compiles the file HelloJava.java </li></ul></ul><ul><li>Outputs executable .class files </li></ul>
  6. 6. HelloJava.java <ul><li>public class HelloJava </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(“Hello World!”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Run with: </li></ul><ul><li>java -cp . HelloJava </li></ul><ul><li>Outputs: </li></ul><ul><li>Hello World! </li></ul>
  7. 7. Some Notes <ul><li>public class HelloJava { … </li></ul><ul><ul><li>Everything in Java must be in a class (container for code and data). </li></ul></ul><ul><li>public static void main(String[] args){ … </li></ul><ul><ul><li>This is a method (which contains executable code). </li></ul></ul><ul><ul><li>The function called from the command-line is required to be main . </li></ul></ul><ul><ul><li>Takes a String array (the arguments), and returns nothing ( void ). </li></ul></ul><ul><li>System.out.println(“Hello World!”); </li></ul><ul><ul><li>System is a class that contains a lot of useful system-wide tools, like standard I/O, and its out class represents standard out. </li></ul></ul><ul><ul><li>The println method of out (and all PrintStream objects) prints the String containted within, followed by a newline. </li></ul></ul><ul><li>java -cp . HelloJava </li></ul><ul><ul><li>Invokes the Java Virtual Machine (JVM) to execute the main of the named class ( HelloJava ). </li></ul></ul><ul><ul><li>Searches the current directory for the class file ( -cp . ). </li></ul></ul>
  8. 8. Comments <ul><li>/* */ C-style comments are also supported </li></ul><ul><li>/** */ are special JavaDoc comments </li></ul><ul><ul><li>Allow automatic documentation to be built from source code, using the javadoc utility. </li></ul></ul><ul><li>// C++-style comments are preferred </li></ul><ul><ul><li>Less ambiguity </li></ul></ul><ul><ul><li>Simpler </li></ul></ul>
  9. 9. Variables <ul><li>String s = “string”; </li></ul><ul><li>int a = -14; </li></ul><ul><li>long b = 5000000000l; </li></ul><ul><li>float c = 2.5f; </li></ul><ul><li>double d = -4.0d; </li></ul><ul><li>byte e = 0x7f; </li></ul><ul><li>char f = ‘c’; </li></ul><ul><li>short g = -31000; </li></ul><ul><li>boolean h = true; </li></ul>int s are signed 32-bit long s are signed 64-bit float s are signed 32-bit double s are signed 64-bit byte s are signed 8-bit char s are signed 16-bit short s are signed 16-bit boolean s are 1-bit, either true or false
  10. 10. Operators <ul><li>Standard arithmetic operators for ints, longs, shorts, bytes, floats and doubles </li></ul><ul><ul><li>+ - * / % << >> >>> </li></ul></ul><ul><li>Boolean operators for non-floating point </li></ul><ul><ul><li>& | ^ ~ </li></ul></ul><ul><li>Logical operators for boolean s </li></ul><ul><ul><li>&& || ^^ ~ </li></ul></ul><ul><ul><li>Comparisons generate boolean s </li></ul></ul><ul><ul><ul><li>== < <= > >= != </li></ul></ul></ul><ul><li>Can be suffixed with = to indicate an assignment </li></ul><ul><ul><li>a = a + b  a += b </li></ul></ul><ul><ul><li>++ and -- operators also ( a = a - 1  a-- ) </li></ul></ul><ul><li>Ternary Operator: </li></ul><ul><ul><li>(a >= b) ? c : d  if (a >= b) c else d </li></ul></ul>
  11. 11. Reference Types <ul><li>Reference Types are non-primitive constructs </li></ul><ul><ul><li>Includes String s </li></ul></ul><ul><li>Consist of variables and methods (code) </li></ul><ul><li>Typically identified with capital letter </li></ul><ul><ul><li>Foo bar = new Foo(); </li></ul></ul><ul><ul><li>Use the new keyword to explicitly construct new objects </li></ul></ul><ul><ul><li>Can take arguments </li></ul></ul><ul><ul><li>No need for destructor or to explicitly remove it </li></ul></ul><ul><ul><li>No pointers </li></ul></ul><ul><ul><ul><li>C++: Foo &bar = *(new Foo()); </li></ul></ul></ul>
  12. 12. String <ul><li>Strings are a reference type, but almost a primitive in Java </li></ul><ul><li>String s = “this is a string”; </li></ul><ul><ul><li>No need for new construction </li></ul></ul><ul><ul><li>Overloaded + operator for concatenation </li></ul></ul><ul><ul><ul><li>String s = “a” + “ kitten ”; </li></ul></ul></ul>
  13. 13. Coercion <ul><li>Coercion = automatic conversion between types </li></ul><ul><li>Up is easy ( int to double , anything to String ) </li></ul><ul><ul><li>Down is hard </li></ul></ul><ul><li>int i = 2; </li></ul><ul><li>double num = 3.14 * i; </li></ul><ul><li>String s = “” + num; </li></ul><ul><li>int a = Integer.parseInt(t); </li></ul>
  14. 14. Expressions and Statements <ul><li>An statement is a line of code to be evaluated </li></ul><ul><ul><li>a = b; </li></ul></ul><ul><li>An statement can be made compound using curly braces </li></ul><ul><ul><li>{ a = b; c = d; } </li></ul></ul><ul><ul><li>Curly braces also indicate a new scope (so can have its own local variables) </li></ul></ul><ul><li>Assignments can also be used as expressions (the value is the value of the variable after the assignment) </li></ul><ul><ul><li>a = (b = c); </li></ul></ul>
  15. 15. if-then-else statements <ul><li>if (bool) stmt1; </li></ul><ul><li>if (bool) stmt1 else statement2; </li></ul><ul><li>if (bool) stmt1 else if stmt2 else stmt3; </li></ul><ul><li>if (bool) { stmt1; … ; } … </li></ul><ul><li>if (i == 100) System.out.println(i); </li></ul>
  16. 16. Do-while loops <ul><li>while (bool) stmt; </li></ul><ul><li>do stmt; while (bool); </li></ul><ul><li>boolean stop = false; </li></ul><ul><li>while (!stop) </li></ul><ul><li>{ </li></ul><ul><li>// Stuff </li></ul><ul><li>if (i == 100) stop = true; </li></ul><ul><li>} </li></ul>
  17. 17. for loops <ul><li>for ( prestmt ; bool ; stepstmt ;) stmt ; </li></ul><ul><ul><li>= { prestmt ; while ( bool ) { stmt ; stepstmt ; } } </li></ul></ul><ul><li>for (int i = 0; i < 10; i++) </li></ul><ul><li>{ </li></ul><ul><li>System.out.print(i + “ “); </li></ul><ul><li>} </li></ul><ul><li>Outputs: “ 0 1 2 3 4 5 6 7 8 9 ” </li></ul>
  18. 18. switch statement <ul><li>switch ( int expression ) </li></ul><ul><li>{ </li></ul><ul><li>case int_constant : stmt ; </li></ul><ul><li>break ; </li></ul><ul><li>case int_constant : stmt ; </li></ul><ul><li>case int_constant : stmt ; </li></ul><ul><li>default: stmt ; </li></ul><ul><li>} </li></ul>
  19. 19. Arrays <ul><li>int [] array = { 1,2,3 } ; </li></ul><ul><li>int [] array = new int[ 10 ]; </li></ul><ul><li>for (int i = 0; i < array.length ; i++) </li></ul><ul><li>array [ i ] = i; </li></ul><ul><li>Array indices are from 0 to (length – 1) </li></ul><ul><li>Multi-dimensional arrays are actually arrays of arrays: </li></ul><ul><li>int [][] matrix = new int [3][3]; </li></ul><ul><li>for (int i = 0; i < matrix.length ; i++) </li></ul><ul><li>for (int j = 0; j < matrix[i].length ; j++) </li></ul><ul><li>System.out.println(“Row: “ + i + “, Col: “ + j + “ = “ + matrix [i][j] ); </li></ul>
  20. 20. enum s <ul><li>What about something, like size? </li></ul><ul><ul><li>Pre-Java 1.5, int small = 1, medium = 2, … </li></ul></ul><ul><ul><li>But what if mySize == -14 ? </li></ul></ul><ul><li>enum Size {Small, Medium, Large}; </li></ul><ul><li>Can also do switch statements on enums </li></ul><ul><li>switch (mySize) { </li></ul><ul><li>case Small: // … </li></ul><ul><li>default: // … </li></ul><ul><li>} </li></ul>
  21. 21. Loop breaking <ul><li>break breaks out of the current loop or switch statement </li></ul><ul><li>while (!stop) { </li></ul><ul><li>while (0 == 0) { </li></ul><ul><li>break ; </li></ul><ul><li>} </li></ul><ul><li>// Execution continues here </li></ul><ul><li>} </li></ul>
  22. 22. Loop continuing <ul><li>continue goes back to the beginning of a loop </li></ul><ul><li>boolean stop = false; </li></ul><ul><li>int num = 0; </li></ul><ul><li>while (!stop) </li></ul><ul><li>{ </li></ul><ul><li>if (num < 100) continue ; </li></ul><ul><li>if (num % 3) stop = true; </li></ul><ul><li>} </li></ul>
  23. 23. Objects <ul><li>class Person </li></ul><ul><li>{ </li></ul><ul><li>String name; </li></ul><ul><li>int age; </li></ul><ul><li>} </li></ul><ul><li>Person me = new Person(); </li></ul><ul><li>me.name = “Bill”; </li></ul><ul><li>me.age = 34; </li></ul><ul><li>Person[] students = new Person[50]; </li></ul><ul><li>students[0] = new Person(); </li></ul>
  24. 24. Subclassing <ul><li>class Professor extends Person </li></ul><ul><li>{ </li></ul><ul><li>String office; </li></ul><ul><li>} </li></ul><ul><li>Professor bob = new Professor(); </li></ul><ul><li>bob.name = “Bob”; </li></ul><ul><li>bob.age = 40; </li></ul><ul><li>bob.office = “U367”; </li></ul><ul><li>However, a class can only “extend” one other class </li></ul>
  25. 25. Abstract Classes <ul><li>If not all of its methods are implemented (left abstract), a class is called abstract </li></ul><ul><li>abstract class Fish </li></ul><ul><li>{ </li></ul><ul><li>abstract void doesItEat(String food); </li></ul><ul><li>public void swim() </li></ul><ul><li>{ </li></ul><ul><li>// code for swimming </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  26. 26. Interfaces <ul><li>class MyEvent implements ActionListener </li></ul><ul><li>{ </li></ul><ul><li>public void actionPerformed(ActionEvent ae) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(“My Action”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Can implement multiple interfaces </li></ul><ul><li>Interfaces have abstract methods, but no normal variables </li></ul>
  27. 27. Static and final <ul><li>A static method or variable is tied to the class, NOT an instance of the class </li></ul><ul><li>Something marked final cannot be overwritten (classes marked final cannot be subclassed) </li></ul><ul><li>class Person </li></ul><ul><li>{ </li></ul><ul><li>public final static int version = 2; </li></ul><ul><li>static long numberOfPeople = 6000000000; </li></ul><ul><li>static void birth() { numberOfPeople++; } </li></ul><ul><li>String name; </li></ul><ul><li>int age; </li></ul><ul><li>} </li></ul><ul><li>// … in some other code </li></ul><ul><li>Person.numberOfPeople++; </li></ul><ul><li>System.out.println(“Running Version “ + Person.version + “ of Person class); </li></ul>
  28. 28. Special functions <ul><li>All classes extend the Object class </li></ul><ul><li>Classes have built-in functions: </li></ul><ul><ul><li>equals(Object obj), hashCode(), toString() </li></ul></ul><ul><li>Equals used to determine if two objects are the same </li></ul><ul><ul><li>o == p – checks their memory addresses </li></ul></ul><ul><ul><li>o.equals(p) – runs o.equals() </li></ul></ul><ul><li>hashCode() – used for hash tables ( int ) </li></ul><ul><li>toString() – used when cast as a String (like printing) </li></ul>
  29. 29. Packages <ul><li>Classes can be arranged into packages and subpackages, a hierarchy that Java uses to find class files </li></ul><ul><li>Prevents naming issues </li></ul><ul><li>Allows access control </li></ul><ul><li>By default, a null package </li></ul><ul><ul><li>Doesn’t work well if you need more than a few classes, or other classes from other packages </li></ul></ul><ul><li>java.io – Base I/O Package </li></ul><ul><ul><li>java.io.OutputStream – full name </li></ul></ul><ul><li>Declare a package with a package keyword at the top </li></ul><ul><li>Import stuff from package with the import keyword </li></ul><ul><ul><li>import java.io.*; </li></ul></ul><ul><ul><li>import java.util.Vector; </li></ul></ul><ul><ul><li>import static java.util.Arrays.sort; </li></ul></ul>
  30. 30. Using Packages <ul><li>Compile like normal </li></ul><ul><li>Packages = a directory </li></ul><ul><li>Java has a “classpath”: root directories and archives where it expects to look for classes </li></ul><ul><li>Example </li></ul><ul><ul><li>java -cp compiled swenson.MyServer </li></ul></ul><ul><ul><li>In the the “compiled” directory, look for a class MyServer in the subfolder “swenson” </li></ul></ul><ul><ul><ul><li>/compiled/swenson/MyServer.class </li></ul></ul></ul><ul><li>Also need to specify -classpath when compiling </li></ul><ul><li>Class files can also be put into ZIP files (with a suffix of JAR instead of ZIP) </li></ul>
  31. 31. Permissions <ul><li>public – accessible by anyone </li></ul><ul><li>protected – accessible by anything in the package, or by subclasses </li></ul><ul><li>(default) – accessible by anything in the package </li></ul><ul><li>private – accessible only by class </li></ul>
  32. 32. Coding Style <ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>if (args.length > 0) </li></ul><ul><li>System.out.println(“We have args!”); </li></ul><ul><li>for (int i = 0; i < args.length; i++) </li></ul><ul><li>{ </li></ul><ul><li>int q = Integer.parseInt(args[i]); </li></ul><ul><li>System.out.println(2 * q); </li></ul><ul><li>} </li></ul><ul><li>System.exit(0); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  33. 33. Tools: Eclipse <ul><li>Popular </li></ul><ul><li>Auto Complete </li></ul><ul><li>Fancy </li></ul><ul><li>Multi-language </li></ul>
  34. 34. Tools: NetBeans <ul><li>Another good GUI </li></ul><ul><li>Full-featured </li></ul><ul><li>Java-centric </li></ul>
  35. 35. Tools: JSwat <ul><li>Debugger </li></ul>
  36. 36. emacs
  37. 37. vi
  38. 38. TextPad
  39. 39. Homework <ul><li>Download Java </li></ul><ul><li>Download a GUI and learn it (e.g., Eclipse) </li></ul><ul><li>Implement a Card class </li></ul><ul><ul><li>enum s for Suit </li></ul></ul><ul><ul><li>hashCode() should return a different value for two different cards, but should be the same for two instances of the same card (e.g., Jack of Diamonds) </li></ul></ul><ul><li>Write a program that builds a deck and deals 5 cards to 4 different players </li></ul><ul><ul><li>toString() should work so you can use System.out.println() to print out a deck, hand, or a card </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×