Java Fundamentals – 1

Being a interviewer as well as tech lead i have noted many people who believes as they are best in java seems they know how to find code in google and get it work. means certain fundamentals are missing in their knowledge banks. so thought to write article series to cover some stuff which really valuable to java beginners as well as who are willing to start career as a programmer. even you are a senior programmer who missed some basic stuff this may valid to you as well. please take a note that i am not here to teach all java. just certain stuff which are valuable.

what is java

it is simple question but most of peoples are struggled to answer. simple answer ( which is not complete answer) is java is a programming language based on C and C++. java is designed to run on virtual machine based with the concept call “write once run anywhere”. if you are coming from C# background you will find this is very much similar. obviously java is object oriented programming language also NOT platform dependent.

History

father of java is James Gosling and first release made in 1995. in 2004 java 1.5 has been released as java 5 with significant changes. current version when this article produce is java 8

what is JVM

even though java considered as platform independent JVM is highly platform dependent. there are separate JVM for each operation systems. first java compile source code to byte code which can read by any JVM. now JVM convert that byte code the machine code according to each operations systems handle things like networking, storage. biggest advantage here is you are writing code to java specification and no need to worry about how operation system deal with it.

JVM
JVM

 

what you need to clearly understand is JVM is not going to compile .java files. its use byte code of already complied code. one other impotent thing is garbage collector. you don’t need to consider about bout allocation for freeing your memory. garbage collector will do the job automatically. but you needs to help garbage collector to do its job.

write first java program

now you are ready to write first java program. this article will not describe how to install java. once you installed java -version and javac -version output should be equal as following image shows. but these number can be different according to your installed version.


java-version

 

if this output is not giving with your computer problem is in java path. correct that before go ahead.

During my professional life I have seen people who started java with IDE has problem on best practices. because IDE will correct you and you will never learn. biggest disadvantage is when you in high rank of you career like tech lead or architect you may want to fix code without IDE only with just text editor. so I personally always recommend to use text editor until you familiar with language.

lets start our first example with NOTEPAD and usual Hello World

goto command prompt and move to location you want to store your program then type


notepad HelloWorld.java


write following code in notepad and save it.


public class HelloWorld{

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

now from command promt type

 


javac HelloWorld.java

it will create new file call HelloWorld.class now you can run your program as


java HelloWorld

use IDE

there are many IDE you can use to develop java program. but here i am going to consider eclipse which is more famous IDE among java developers. you can download eclipse from its official web site but you need to make sure you are downloading correct version for you jdk. for example if you are using 32 bit jdk then you need to download 32 bit eclipse version

eclipse is workspace based IDE and it will ask to create workspace in first time execute. you can use default one or your customized one. eclipse will create all projects inside that workspace. also you can use multiple workspace based on your requirement.

following images will guide you to create your first program with eclipse

Create new project in eclipse
Create new project in eclipse

 

name new project as "HelloWorld"
name new project as “HelloWorld”

 

add new class

Add new class to java program
Add new class to java program

 

implement new class

Implement new class
Implement new class

 

execute program

execute

Packages

package is use to group similer behavior classes together. if you not created package its belongs to default package (no package). standers way is reverse order is official web site. eg:

  • org.apache
  • com.sun
  • org.w3c

however you can create package the way you want. above one is just a guide. when you create package it will store in respective folder in file system as well. refer the following images

package in file system
package in file system

now you should be good in background of java and how it use. lets discuss about more about java in coming articles

Share this on your world...Share on Facebook
Facebook
Share on Google+
Google+
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
Print this page
Print

One Response to “Java Fundamentals – 1”

  1. Java Fundamentals – 2 | [NOK] - Notes of Krish

    […] before refer this post it is expect to refer Java fundamentals – 1 post. […]

    Reply

Leave a Reply