Principles of Java
Wednesday, September 19, 2012 by Sai
The Java programming language has been developed over the years following very strict principles.
The five principles of Java as declared when Java was created include, first,
that it's a simple, object- oriented, and familiar language.Its simplicity lies greatly in its consistency.Once you learn how to do one thing in Java, you know how to do it the same way throughout the language because it never deviates from the way thelanguage 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 the Java programming language,you'll have a much better sense of how to architect your applications.
And 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.
Its 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.And you create applications by combining multiple classes together.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.
Java was created 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, and today Java applications run just as fast or sometimes even faster than applications built in C++.
And 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 as machine code directly, this is what makes the applications portable.
It's multithreaded, and it makes it easy to build applications that do more than
one thing at the same time.
And it's dynamic, in that it can change data types at runtime as long as those
data types are compatible with each other.
The principles can be summarize as:
Here is the runtime architecture of Java.
Again, it's an interpreted language, the application is compiled to
bytecode rather than machine language, and that's what makes it portable
between operating systems.
Here is the software stat that's used at runtime when you run your application.
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 you use most of the time will be the one provided by
Oracle, the one that was created by Sun Microsystems, but there are other JVMs out there provided by IBM, the Virtual Machine that's provided for Android by Google and 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, and it consists of
all of the functionality that's provided with the core Java developer toolkit.And finally your application runs on top of all that once again as compiled bytecode.
So let's compare Java to a couple of popular languages.
First C++, Java was originally created 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 be run in 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 have mentioned earlier that C++ is compiled to native machine language and
while that makes it very fast and gives it access to low-level functions, 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 is an interface called Java Native Interface sometimes called JNI,that lets you call those native functions through the JVM.
C++ lets you write once and then as long as you followed standard C++ syntax you can recompile for each operating system you are targeting, whereas the principles of Java say that you can write once and 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.Here's a way in which the two languages are very different.C++ requires explicit memory management and uses pointers.C++ applications as a result can have memory leaks and it's up to the developer to make sure that they've sealed up all the holes in their application.
In Java, memory is managed for you.
When you create instances of classes or objects, the Java Virtual Machine automatically allocates the memory. And when you're done with the objects the JVM sweeps up the memory by destroying dereferenced objects, this is called the Garbage Collector.This means that in Java applications you don't have to know specifically how much memory is being used at any given time, you still have to pay attention to making sure that you only create the objects you need.
And in C++ you can use multiple inheritance, this means that when you define an object you can inherit functionality for multiple super classes.
The Java inheritance model is single inheritance so you can only inherit directly from a single superclass.
This makes it easier to figure out where there are problems when your
application has bugs.Let's also compare Java to JavaScript.
Even though the names of these languages share the term Java, they are really
not closely related.Java again wasn't compatible with previous languages even though it borrowed syntax from C.
Java has morphed into a standard known as ECMAScript or ECMAScript.Other languages that are based on the ECMAScript standard include
Adobe'sActionScript 3.0 and JScript from Microsoft and a number of others.Java is compiled to bytecode and interpreted at runtime.
JavaScript is interpreted directly from source code.
Java can make native function calls through the JNI interface, while inJavaScript at least as it's implemented in a browser, is restricted to something
called a browser Sandbox.It can only play in the Sandbox, and it can't make native function calls.
Java is write once, run anywhere.JavaScript is even more portable in the sense that it has brought compatibility in many browsers and many operating systems.Java runs in a protected virtual machine but you have to explicitly have that virtual machine installed.And in a similar fashion JavaScript is executed by the browser and restricted to that browser sandbox for security.
Both languages manage the memory for you, neither requires that you specifically allocate or deallocate memory.
And in terms of inheritance, Java uses a traditional class-based inheritance
where you define classes and then inherit their functionality.
JavaScript uses something called prototype-based inheritance.
This model allows you to add functions, properties, and other functionality to a pre-defined class at runtime, something Java doesn't allow you to do.
So that's a look at the principles of Java and how you might compare this
language to C++ and JavaScript.
In the continuum of languages you might play C++ at the strictest level, Java
somewhere in the middle, and JavaScript at the most dynamic.
And you typically use Java when you want to build applications that rely on that language.