Join David Gassner for an in-depth discussion in this video Principles and components of Java, part of Java 8 Essential Training.
- From the beginning, Java has followed some clearly stated principles. First of all, it's a simple, object-oriented and familiar language. It's simplicity lies greatly in its consistency. Once you learn how to do one thing in Java, you know how to do it the same throughout the language because it never deviates from the way the language is architected. It's an object-oriented language so once you understand the principles of encapsulation, inheritance and polymorphism and how those are implemented in Java, you'll have a much better sense of how to architect your applications.
For developers who've worked with C-style languages like C and C++, the syntax of Java is very familiar. Java was created to be robust and secure. It's robustness lies greatly in its object-oriented characteristics. Because you're designing everything as an object, everything has methods or functions and properties also known as fields that are members of classes. This lets you create your code in small chunks and it makes it easy to debug and maintain your applications over time.
Java was designed to be portable so that you'd be able to compile it once and then run your application on multiple operating systems and processors. The language was designed to be high-performance. The original version of the Java virtual machine wasn't as fast as C++ applications but over the years, it's been improved enormously. Today Java applications run just as fast as, or sometimes even faster than applications built in C++. Finally, Java was created as an interpreted language, it supports multithreading and it's dynamic.
Interpreted means that the application is compiled to a format that's interpreted at runtime rather than being run directly as machine code, that's what makes the applications portable. It's multithreaded and it makes it easy to build applications that can do more than one thing at a time. And it's dynamic in that it can change data types at runtime so long as those data types are compatible with each other. Let's talk about Java's runtime architecture. Again, it's an interpreted language.
Your text-based code is compiled to bytecode rather than machine language. That's what makes it portable between operating systems. Here's the software stack that's used runtime. You start with the operating system. You can run Java applications on Windows, Mac, Linux, Solaris and any other operating system for which there is a usable Java Virtual Machine. The Java Virtual Machine or JVM that you use most of the time will be the one provided by Oracle, that was originally created by Sun Microsystems.
But there are other JVMs out there including one provided by IBM and one that's provided for Android by Google, and there are many others. On top of the Virtual Machine, you add the core runtime and additional libraries. The core runtime is sometimes called the Java Class Library. It's included with both the JDK and the Java Runtime Environment or JRE. Finally, your application is compiled into bytecode and that bytecode runs on top of all of those other layers.
Let's compare Java to a couple of popular languages. First, C++. Java was originally create by C++ developers and they had in mind improving the developers' lot. Here are some ways in which Java is different from C++. If you're a C developer, all of your code can run on C++ as well. The two languages are compatible with each other, that's not true for Java. Even though Java has syntax that's very similar to C and C++, its code is unique.
It has its own rules and its own syntax. I've mentioned earlier that C++ is complied to native machine language and while that makes it very fast and gives it access to low-level functionality, it means that your application has to be recompiled for each operating system and processor that you want to target. Java is compiled to bytecode. Because C++ is compiled to native machine language, it allows direct calls to the native system. In Java there's an interface called Java Native Interface, or JNI, referred to sometimes as JNI.
It lets you call those native functions through the JVM. If you use JNI, you have to create your own native code, typically with C++. C++ lets you write once and then as long as you followed standard C++ syntax and libraries, you can recompile for each operating system you're targeting. Whereas with Java, you can write and compile once and then run anywhere. Java runs in a protected virtual machine environment, and again, you have to go through that JNI interface to make those low-level system function calls.
This is a standard for building industrial-strength web applications. And there are many JEE application servers out there including some that are paid and some that are free. Java Platform, Micro Edition or ME is the standard for programming micro-controllers, sensors, and mobile devices. For example, the SIM card on most smartphones is programmed in Java. I've mentioned the terms JRE and JDK previously. The JRE is the runtime environment.
In the past, the JRE was installed automatically on common operating systems such as Mac and Windows. Modern distributions of these operating systems come without the JRE so it's up to the user to install it. But the JRE is free and can be downloaded from Oracle individually. There are versions of the JRE for Windows, Linux, OS X, Solaris and other operating systems. The JRE runs all Java applications on the desktop and is also used at runtime in a JEE environment to run server-based applications.
The Java Runtime Environment has reputation for having security issues. It's very hackable but Oracle does its best to keep up with these challenges through issuing regular updates. It's up to the individual computer user or administrator to install these updates to make sure that their systems are secure. Again, mobile operating systems, such as Android, have their own distinct runtimes. They don't use Oracle's JRE. The JDK or the Java SE Development Kit is also available at no cost from Oracle.
It lets you compile and package your Java-based applications and includes these critical command line tools in addition to many others. If you code and compile Java from the command line, you'll need to learn how to use these tools. But if you use one of the common Java IDEs, or Integrated Development Environments, they protect you from having to know the details of how these command line tools operate. So that's a look at the principles and components of the Java programming language.
In this course, I'm going to focus on Java SE, just the programming language. I won't be getting into Enterprise Edition or Micro Edition but those are also critical elements of the Java ecosystem.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files