Object-oriented programming on the Java platform
This two-part tutorial introduces the structure, syntax, and programming paradigm of the Java™ language and platform. You’ll learn the Java syntax you are most likely to encounter professionally and Java programming idioms you can use to build robust, maintainable Java applications.. You’ll get started with creating Java objects and adding behavior to them, and conclude with an introduction to the Java Collections Framework, with considerable ground covered in between.
The Java language
Like any programming language, the Java language has its own structure, syntax rules, and programming paradigm. The Java language’s programming paradigm is based on the concept of object-oriented programming (OPP), which the language’s features support.
The Java language is a C-language derivative, so its syntax rules look much like C’s: for example, code blocks are modularized into methods and delimited by braces ({
and }
), and variables are declared before they are used.
Structurally, the Java language starts with packages. A package is the Java language’s namespace mechanism. Within packages are classes, and within classes are methods, variables, constants, and so on. You’ll learn about the parts of the Java language in this tutorial.
The Java compiler
When you program for the Java platform, you write source code in .java files and then compile them. The compiler checks your code against the language’s syntax rules, then writes out byte codes in .class files. Byte-codes are standard instructions targeted to run on a Java virtual machine (JVM). In adding this level of abstraction, the Java compiler differs from other language compilers, which write out instructions suitable for the CPU chip set the program will run on.
The JVM
At run time, the JVM reads and interprets .class files and executes the program’s instructions on the native hardware platform for which the JVM was written. The JVM interprets the bytecodes just as a CPU would interpret assembly-language instructions. The difference is that the JVM is a piece of software written specifically for a particular platform. The JVM is the heart of the Java language’s “write-once, run-anywhere” principle. Your code can run on any chipset for which a suitable JVM implementation is available. JVMs are available for major platforms like Linux and Windows, and subsets of the Java language have been implemented in JVMs for mobile phones and hobbyist chips.
The garbage collector
Rather than forcing you to keep up with memory allocation (or use a third-party library to do this), the Java platform provides memory management out of the box. When your Java application creates an object instance at run time, the JVM automatically allocates memory space for that object from the heap, which is a pool of memory set aside for your program to use. The Java garbage collector runs in the background, keeping track of which objects the application no longer needs and reclaiming memory from them. This approach to memory handling is called implicit memory management because it doesn’t require you to write any memory-handling code. Garbage collection is one of the essential features of Java platform performance.
The Java Development Kit
When you download a Java Development Kit (JDK), you get — in addition to the compiler and other tools — a complete class library of prebuilt utilities that help you accomplish just about any task common to application development. The best way to get an idea of the scope of the JDK packages and libraries is to check out the JDK API documentation .
The Java Runtime Environment
The Java Runtime Environment (JRE; also known as the Java runtime) includes the JVM, code libraries, and components that are necessary for running programs written in the Java language. It is available for multiple platforms. You can freely redistribute the JRE with your applications, according to the terms of the JRE license, to give the application’s users a platform on which to run your software. The JRE is included in the JDK.