Introduction to Java Programming Design goals Language features Running sample code Tools support A1 1
Origin Java was developed by Sun in the 1990s, as an alternative to C++. Designed to be multi-platform - Compiles to bytecode (IR) that runs on a Java Virtual Machine. - Binary compatibility across platforms (i.e. you can deploy the compiled code to multiple targets and it will run). Object-oriented language. Strongly-typed, static compilation (to bytecode). Strong library support for different application styles and paradigms. Open source under GNU GPL in 2007 Sun and Java acquired by Oracle in 2010 Why? - Supports Win/Mac/Linux desktop + Android 2
Java Tools Installation You will need to install the Java SDK 11 and IntelliJ 2019.3 Community. Java JDK https://adoptopenjdk.net IntelliJ Community https://www.jetbrains.com/idea/download 3
Java Documentation https://docs.oracle.com/en/java/javase 4
Virtualization Java source files (.java extension) - compile to bytecode (.class files) - Java Virtual Machine (JVM) runs it - Supports Just-in-Time (JIT) compilation for near-native performance. JVM can execute code from any language that compiles to JVM bytecode (e.g. Java, Scala, Kotlin) Java runs everywhere by having a JVM for each target platform (Mac, Linux, Windows, Raspberry Pi, Arduino, car OS, toasters…) http://viralpatel.net/blogs/java-virtual- machine-an-inside-story/ 5
Compiling Java Applications Java files contain classes. • Each file must contain a class with the same name as the file. • One class should typically have a main() method Compile java source files using `javac ’ You will often have multiple class files (typically one per .java file) You can compile all at once e.g. `javac *.java’ To run an application Use the `java' command with the name of the class file containing the `main()’ method: // compile // execute 6
Using Libraries Classes are grouped into ”packages” (collection of classes) package keyword to assign source to a package - a package is a subdirectory - It’s also the name of the namespace for a class file There are 3 classes here: Main, SomeClass1, SomeClass2 The package for these Java source files will be net.codebot e.g. net.codebot.Main Use `import` at the top of a source file to import a class from a different package. 7
Java vs. C++? Java “feels” like C++ in some ways… - C++ syntax w/ semi-colons - Classes and “standard” OO features - Static typing: compiler checks your program when its compiled But Java isn’t C++ - No headers! Single file declaration and definition. - No pointers! Reference or value types, but no raw addresses. - No operator overloading. Use methods like equals() or plus() - No support for multiple inheritance. Use interfaces instead. - Garbage collection! No need (or support) for destructors. 8
Hello.java Hello World! class definition static main constructor Things that stand out • Top-level class definition (remember that Filename matches Class name) • Static main method is scoped within the class • No-argument constructor Compiling and running it compiles to Hello.class > javac Hello.java > java Hello executes Hello.class 9
Command-Line Parameters Command-line arguments are passed to the main() method as an array of Strings. - The shell will automatically expand wildcards, so that each filename comes in as a string. - See public/code/04.java/CommandLine 10
Bicycle.java class fields constructor methods main 11
Useful Packages and Packages Package Classes (Examples) Description java.awt Color, Graphics, Font, Base classes for creating user interfaces Graphics2D, event. and for painting graphics and images. javax.swing JFrame, JButton, JList, ”Lightweight" components that works the JToolbar same on all platforms. javafx.* Scene, Stage, Button, ”Lightweight” components that work across all platforms. More “modern” than Swing. java.io File, FileReader, Provides for system input and output FileWriter, InputStream through data streams, serialization and the file system. java.lang Boolean, Integer, String, Provides classes that are fundamental to System, Thread, Math the design of the Java programming language. java.util ArrayList, HashMap, Contains the collections framework, Observable legacy collection classes, event model, … 12
No Pointers! Parameters are passed by value. However, this isn’t always obvious. Primitive types ( int , float , etc.) are allocated on the stack and always passed by value. Objects are allocated on the heap, and the address is passed-by- value - Java uses pass-by-reference semantics, so it appears as- if you’re passing by reference. - Practically, you can treat Java as passing objects by reference. There are no pointer semantics in Java - i.e. no * , no & , no out , no ref both refer to same memory on the heap 13
Inheritance Inherit some methods or fields from a base class (“is a”) Very common in Java to extend and override other classes Example: - “Mountain Bike” is - a “Bike” - Mountain bike inherits speed and gear fields - Mountain bike defines addition field for suspension type Bike Mountain Bike 14
Animals1.java top-level class abstract inner base class inner derived class ” Meow ! “ “ Woof ! “ 15
No Multiple-Inheritance Java only supports single (class) inheritance However, to specify complex class behavior, you can use interfaces An interface represents an API (i.e. it defines functionality) - It represents a set of methods a class must have - You can’t instantiate an interface - Essentially, it’s a pure abstract class Classes can choose to implement an interface - A class that implements an interface must implement *all* of the methods in the interface - A class can implement multiple interfaces, representing different behaviours that the class wants to support (e.g. you might have interfaces indicating that something printable and loggable). 16
Animals2.java interface implementations The interface Pet is like a type 17
Managing Errors You can use things like error codes and check return values in your own code, but system libraries and classes use exception handling. You need to “wrap” library calls in try… catch() blocks like this: 18
How to Start A1? Getting Started Copy A0 into A1 (or create a new project in a1/ directory) Open your project in IntelliJ, and make sure it builds! Make sure that you have a single Java file: FindFiles.java - i.e. class is named FindFiles Structure Read the command line parameters and options 1. Check that they are valid 1. Get the files in the directory that match the given filename or 2. regular expression Print the absolute path of the found files 3. 19
A1 Classes Java has extensive libraries that cover any functionality you will need. Useful classes for A1: - java.lang. String • matches() • equals (String) // do NOT use == since that compares objects - java.io. File • isDirectory() • getParentFile() • listFiles() See Sample Code! 1. Intro project 20
Packaging Java Applications How do you deploy a more complex Java application? 1. Give out all of the .class files (could be hundreds of them!) - i.e. email, flash drive etc. 2. Use `jar’ utility to package the .class files into a single file - Basically a ZIP file with some metadata. e.g. Main.jar - User can execute from the JAR file directly. e.g. java – jar Main.jar 3. Use a third-party utility to build a standalone EXE file (e.g. Launch4j) For this course, we’re not going to package our apps. 21
Coding w. an IDE We’re using IntelliJ 2019.3 in this course. This is an Integrated Development Environment (IDE) that provides: - Better code navigation Yes, you can do some of this in - Code completion vim... but there - Debugging are IDE - Integration with API docs exclusive features we be - Integration with testing framework using! - Simplified build framework For your assignments, you are required to submit source code AND an IntelliJ project that compiles and builds your code. - This is critical when we start building more complex applications (esp. in Android). 24
Recommend
More recommend