- Describe the features of Java
- Describe the real-world applications of Java
- Describe the Java Development Kit (JDK) and the Java Runtime Environment (JRE)
- Java is a modern, strongly-typed, OO programming language
- Invented by Sun Microsystems in 1995
- And it's come a long way since then
- Acquired by Oracle
- Built-in support for:
- Networking and database access
- GUI and internationalization
- Multithreading, security, error handling, and much more
- Syntax based on C/C++
- Very similar to Microsoft .NET C#
- Java Virtual Machine (JVM) executes the code
- Java Runtime Environment (JRE) includes the JVM and other runtime facilities outside the JVM
- Java Core API
- Thousands of built-in classes to help you
- The "built-in support" on the previous page is mostly done via these
- Getting to know them and how to use them is one of your challenges
- But once you know OO and how to read an API, you're there
- Java Standard Edition (Java SE)
- "Foundation" platform - runtime and Core API are in here
- Generally for client-based applications
- Contains some APIs for server-side enterprise systems, too, e.g., JDBC for database access
- Java Enterprise Edition (Java EE)
- Geared toward server-side and multitier enterprise systems
- Adds APIs, e.g., servlet/JSP, EJB, transactions, etc.
- Depends on Java SE
- Java Micro Edition (Java ME)
- For smaller devices - PDAs, cell phones, etc.
- Subset of Java SE (smaller footprint)
- Standardized - Sun (with input from others) specifies the language and its Core API
- Sun was once the owner, but Java is now open source and specified via the Java Community Process (JCP)
- Portable - runs on any platform (without recompiling)
- Sun's motto: "Write Once Run Anywhere"
- But there's lots of different computing platforms out there...
- Unix (lots of different flavors), Windows, MacOS, MVS, VMS, etc.
- So how does Java accomplish this?
- Code runs on the JVM, not directly on the operating system
- The JVM, though, does run on the target operating system
- Java was designed to protect developers from common pitfalls
- Things in other languages deliberately omitted
- No multiple inheritance (can have only one superclass)
- No manipulation of object references to memory locations
- No direct access to memory addresses
- Java was designed to help you write reliable software
- Strongly typed (enforced by compiler)
- Case sensitive
- Compiler-checked exceptions
- Automatic array bounds checking
- Automatic memory management (garbage collection)
- Portable
- Write source file with any text editor
- FirstExample.java
- Your write the class definition (class) in here
- NOTE: classes are named using CamelCase
- Compile it with javac
- javac FirstExample.java
- This will give error(s) or FirstExample.class
- Run it with java
- java FirstExample
- This launches the JVM and executes FirstExample.class
- JRE + command line tools for compiling and other tasks
- Free
- Often obtained from operating system vendor
- Remember, the VM itself is platform-specific
- There is also a JRE-only package
- No compiler or other tools, just the runtime
- If you have compiled Java code, all you need is a JRE to run it
- Vendors that provide Java-based applications usually bundle the JRE with their installer
- Why would an application vendor choose Java for its language?
- Filename must have .java extension
- Contains 1 or more class definitions
- You can put more than one class in a source file, but don't
- Unless you enjoy getting yourself and others confused
- Max of one public class (details later)
- Should only be one class per file anyway
- Classname should match filename, including case
- If public class, classname must match filename
class HelloWorld {
}
- Entry point for every Java application
- Signature is important (args variable name is not critical)
- Memorize it
- The void means main doesn't return any value
- To whom? Who calls main, anyway?
- We'll talk later about public and static
class HelloWorld {
public static void main(String[] args) {
// code goes here
}
}
- Common problems:
- javac YourClass.java
- java YourClass
- path or classpath (more later)
- Multiple JDKs installed on your machine and java -version doesn't give desired one
- Check PATH
- Delete any java.exe and javaw.exe files located other than in JDK installation directory, especially on
Windows (see notes)
- Review: JDK tools (java, javac, etc.) are located by looking on your operating system path
- Which needs to contain <java>\bin
- At runtime, JVM looks for classes on the classpath
- Much like a system path, classpath is a list of directories or JAR files (more on these later)
- If you get errors at runtime that look like "cannot find class" or "class not found," this is a classpath
issue
- Can be specified by CLASSPATH environment variable
- Can be provided to JDK tools with -classpath flag
- java -classpath C:\Student\Java FirstExample
- If you have no classpath, your classpath is implicitly "dot" (.)
(current directory)
- If you do have a classpath, you give up the free "dot" (.)
- So if you want current directory on classpath, put "dot" (.) back in
- If you have a CLASSPATH environment variable and use the -classpath flag to the JDK tools, -classpath
overrides