Thursday, November 21, 2019

Beginning Java programming with Hello World Example

Beginning Java programming with Hello World Example

The process of Java programming can be simplified in three steps:
  • Create the program by typing it into a text editor and saving it to a file – HelloWorld.java.
  • Compile it by typing “javac HelloWorld.java” in the terminal window.
  • Execute (or run) it by typing “java HelloWorld” in the terminal window.
Below given  program is the simplest program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.

/* This is a simple Java program.
   FileName : "HelloWorld.java". */
class HelloWorld
{
    // Your program begins with a call to main().
    // Prints "Hello, World" to the terminal window.
    public static void main(String args[])
    {
        System.out.println("Hello, World");
    }
}
Output:


Hello, World
The “Hello World!” program consists of three primary components: the HelloWorld class definition, the main method and source code comments. Following explanation will provide you with a basic understanding of the code:
  1. Class definition:This line uses the keyword class to declare that a new class is being defined.
    class HelloWorld 
    
    HelloWorld is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace  {  and the closing curly brace  } .
  2. main method: In Java programming language, every application must contain a main method whose signature is:
    public static void main(String[] args)
    
    public: So that JVM can execute the method from anywhere.
    static: Main method is to be called without object. 
    The modifiers public and static can be written in either order.
    void: The main method doesn't return anything.
    main(): Name configured in the JVM.
    String[]: The main method accepts a single argument: 
              an array of elements of type String.
    Like in C/C++, main method is the entry point for your application and will subsequently invoke all the other methods required by your program.
  3. The next line of code is shown here. Notice that it occurs inside main( ).
    System.out.println("Hello, World");
    
    This line outputs the string “Hello, World” followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. System is a predefined class that provides access to the system, and out is the variable of type output stream that is connected to the console.
  4. Comments: They can either be multi-line or single line comments.
    /* This is a simple Java program. 
    Call this file "HelloWorld.java". */
    
    This is a multiline comment. This type of comment must begin with /* and end with */. For single line you may directly use // as in C/C++.
Important Points :
  • The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class and there is at most one public class which contain main() method.
  • By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.
Compiling the program :
  • After successfully setting up the environment, we can open terminal in both Windows/Unix and can go to directory where the file – HelloWorld.java is present.
  • Now, to compile the HelloWorld program, execute the compiler – javac , specifying the name of the source file on the command line, as shown:
    javac HelloWorld.java 
    
  • The compiler creates a file called HelloWorld.class (in present working directory) that contains the bytecode version of the program. Now, to execute our program, JVM(Java Virtual Machine) needs to be called using java, specifying the name of the class file on the command line, as shown:
    java HelloWorld
    
    This will print “Hello World” to the terminal screen.
In Windows
Capture

Setting up the environment in Java

Setting up the environment in Java :

 
Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, etc.
Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. The latest version is Java 11.
Below are the environment settings for both Linux and Windows. JVM, JRE, and JDK  all three are platform dependent because the configuration of each Operating System is different. But, Java is platform-independent.

There are few things which must be clear before setting up the environment
  1. JDK(Java Development Kit) : JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger.
  2. JRE(Java Runtime Environment) : JRE contains the parts of the Java libraries required to run Java programs and is intended for end users. JRE can be view as a subset of JDK.
  3. JVM: JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms. 

Steps for Setting up Java Environment for Windows
  1. Java8 JDK is available at Download Java 8.
    Click second last link for Windows(32 bit) and last link for Windows(64 bit) as highlighted below.
    Capture
  2. After download, run the .exe file and follow the instructions to install Java on your machine. Once you installed Java on your machine, you have to setup environment variable.
  3. Go to Control Panel -> System and Security -> System.
    Under Advanced System Setting option click on Environment Variables as highlighted below.
    Capture
  4. Now, you have to alter the “Path” variable under System variables so that it also contains the path to the Java environment. Select the “Path” variable and click on Edit button as highlighted below.
    java environment setuo
  5. You will see list of different paths, click on New button and then add path where java is installed. By default, java is installed in “C:\Program Files\Java\jdk\bin” folder OR “C:\Program Files(x86)\Java\jdk\bin”. In case, you have installed java at any other location,then add that path.
    Java environment setup
  6. Click on OK, Save the settings and you are done !! Now to check whether installation is done correctly, open command prompt and type javac -version. You will see that java is running on your machine.
  7. In order to make sure whether compiler is setup, type javac in command prompt. You will see a list related to javac.
Steps for Linux
In Linux, there are several ways to install java. But we will refer to the simplest and easy way to install java using the terminal. For Linux, we will install OpenJDK. OpenJDK is a free and open-source implementation of the Java programming language.
  1. Go to Application -> Accessories -> Terminal.
  2. Type command as below.
    sudo apt-get install openjdk-8-jdk
    
  3. For “JAVA_HOME” (Environment Variable) type command as shown below, in “Terminal” using your installation path…(Note: the default path is as shown, but if you have to install OpenJDK at other locations then set that path.)
    export JAVA_HOME = /usr/lib/jvm/java-8-openjdk
    
  4. For the “PATH” (Environment Value) type command as shown below, in “Terminal” using your installation path…Note: the default path is as shown, but if you have to install OpenJDK at other locations then set that path.)
    export PATH = $PATH:/usr/lib/jvm/java-8-openjdk/bin
    
  5. You are done !! Now to check whether the installation is done correctly, type java -version in the Terminal. You will see that java is running on your machine.
  6. Popular Java Editors/IDE :
    • Notepad/gedit: They are simple text-editor for writing java programs. Notepad is available on Windows and gedit is available on Linux.
    • Eclipse IDE: It is most widely used IDE(Integrated Development Environment) for developing software in java. You can download Eclipse from https://www.eclipse.org/downloads/here

Wednesday, November 13, 2019

Java Programming Language 02 : Approach to start learning java



Java | Approach to start learning Java


Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language.
Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in every nook and corner.

          Brief history about java
  • Java is a simple language: Java is easy to learn and its syntax is clear and concise. It is based on C++ (so it is easier for programmers who know C++). Java has removed many confusing and rarely-used features e.g. explicit pointers, operator overloading etc. Java also takes care of memory management and it also provides an automatic garbage collector. This collects the unused objects automatically.
  • Java is a platform-independent language: The programs written in Java language, after compilation, are converted into an intermediate level language called the bytecode which is apart of the Java platform irrespective of the machine on which the programs run. This makes java highly portable as its bytecodes can be run on any machine by an interpreter called the Java Virtual Machine(JVM) and thus java provides ‘reusability of code’.
  • Java is an object-oriented programming language: OOP makes the complete program simpler by dividing it into a number of objects. The objects can be used as a bridge to have data flow from one function to another. We can easily modify data and function’s as per the requirements of the program.
  • Java is a robust language: Java programs must be reliable because they are used in both consumer and mission-critical applications, ranging from Blu-ray players to navigation systems.
  • Java is a multithreaded language: Java can perform many tasks at once by defining multiple threads. For example, a program that manages a Graphical User Interface (GUI) while waiting for input from a network connection uses another thread to perform and wait’s instead of using the default GUI thread for both tasks. This keeps the GUI responsive.
  • Java programs can create applets: Applets are programs that run in web browsers.
  • Java does not require any preprocessor: It does not require inclusion of header files for creating a Java application.
  • Therefore, Java is a very successful language and it is gaining popularity day by day
    You can download java from 
    https://www.oracle.com/technetwork/java/javase/downloads/index.html
    . Here you will find different versions of java. Choose and download the one compatible with your operating system.

Java programming language tutorial 01 : Introduction



Java is one of the most popular and widely used programming language.
  • Java has been one of the most popular programming language for many years.
  • Java is Object Oriented. However it is not considered as pure object oriented as it provides support for primitive data types (like int, char, etc)
  • The Java codes are first compiled into byte code (machine independent code). Then the byte code is run on Java Virtual Machine (JVM) regardless of the underlying architecture.
  • Java syntax is similar to C/C++. But Java does not provide low level programming functionalities like pointers. Also, Java codes are always written in the form of classes and objects.
  • Java is used in all kind of applications like Mobile Applications (Android is Java based), desktop applications, web applications, client server applications, enterprise applications and many more.

Beginning Java programming with Hello World Example

Beginning Java programming with Hello World Example The process of Java programming can be simplified in three steps: Create the prog...