Signup/Sign In
LAST UPDATED: APRIL 24, 2023

Learn Java Programming from Scratch

Technology #java#programming

    Java is one of the world's most important and widely used computer languages, and it has held this distinction for many years. Unlike some other computer languages whose influence has weared with passage of time, while Java's has grown.

    Java is a high level, robust, object-oriented and a secure and stable programming language but it is not a pure object-oriented language because it supports primitive data types like int, char etc.

    Java is a platform-independent language because it has runtime environment i.e JRE and API. Here platform means a hardware or software environment in which anapplication runs.

    Java codes are compiled into byte code or machine-independent code. This byte code is run on JVM (Java Virtual Machine).

    The syntax is Java is almost the same as C/C++. But java does not support low-level programming functions like pointers. The codes in Java is always written in the form of Classes and objects.

    As of 2020, Java is one of the most popular programming languages in use, especially for client-server web applications.Its has been estimated that there are around nine million Java developers inside the world.


    Creation of Java

    Java was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in 1991. It took 18 months to develop the first working version.

    The initial name was Oak but it was renamed to Java in 1995 as OAK was a registered trademark of another Tech company.


    Application of Java

    Java is widely used in every corner of world and of human life. Java is not only used in softwares but is also widely used in designing hardware controlling software components. There are more than 930 million JRE downloads each year and 3 billion mobile phones run java.

    Following are some other usage of Java :

    1. Developing Desktop Applications
    2. Web Applications like Linkedin.com, Snapdeal.com etc
    3. Mobile Operating System like Android
    4. Embedded Systems
    5. Robotics and games etc.

    Types of Java Application

    Following are different types of applications that we can develop using Java:

    1. Standalone Applications

    Standalone applications are the application which runs on separate computer process without adding any file processes. The standalone application is also known as Java GUI Applications or Desktop Applications which uses some standard GUI components such as AWT(Abstract Windowing Toolkit), swing and JavaFX and this component are deployed to the desktop. These components have buttons, menu, tables, GUI widget toolkit, 3D graphics etc. using this component a traditional software is developed which can be installed in every machine.

    Example: Media player, antivirus, Paint, POS Billing software, etc.

    2. Web Applications

    Web Applications are the client-server software application which is run by the client. Servlets, struts, JSP, Spring, hibernate etc. are used for the development of a client-server application. eCommerce application is also developed in java using eCommerce platform i.e Broadleaf.

    Example: mail, e-commerce website, bank website etc.

    3. Enterprise Application

    Enterprise application is middleware applications. To use software and hardware systems technologies and services across the enterprises. It is designed for the corporate area such as banking business systems.

    Example: e-commerce, accounting, banking information systems etc.

    4. Mobile Application

    For mobile applications, Java uses ME or J2ME framework. This framework are the cross platform that runs applications across phones and smartphones. Java provides a platform for application development in Android too.

    Example: WhatsApp, Xender etc.


    Download JDK

    For running Java programs in your system you will have to download and install JDK kit from here (recommended Java Version is Java 11 but you can download Java 13 or Java 14).


    Setting Java Environment and Classpath

    An Environment variable is a dynamic "object" on a computer that stores a value(like a key-value pair), which can be referenced by one or more software programs in Windows. Like for Java, we will set an environment variable with name "java" and its value will be the path of the /bin directory present in Java directory. So whenever a program will require Java environment, it will look for the java environment variable which will give it the path to the execution directory.


    Setting up path for windows ( 2000/XP/vista/Window 7,8 )

    Assuming that you have installed Java in C:\ Program files/ Java / JDK directory

    Step 1: Right click on my computer and select properties.

    setting classpath in java

    Step 2: Go to the Advance System Settings tab.

    setting classpath in java

    Step 3: Click on Environment Variables button.

    setting classpath in java

    Step 4: Now alter the path variable so that it also contains the path to JDK installed directory.

    setting classpath in java

    For e.g:- Change C:\windows/ system 32. to C:\windows/system 32; C:\program files / Java/ JDK.


    Setting up path for window 95/98/ME

    Assuming that you have installed Java in C:\program files\ java\ JDK directory, do the following:

    Step 1: Edit the C:\autoexec.bat file and add the following line at the end.

    SET PATH =% PATH% C:\ PROGRAM FILE/JAVA/JDK/bin 


    Setting up path for Linux , Unix , Solaris, free BSD

    Step 1: Environment variable path should be set to point where java binaries have been installed. Refer to your shell if you have trouble doing this.

    For Example: If you use bash as your shell, then you would add following line to the end

    bash mc: export PATH=/ Path/to/java 

    We recommend you to download latest Java 11 version. Java 11 is a LTS(Long Term Support) version and currently widely in used. You can refer our step by step installation guide to install the Java 11.

    Recommended Java Version: Download the latest version of JDK

    Java Hello World! Program

    Creating a Hello World Program in Java is not a single line program. It consists of various other lines of code. Since Java is a Object-oriented language so it require to write a code inside a class. Let us look at a simple java program.

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

    Lets understand what above program consists of and its keypoints.

    class : class keyword is used to declare classes in Java

    public : It is an access specifier. Public means this function is visible to all.

    static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.

    void : It is the return type, meaning this function will not return anything.

    main : main() method is the most important method in a Java program. This is the method which is executed, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error.

    String[] args : This represents an array whose type is String and name is args. We will discuss more about array in Java Array section.

    System.out.println : This is used to print anything on the console like printf in C language.


    Steps to Compile and Run your first Java program

    Step 1: Open a text editor and write the code as above.

    Step 2: Save the file as Hello.java

    Step 3: Open command prompt and go to the directory where you saved your first java program assuming it is saved in C drive.

    Step 4: Type javac Hello.java and press Return(Enter KEY) to compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line.

    Step 5: Now type java Hello on command prompt to run your program.

    Step 6: You will be able to see Hello world program printed on your command prompt.


    Ways to write a Java Program

    Following are some of the ways in which a Java program can be written:

    Changing the sequence of the modifiers and methods is accepted by Java.

    Syntax: static public void main(String as[])

    Example:

    class Hello
    {
    	static public void main(String as[])
    	{
    	System.out.println ("Welcome to Studytonight");
    	}
    }

    Now let us see What happens at Runtime

    After writing your Java program, when you will try to compile it. Compiler will perform some compilation operation on your program.

    Once it is compiled successfully byte code(.class file) is generated by the compiler.

    class-file at runtime in Java

    After compiling when you will try to run the byte code(.class file), the following steps are performed at runtime:-

    1. Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
    2. Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
    3. Interpreter reads the byte code stream and then executes the instructions, step by step.
    I have been into tech development for around 9 years now have a good hands on with the languages ike java,python php etc. I have a pretty good interest in Design and Algorithms as well.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS