Tuesday, April 28, 2015

Hello world in Java

Is Java Development Kit installed on your machine?

The path of the JDK (for Windows) by default is the following:

C:\Program Files\Java\jdk<version>

If the above directory is not present, there is a good chance JDK is not installed on your machine (you may want to search for it). Download and install the JDK from Oracle's website (for Windows it is available as an exe installer).

(After installation, you have to set the PATH environment variable in Windows. That is not discussed here. For this, you may refer to any source on the Web. The configuration process differs for each OS.)

When you have done that, open the console and type the following commands:

 C:\Users\Utsav>javac -version  
 javac 1.7.0_71
 C:\Users\Utsav>java -version  
 java version "1.7.0_71"  
 Java(TM) SE Runtime Environment (build 1.7.0_71-b14)  
 Java HotSpot(TM) Client VM (build 24.71-b01, mixed mode, sharing)

If you see the above (or slightly differing) output, you have the JDK set up to begin development.

"Hello world!" in Java

Open the notepad (or your favorite text editor) and type the following program:

 public class HelloWorld {  
     public static void main(String[] args) {  
         System.out.println("Hello world!");  

Don't worry about the details for now. Save the file as HelloWorld.java (note the .java extension). Open the console and change to the directory containing the above file.

Run the following command:

 D:\Dropbox\practice\blog>javac HelloWorld.java  

If the command executes successfully as seen above, it means you have compiled the code and it is ready to be run by the JVM (more on that later). A file with the name HelloWorld.class must have been created in the same directory.

Now run the following command:

 D:\Dropbox\practice\blog>java HelloWorld  
 Hello world!  

The output is "Hello world!" as seen above. Congratulations! You have said "Hello world!" in Java. Feel free to experiment further until you feel ready to move ahead.

Java Applications - The Big Picture

What is Java?

Java is a (object-oriented) programming language. It is a computing platform widely used for development of:
  • Websites
  • Standalone desktop applications
  • Android apps
  • Online games
  • Apps for Internet of things (and the like)
Java comes in 3 flavors:
  1. Java SE (Standard Edition)
  2. Java EE (Enterprise Edition)
  3. Java ME (Micro Edition)
Java SE forms the core part of the Java library. Beginners start with learning Core Java development. It is used in developing console-based (or GUI based) desktop applications and Android applications. Core Java is widely used in developing processing logic for websites (formally called enterprise applications). Java programmers write business logic (eg. calculating mortgage interest) for a website (whereas Web designers build the GUI using HTML/CSS/Javascript)

Java EE is a superset of Java SE. Crudely speaking, it is used in development of (complex) enterprise applications. Note that Core Java is only used for developing business logic. Other requirements include returning a dynamic response to the user, security, scalability and communication with a database etc.

Java ME is a subset of Java SE. It is used for developing applications for small-footprint devices (those with huge constraints on power and memory) like CD players and PDA's.

Friday, April 10, 2015

What is JDK, JRE, JVM and bytecode?

A Java source file has an extension of.java.

This file is compiled to create a .class file which is also called bytecode.

The bytecode is portable and can be executed (using a JVM) on any machine. This is why Java is said to be a WORA (Write Once, Run anywhere) platform.

JVM stands for Java Virtual Machine. It is used to execute the bytecode (created after compiling a Java program).

JDK stands for Java Development kit.

It is used by developers to develop (and test) Java applications.

JVM is a part of the JDK.

JRE stands for Java Runtime Environment.

It is used to run/execute applications in Java (that were built using the JDK). It is installed on most machines (and is used by people world over who use applications made in Java).

JVM is a part of JRE which is a part of JDK.