INTRODUCTION FOR JAVA
java is an object-oriented, cross platform, multi-purpose programming language produced by Sun Micro systems. First released in 1995, it was developed to be a machine independent web technology. It was based on C and C++ syntax to make it easy for programmers from those communities to learn. Since then, it has earned a prominent place in the world of computer programming.
FEATURES OF JAVA
Java has many characteristics that have contributed to its popularity:
- Platform independence – Many languages are compatible with only one platform. Java was specifically designed so that it would run on any computer, regardless if it was running Windows, Linux, Mac, Unix or any of the other operating systems.
- Simple and easy to use – Java’s creators tried to design it so code could be written efficiently and easily.
- Multi-functional – Java can produce many applications from command-line programs to applets to Swing windows (basically, sophisticated graphical user interfaces).
- Java does have some drawbacks.
- Since it has automated garbage collection, it can tend to use more memory than other similar languages.
- There are often implementation differences on different platforms, which have led to Java being described as a “write once, test everywhere” system.
- Lastly, since it uses an abstract “virtual machine”, a generic Java program doesn’t have access to the Native API’s on a system directly.
- None of these issues are fatal, but it can mean that Java isn’t an appropriate choice for a particular piece of software.
Java is a programming language created by James Gosling from Sun Micro systems (Sun) in 1991. The target of Java is to write a program once and then run this program on multiple operating systems. The first publicly available version of Java (Java 1.0) was released in 1995. Sun Micro systems was acquired by the Oracle Corporation in 2010. Oracle has now the steer manship for Java. In 2006 Sun started to make Java available under the GNU General Public License (GPL). Oracle continues this project called Open JDK.
Over time new enhanced versions of Java have been released. The current version of Java is Java 1.8 which is also known as Java 8.
Java is defined by a specification and consists of a programming language, a compiler, core libraries and a run time (Java virtual machine) The Java run time allows software developers to write program code in other languages than the Java programming language which still runs on the Java virtual machine. The Java platform is usually associated with the Java virtual machine and the Java core libraries.
The Java language was designed with the following properties:
- Platform independent: Java programs use the Java virtual machine as abstraction and do not access the operating system directly. This makes Java programs highly portable. A Java program (which is standard-compliant and follows certain rules) can run unmodified on all supported platforms, e.g., Windows or Linux.
- Object-orientated programming language: Except the primitive data types, all elements in Java are objects.
- Strongly-typed programming language: Java is strongly-typed, e.g., the types of the used variables must be pre-defined and conversion to other objects is relatively strict, e.g., must be done in most cases by the programmer.
- Interpreted and compiled language: Java source code is transferred into the bytecode format which does not depend on the target platform. These bytecode instructions will be interpreted by the Java Virtual machine (JVM). The JVM contains a so called Hotspot-Compiler which translates performance critical bytecode instructions into native code instructions.
- Automatic memory management: Java manages the memory allocation and de-allocation for creating new objects. The program does not have direct access to the memory. The so-called garbage collector automatically deletes objects to which no active pointer exists.
The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine.
The Java virtual machine is written specifically for a specific operating system, e.g., for Linux a special implementation is required as well as for Windows.
DEVELOPMENT PROCESS WITH JAVA
Java source files are written as plain text documents. The programmer typically writes Java source code in an Integrated Development Environment(IDE) for programming. An IDE supports the programmer in the task of writing code, e.g., it provides auto-formatting of the source code, highlighting of the important keywords, etc.
At some point the programmer (or the IDE) calls the Java compiler (
javac). The Java compiler creates the by tecode instructions. These instructions are stored in
.class files and can be executed by the Java Virtual Machine.
MULTIPLE INHERITANCE METHOD
If a class implements two interfaces and if these interfaces provide the same default method, Java resolves the correct method for the class by the following rules:
- Super class wins always against the super interface – If a class can inherit a method from a super class and a super interface, the class inherits the super class method. This is true for concrete and abstract super class methods. This rule implies that default methods are not used if this method is also declared in the super class chain.
- Sub types win over Super types – If a class can inherit a method from two interfaces, and one is a sub type of the other, the class inherits the method from the sub type.
- In all other cases the class needs to implement the default method.
All interfaces that have only one method are called functional interfaces. Functional interfaces have the advantage that they can be used together with lambda expressions. See What are lambdas? to learn more about lambdas, e.g., the type of lambdas is a functional interface.
The Java compiler automatically identifies functional interfaces. The only requirement is that they have only one abstract method. However, is possible to capture the design intent with a
Several default Java interfaces are functional interfaces:
Java also contains the
java.util.function package which contains functional interfaces which are frequently used such as:
- Predicate<T> – a boolean-valued property of an object
- Consumer<T> – an action to be performed on an object
- Function<T, R> – a function transforming a T to a R
- Supplier<T> – provides an instance of T (such as a factory)
- UnaryOperator<T> – a function from T to T
- BinaryOperator<T> – a function from (T, T) to T
Instance variable is associated with an instance of the class (also called object). Access works over these objects.
Instance variables can have any access control and can be marked
transient. Instance variables marked as final cannot be changed after a value has been assigned to them
Local (stack) variable declarations cannot have access modifiers. Local variables do not get default values, so they must be initialized before they can be used.
final is the only modifier available to local variables. This modifier defines that the variable cannot be changed after the first assignment.
CLASS METHODS AND CLASS VARIABLES:
Class methods and class variables are associated with the class and not an instance of the class, i.e., objects. To refer to these elements, you can use the classname and a dot (“.”) followed by the class method or class variable name.
Class methods and class variables are declared with the
static keyword. Class methods are also called static methods and class variables are also called static variables or static fields.
An example for the usage of a static field is
println of the following statement:
System.out.println("Hello World"). Hereby
out is a static field, an object of type
PrintStream and you call the
println() method on this object.
If you define a static variable, the Java run time environment associates one class variable for a class no matter how many instances (objects) exist. The static variable can therefore be seen as a global variable.
The Java programming language supports lambdas as of Java 8. A lambda expression is a block of code with parameters. Lambdas allows to specify a block of code which should be executed later. If a method expects afunctional interface as parameter it is possible to pass in the lambda expression instead.
The type of a lambda expression in Java is a functional interface.