This blog is under construction

Tuesday, 22 March 2016

Simple Java program to print "Hello World"

Let us write a simple Java program to print "Hello World".

/*
 * HelloWorld.java - simple java program to print "Hello World"
 */

public class HelloWorld {
 public static void main(String args[]) {
     // print "Hello World"
  System.out.println("Hello World");
 }
}


Above is the simple Java program which would print "Hello World" on execution.  While saving the Java program, please make sure that the file has .java extension and the name of the file matches with the name of the class that has main() function.  We have stored the above source code in HelloWorld.java and the file names matches exactly with the name of the class in which main() function resides.

Let us compile the above Java program.  Basically, Java compiler would take Java source code as input and provide us bytecode version of the input Java program as output.  As a result of compiling HelloWorld.java, we will get a class file named HelloWorld.class which contains bytecode of HelloWorld.java.

d:\Java>javac HelloWorld.java

Java Virtual Machine would take the bytecode as input and prints the program output on the console.  Let us execute our Java program by providing our class name HelloWorld as command line argument to Java command or Java application launcher.  As a result of program execution, "Hello World" is printed on output screen.

d:\Java>java HelloWorld
Hello World

/* */ Multi-line comments using which we can be used to provide comments in multiple lines to explain code flow or programming logic. These comments are ignored by the compiler
// Single line comments
public access modifier which helps to control the visibility of classes, methods and other members
class HelloWorld class is the Keyword used to declare class. HelloWorld is the class name
main() Method where the program execution begins
static static is the keyword which can be used on variables, methods or blocks. static methods can be called without instantiation. Here, main() is a static method
void Return type of a function. void is used as return type when a function has nothing to return
System.out.println() System is the built in class. out is the object and println() is a built-in method
{ } open and close brace

Saturday, 19 March 2016

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following.
  • How to install JDK
  • How to run java program without setting java path
  • How to set temporary java path in windows
  • How to set permanent java path in windows

How to install JDK?
  • Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website.
  • Install the downloaded JDK in your PC.

Once JDK is installed, you can run your java program using any method shown below.
  • Having your java source code under jdk/bin, compile the source code and run the program from same directory
  • Setting temporary path for java, compiling the source code and running the program from any location using command prompt
  • Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt

Let us see how to run java program without setting environmental variables.  Once java is installed in your PC, move to the location where java is installed and check whether javac and java executable are present(jdk/bin).  In the below example, Java is installed in the below path.

C:\Java\jdk1.6.0_22\bin


I have a java program named javaexample.java stored under C:\Java\jdk1.6.0_22\bin directory where java compiler and java executable or application launcher are present. Now, let us try to compile and execute our java program.

Launch the command prompt and move to the location where javac.exe and java.exe are located(C:\Java\jdk1.6.0_22\bin).  Then, compile the java source code as shown below.

javac  javaexample.c

where javaexample.c is the file where our source code resides.  As a result of compilation, we will get a class file named javaexample.class under the same directory where the source code is located.



Below is the source code of the program which we compiled above.  And the program would print "Hello World" on execution.

public class javaexample {

    public static void main(String args[]) {
        /* prints "Hello World" on the output screen */
        System.out.println("Hello World");
    }

}

Let us execute the program using java application launcher and check the output.

"Hello World" is printed on our output screen as a result of executing our java program.

How to set temporary java path in windows?
Below are the steps to set the temporary java(JDK) path to compile and execute your java program that is located anywhere in our computer.
  • Command prompt would display javac as unrecognized command when the same is executed without setting path environmental variable.
  • Set the path environmental variable with the location where java compiler(javac) and java application launcher(java) are located.  In my case, javac and java are located in C:\Java\jdk1.6.0_22\bin.  Hence, I am setting the same location as value for path environmental variable in command prompt.  This tells our system about the location of javac and java executable.
  • Run javac in command prompt again and check whether the command is recognized.

Please note that the above set path would be cleared on closing the command prompt. We need to set the path again if we need to compile or execute the program after relaunching command prompt.

How to set permanent java path in windows?
Below are the steps to set permanent java(JDK) path to compile and execute your java program that is located anywhere in our computer.
  • Click Start -> Control panel -> System and Security -> System
  • Click Advanced settings which will open a pop up window named "system properties"
  • Choose "Advanced" tab in system properties
  • Click on "Environment variables" and update your Java(JDK/bin) path.
  • Run javac in command prompt again and check whether the command is recognized.
Trying to execute javac in command prompt without setting path environment variable.

Trying to set the environment variable(Start -> Control panel -> System and Security -> System -> Advanced system settings).

Setting environment variable - Click new under user variable and update PATH variable and its value.  Please don't update system variable.

Checking whether javac command is getting recognized in command prompt.


Introduction to Java


Here, we are going to see few key points on Java programming language and the difference between JDK, JRE and JVM.
  • Java is a high level programming language which can be used to create standalone, web, mobile or enterprise applications.
  • It was developed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems.
  • It was initially named as Oak.  In 1995, Oak was renamed as Java.
  • Java programming language helps us to create applications that can be run on multiple platforms without having to port the code and recompile.  In a nutshell, Java is a portable language.
  • Java is object oriented as it helps us to correlate real world object or examples to java programs
  • It is known for its robustness as it avoid drawbacks in other programming languages like pointer arithmetic or memory management etc.
  • It allows us to create multi-threaded applications where each thread can do separate task.
  • Java was initially owned by Sun Microsystems.  In 2010, Oracle acquired Sun Microsystems.


Before getting into Java tutorials, we may need to learn about JVM, JDK and JRE

What is JVM?
On compiling a Java program, we will get class file as output which consists of bytecodes. And these bytecodes are executed by java runtime system called Java Virtual Machine. It consists of class loader, security engine, bytecode verifier and garbage collector. 
  • Class loader helps to load all the class files that are needed for the program execution.
  • Security engine helps to prevent unauthorized access into the restricted area.
  • Bytecode verifier runs several test on our bytecode to find whether there is any error conditions like stack overflow, stack underflow, invalid type conversion, illegal code access etc.
  • Garbage collector takes care of cleaning the unfreed memory.

What is JRE?
Java Runtime Environment which consists of JVM and Java Application Program interfaces. In other words, its the superset of JVM and it provides environment for running a Java application or program.

What is JDK?
Java development kit provides environment for developers to develop, compile and run Java application. JDK consists of both JVM and JRE.  In other words, it is the superset of JRE.  It provides a large set of tools like complier, interpreter, Java packages, document generator etc.

Saturday, 5 March 2016

Save image in Microsoft Paint without white background

Here, we are going to see how to save image in mspaint without white background
1. Create your own drawing or painting



2. Crop the needed portion of your drawing or painting or image.


Here, I am going to crop the porting inside the red box.


Now, we have cropped the portion that needs to be saved as an image.

5. Clear the screen and paste the image which we cropped earlier.


6. Decrease the screen size by 50%


7. Now, adjust your screen to fit to your image size.



8. Save the image to desired file format(jpeg, png, bmp etc)


9. Go to the saved location and open the file.  Below is the output image of mine

Hope you liked this article!!  For any suggestions, please comment below.