Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Introduction to Java


  • Please log in to reply
No replies to this topic

#1 mdebnath

mdebnath

    CC Newcomer

  • Member
  • PipPip
  • 17 posts
  • Programming Language:C, Java, C++
  • Learning:C, Java, C++

Posted 16 February 2013 - 08:35 PM

Let's start with a very simplistic explanation – Java is a programming language just like any other computer language such as C, C++, Perl etc. Computer languages are used to create software. Software is nothing but an assemblage of one or more programs written in the language. In general software developed for a particular purpose or its by product is called an application, so if it is written in Java it is called Java application.

 

Evolution of language

 

Does it mean every programming language is pretty much the same? No, each language has a purpose, strong points and weak points. To draw an analogy – a scissor is not built to do a knife's job, though both may be used to cut your finger ;). For example, C language may be pretty good for low level programming, such as, creating programs like the drivers you install for say, printer. With time it occurred that when the software became big i.e., lines of code written are too many, it's unmanageable. The residual bug or flaws which remain in the program are difficult to pinpoint and rectify/debug. So people started thinking for a way to overcome this weakness, it was found that the problem was not with the language but in the procedural way we think of a certain problem into module and implement them in our logic. So, C evolved to C++, an object oriented version, keeping every feature of its predecessor intact, a super set of C. Here, how to think of a problem got a paradigm shift. Problems are no more isolated into modules rather as an object with some relevant properties and functions.

 

Note: Readers should note that the description given above does not provide any chronological order of language evolution rather an idea on how things changed gradually. If you seek the chronology of language evolution visit Timeline of Programming Language in Wikipedia.

 

Object Oriented is a thought process

 

Suppose you ask, define yourself? In a procedural way I may say, I am a collection of digestive module, auditory module, optical module etc. But when I define in a object oriented way I shall say – I am an entity with the properties, one stomach, two ears, two eyes with a digestive function, an auditory function and an optical function respectively. See, how thought process changed from procedural to object oriented and became more realistic. The same idea applies while writing programs in C++ or Java. They belong to the genre of object oriented languages and work with objects and their interaction. This oversimplified explanation perhaps gives you an idea on how to think in object oriented term.

 

Why Java is different


What makes Java different from any other programming language is that the output of a Java compiler is not an executable code. Rather, it is  bytecode (.class files). Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system called Java Virtual Machine (JVM). A little explanation needed here - pretty much any programming language like C, C++ when compiled gives the output as executable code. This executable code interacts directly with the hardware or operating system to provide the desired output of the application. A java application acts very differently in the scenario. The compiled bytecode may be termed as half compiled executable code in the sense that it acts as an intermediary, without interacting directly either with the hardware or operating system. JVM provides the playground for bytecode and sits between Java application and the operating system.

 

jvm.jpg

 

Why this JVM

 

Modern language such as C, C++ are designed to be compiled to executable code mostly because of performance concerns. Translating a Java program into bytecode makes it easier to run a program in a wide variety of environments. The reason is pretty much straightforward, only the JVM needs to be implemented for each operating system or platform. Once the run-time package exists for a given system, any Java program can run on it. In other words - write a programs in Java, compile it, it will create a .class (which is the bytecode) file, take the class file in any platform such as Windows, Linux, Mac etc. It will run provided there is a JVM installed on those systems. While in case of C or C++, the program has to be compiled every time to run when transitioned from  platform to platform basis. This makes programs written in C or C++, platform specific, on the other hand Java, platform independent.

 

Note: Though Java is platform independent but the details of JVM in which it runs differs from platform to platform. The JVM which you may have installed in Windows will not install in Linux. Each platform has a specific JVM which is packaged with JDK (java development kit with JRE) or singleton JRE(only the run-time environment). Developers of the Java compiler took all the pains of creating JVM for almost all platforms, you don't have to worry about that.

 

Compiler! What is it?


A compiler is a program, yes it is, just like any other programs like operating system (Windows, Linux, Mac etc.), your favorite word  processor (say, MsWord) or the computer game you play. But the purpose of each of the programs is different. So what is the purpose  of a compiler? Compiler basically translates your source code into target code. Source code may be the code you write in C, C++ or  Java and target code is the executable code (program that runs) may be .exe file for C, C++ and .class files for your Java. OK, but  then, why do we need to translate it after all? Well, you see, computer is a bunch of wires, circuits, electronic equipment, as obvious they do understand only signals in the form of voltages and nothing else. Like the switch used in the table LAMP you have, it functions pretty much as an ON (with signal) and OFF (without signal). Theoretically (rather mathematically), this ON and OFF is represented as 1 and 0. So we can say, computer understands the language of 0 and 1. This type of language is called machine level language. Now, do you think such a dumb machine can really understand such highly sophisticated Java language? Obviously no. Some  mechanism should be there to make it simple for the computer to understand what we instruct. Here, compiler comes into action. In Java when you call javac (which is the java compiler, you don't need to worry about it when using the editor like Eclipse), it takes your java file (source code) as input translates into bytecode (.class files), output, and if there is any error in the source code it gives an error message. That is the job of the compiler, simple.


Note: Compiler does actually more than that otherwise it would have been named a translator rather than a compiler but to start with, perhaps this understanding would suffice. We shall go a little deeper into compiler and different types of error in debugging tutorial down the line.

 

 

 

Quizzes

  1. Compiled Java file contains
    1. executable code
    2. bytecode
    3. classes
    4. None of the above
  2. The concept “write once and run everywhere” in Java is due to
    1. Operating System
    2. bytecode
    3. Hardware
    4. Java compiler
  3. The output of Java compiler is
    1. .exe file
    2. .txt file
    3. .java file
    4. .class file
  4. Extension of Java source code file is
    1. .java
    2. .class
    3. .cpp
    4. .txt
  5. Java source code file name is
    1. Same as the class name
    2. Any name
    3. main
    4. MyClass
  6. Bytecode executes within the purview of
    1. Operating System
    2. Eclipse
    3. JVM
    4. None of the above
  7. At the core computer only understands
    1. 0 and 1
    2. bytecode
    3. Java language
    4. None of the above
  8. If source code is the Java file then target code is
    1. bytecode
    2. executable file
    3. machine level language
    4. All of the above
  9. Compiler acts as a
    1. Operating System
    2. Translator
    3. Software
    4. Program
  10. C++ needs to be compiled to run in every platform to become portable, Java is inherently portable due to
    1. JVM
    2. Java compiler
    3. Object oriented language phenomenon
    4. Operating System
  11. JVM is
    1. Java Virtual Medium
    2. Java Volatile Machine
    3. Java Visual Machine
    4. Java Virtual Machine

Spoiler

   

 

Tasks: Try yourself

  • Get acquainted with the Eclipse environment
  • Find out the location where javac compiler installed in your computer
  • What does the line mean ”...because of performance concerns” in the article?
  • Find out some difference between C++ and Java
  • Find out how assembly language evolved after machine language
  • Find out the characteristics of low, high and middle level languages with some examples

 

 


Previous: Getting Started | Next: Java Application: First program in Java | Back to Table of content
 


Edited by mdebnath, 08 March 2013 - 01:12 AM.

  • 1




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download