The phrase "Java Programming" is common these days. It means programming or coding in Java language. Java is a programming language that was developed by Sun Microsystems in the year 1995, since then, Java Programming gained attention of many software developers and simple users. Currently Java is one of the most popular and widely used programming language.

History

The foundations of Java technology was laid in a project called "the Green Project" at Sun Microsystems in 1991. The team who worked on that project was named as the "Green Team" and it comprised of 13 people and was led by James Gosling. This team worked very hard for 18 months and finally achieved the results. They achieved a device-independent programming language code-named "Oak". The first working compiler of Java was read in 1995.

Goal of Java Programming

Java programming provides
  • object-oriented programming methodology
  • platform independence
  • built-in support for using computer networks
  • support to execute code from remote sources in a secure way
Advantages of Java Programming
  • Interactive programming of web-sites (Java-applets)
  • Portable
  • Typically small size of programs
  • Object oriented
  • Very secure and stable
  • Easy to learn (no pointers, no necessary memory management, no multiple inheritance…)
  • Can be run independently from the operating system ((nearly) platform independence)
Java Programming Tools

Java programming can be done in variety of different Integrated Development Environments (IDEs) available these days.
  • Java Development Kit, Standard Edition, Version 1.5
  • Symantec Visual Cafe
  • Borland JBuilder
  • IBM Visual Age for Java
  • Sun Forte for Java
  • Eclipse
  • NetBeans
and many more.


Flavors of Java Programming

Java programming can be done in following flavors of Java.

Java SE: Java SE can be used to develop and deploy Java applications on desktops, servers, embedded and Real-Time environments. Java SE includes classes that support the development of Java Web Services and provides the foundation for Java Platform, Enterprise Edition (Java EE). Two important things in Java SE are Java Runtime Environment (JRE) and Java Development Kit (JDK).

The Java Runtime Environment (JRE) includes libraries which are used by Java Virtual Machine, and other components to run applets and applications written in the Java programming language. Java

Development Kit contains everything that is in the JRE, and also includes compilers and debuggers which are used for developing applets and applications.

Latest Java SE version available is Java 6. It can be downloaded from:
Java SE Downloads

Java EE: Java Enterprise Edition is used to for developing portable, robust, scalable and secure server-side Java applications. Java EE provides web services, component model, management, and communications APIs which can be used in implementing service-oriented architecture (SOA) and next-generation web applications. Java EE is industry standard. Java programming in Java EE can be done in Servlets, JSP, Web Services, Beans, Enterprise Java Beans etc.

Java ME: Java programming language also provides a platform for writing applications for mobile devices. That platform is called Java ME. Java Micro Edition provides a flexible environment for creating applications that can run on mobile devices, personal digital assistants (PDAs), TV set-top boxes, and printers. Java ME provides flexible user interfaces, security and network protocols.

Applications made using Java ME are portable across many devices.

Java Programming compared to other OOP languages
  • No pointers -> Big error source in C++
  • No multiple inheritance
  • No memory management necessary Garbage Collector
  • Fully object-oriented – no hybride programming allowed like in e.g. C++
Platform Independence

Java programming is platform independent. Code is compiled into bytecode. At runtime, this bytecode is interpreted into native code for execution. This platform independence feature of Java programming, has forced many software manufacturers to adopt Java for manufacturing client specific softwares.



Garbage Collection

Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. Talking about garbage collection, Java programming is easier and simple than C and C++. While doing Java programming, coder does not worry about freeing memory after use. The heap of Java virtual machine's,stores all objects created by a running Java application. Java runtime is responsible for managing the object's lifecycle.


Installation of SDK 1.5If you’ll have a look into C:\jdk1.5\...
  • …bin: Contains all tools for development (Compiler, Debugger,…)
  • …demo: Examples
  • …docs: If downloaded, this folder contains the Java-documentation
  • …include: Native interface header files: All C-header-files which have to be integrated into the java-code (e.g. definitions concerning input/outputvariables, time declarations,…)
  • …jre: Java Runtime Environment
  • …lib: All libraries which have to be included when code-compilation takes place
Java Programming Steps
  1. Writing the source code
  2. Saving the source code under file extension java (MyClass.java)
  3. In Microsoft Windows: Open the (black) command window via START>Programs>Utilities
  4. Go to the folder which contains your MyClass.java file
  5. Compile your application/ applet with JDK 1.5:
    Type in javac MyClass.java. A class-file will be generated (MyClass.class) in the same folder
  6. Type in java MyClass. Application will be started
Java programming is case sensitive so take special care. If you make any change in the code, then you have to recompile the code.

Java Programming Basics

Before starting Java programming, following key points should be known:
  • After each expression: a semicolon (;)
  • Java is case-sensitive
  • Floating point literals: Use of point (.) for example 1.456
  • Char-literals: Use of single quotation mark ‘ for example ‘M’
  • String-literals: Use of double quotation marks “ for example “ABC”
  • Comments: // xxx or /* xxx */ multiple line comment
  • Outside class-declarations: Only the instructions "import", "package" and your own comments are allowed! Nothing else.
  • One program can contain one or more class-declarations
  • A class-declaration consists of:
    <Modificator> keyword class <class name>
    {
    <Trunk>
    }
  • Modificator "public" in classes means: class can be "seen" in all other classes
  • Modificator "static" in methods means: method can be used without instantiation.
  • Modificator "void" in methods means: you‘ll get no return-value after method-execution.
  • One source-code file can only contain one public-class.
  • The source-code file has to have the same name as the public-class.
  • The trunk contains variables (let‘s say properties) and methods
  • If the variables are defined within methods, we‘ll have local variables i.e can't be accessed outside the method
  • System.out.println: System.out is also called "reference" - don‘t mix it with reference-variables
JavaProgramming.java

Java Code:
public class JavaProgramming {
    public static void main(String[] args) {
        System.out.println("Java Programming");
    }
}


Object Orientation in Java Programming


Java Programming is based on Object Oriented Approach. If you are starting Java programming, you should be aware of following object oriented concepts.
  • Abstraction: every object can be seen as an abstract actor, that has a state, can perform work, communicates with other objects without revealing how these facilities are implemented
  • Data encapsulation: conceals the exact details of how a particular class works from objects that use its code or send messages to it
  • Inheritance (aka generalization): a way to form new classes using classes that have already been defined → is-a relationship e.g. a "Ford", "Mercedes",... is a "car"
  • Polymorphism: behavior that varies depending on the class in which the behavior is invoked, that is, two or more classes can react differently to the same message


In Java programming, we use operator new for Instantiation.

If you use new:
  • Memory-allocation
  • Call of Constructor
Java programming language provides the option to declare more than one constructor in one class. Decision, which constructor should be used is based on number and types of the arguments.

Example:

Java Code:
public class JavaProgramming {

    public JavaProgramming() {
        System.out.println("Default constructor of JavaProgramming class");
    }

    public JavaProgramming(int a) {
        System.out.println("Constructor expecting integer parameter of JavaProgramming class");
    }
}


public class MyClass {
    public static void main(String []str)
    {
        JavaProgramming obj1 = new JavaProgramming();
        JavaProgramming obj2 = new JavaProgramming(1);
    }
}
Output:

Default constructor of JavaProgramming class
Constructor expecting integer parameter of JavaProgramming class


In Java programming language, constructors are normally used to initialize resources. Remember following points:
  • A certain method which creates your object (instance).
  • They enable you to set start values for instance variables (initiate certain values).
  • You can overload constructors (see: later).
  • They always have the same name as the class
  • They don’t have any return type/ return value (they are void implicitly)
Java programming language provides 3 different kinds of variables.

Local variables are only valid within methods or instruction blocks. Explicit initialization is necessary (so called „Definite Assignment“)!
If no explicit initialization is dine then we get compiler-errors.

Instance variables are valid during object-lifetime. Implicit initialization is done by constructor-method. Theses are visible through modificators (Private, Protected, Public).

Class variables are defined using keyword static. The are Implicitly initialized when class is loaded.

Java programming language provides two types of control structures:
  • looping (for, while, do...while)
  • conditional (if, switch)
First lets talk about If statement.

if statement:
  • if – else statements can be nested
  • else-branch is not mandatory
  • If there is only one statement, brackets can be omitted
  • there is no semicolon after the if or else statement
Java Code:
if(a>10)
        {
            System.out.println("a > 10");            
        }
switch statement:

Few points about switch statement:
  • Actually a shortcut of several if-statements
  • Restricted to int or lower order data types
  • default is optional
  • If more than one statement desired you have to use a block, i.e.{statement1; statement2;...}
  • Without break: after match all cases until the next break or end of switch would be executed (fall through)
  • Sometimes fall-through is intended, but there should be a comment saying that
Java Code:
        switch (a){
        case 1: System.out.println("JavaProgramming: 1");
        break;
        case 2: System.out.println("JavaProgramming: 2");
        break;
        default: System.out.println("JavaProgramming: Default");;
        }
while and do while loops:

While and do-while loops are very similier. While loop is executed 0 to n times where as do-while loop is executed 1 to n times.
  • while-loop is a repelling loop,i.e. loop-condition is checked before every loop-cycle, whereas
  • do/while-loop is an accepting loop, i.e. loopcondition is checked not until end of the loop
  • You can omit the brackets if you have just one statement
  • while and do/while-loops can be transferred into another
  • While and do/while-loops can be nested
  • Observe: end of do/while is marked by a semicolon
Example while loop:

Java Code:
int num = 2;
        int start = 1;
        int end = 10;
        
        while(start <= end)
        {
            System.out.println(num + " * " + start + " = " + (num*start));
            start++;
        }
Output:

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20


Example do while loop:

Java Code:
    int num = 0;
                
        do{
            System.out.println("I am doing Java Programming");
            num--;
        }while(num>1);
Output:

I am doing Java Programming


for loop:

for loop is usually used to go through an array or some data structure.
  • for-loop is a special while loop
  • for-loop is repelling too
  • Brackets can be omitted if just one statement
  • for-loop can be nested
  • In the first and last part of a for-loop you can write a comma ','
  • you can declare multiple variables or write several expressions
Example:

Java Code:
        int num = 2;
        int end = 10;
        
        for(int start=1;start <= end; start++)
        {
            System.out.println(num + " * " + start + " = " + (num*start));
        }
Output will be the same as in while loop example.

I hope this short article Java programming article was useful for you.