Core Java Tutorial

Introduction:

Author: Pramod Poduval, Full-stack Developer, Mentor, Catalyst, International Trainer

There are opportunities for learning, securing international certificates (Pearson Standard) without spending much, before you start earning. You may contact me at pramodkpoduval@yahoo.com for more info.

This tutorial is for novices that are interested in software development.

The first step of learning java is setting up a proper IDE (Integrated Development Environment).  Unless you set up a java environment properly, you will not be able to develop Java programs and run them successfully. The following links will help you to do the setup.


JDK
https://www.jetbrains.com/idea/
IntelliJ (IDE)

https://www.jetbrains.com/idea/

Pre-requisites:
You need to be a self-motivator and have awareness about computer programs and computer programming languages.

  1. Introduction to Java:
  2. History
  3. Features
  4. Java Basics:
  5. Identifiers
  6. Modifiers
  7. Variables
  8. Arrays
  9. Enums
  10. Keywords
  11. Comments
  12. Inheritance
  13. Interfaces
  14. Classes & Objects
  15. Objects
  16. Classes
  17. Constructors & Singleton Class
  18. Creating an Instance
  19. Accessing Instance Variables and Methods
  20. Package
  21. Import Statements.
  22. Introduction to Java
    History
    A programming language developed by Sun Microsystems in 1995 which was initiated by James Gosling, Java is holding the concept of Write Once, Run Anywhere. SE 8 is the latest release of Java Standard Edition. The latest updates will be available from the Oracle website as it is the current owner of this technology after taking over the Sun Microsystems in 2010.

    2. Features

    Object Orient Programming
    Programing in Java is constructing objects having properties and behaviours. Objects can be extended.  So the java program can be extended.

    Platform Independent
    The byte code compiled of Java program is independent in nature and is interpreted by Java Virtual Machine which will work on any Platform. The compiled code executable on any processor with the help of Java Runtime Environment.


Secure
Java technology has very good features for securities. It is virus free as well as tamper free because of the authentication techniques and encryptions.

Portable
Since the compiled Java Program is platform independent; it can be installed in any platform without having any specific dependency.

De-bugging
The Java IDEs and compilers provide various options for debugging. The compile time errors and run time errors are detectable and mitigatable using the IDEs like Eclipse, IntelliJ etc.

Multithreaded
Multithreading ability makes the Java Applications do multi-tasking entities.  It’s a good feature for developing highly interactive applications.
Other features
There are some more features like high performance, distributed, dynamic etc. You may visit the Oracle website and learn more about the features once you get acquainted with Java technology.

 

  1. Java Basics:
    Case Sensitive – That means identifier Name and name have different meaning in Java.

    Class Names – First letter should be in Upper Case for class names. If multiple words are used to form a name of the class, each successive word’s first letter should be in Upper Case. Example: class SampleClassName
    Function Names – Function names should start with a Lower Case letter. For multiple words each successive word’s first letter should be in Upper Case. Example: public void sampleFunctionName()

    File Name – Name of the class file should exactly match the class name. When saving the file, you should save it using the class name and the extension is ‘.java’. Example: SampleClassName is the class name. Then the file should be saved as SampleClassName.java’
    public static void main(String args[]) – For processing the Java program it starts from the main() method which is a compulsory part of every Java program.

    1. Identifiers
      Identifiers are the name used for classes, methods, variables etc. The following are the things to be taken care while using identifiers:

      1. The identifier should start with a letter, $ or _
      2. Keywords of Java cannot be used as identifier
  • Identifiers are case sensitive
  1. Modifiers
    There are two categories of modifiers to provide or restrict the accessibility of class, method, variables, constants etc. The following are the categories:

    1. Access Modifiers: default, public , protected, private
    2. Non-access Modifiers: final, abstract, strictfp
  2. Variables
    Variables are the reference of the memory for storing a particular typed data. There are three types of variables in Java Programing:

    1. Local Variables
    2. Static Variables
  • Instance Variables

 

  1. Arrays
    Arrays are the containers for storing multiple data of same type.
  2. Enum
    Enum is a way to store pre-defined multiple values and assign to variable of that type.
  3. Keywords
    There are some words reserved in Java which are not to be used as constant or variable or any other identifier names. The following are the keywords:
    abstract, assert, Boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void
  4. Comments
    There are two ways a programmer can add comments to a code segment for explaining the logic behind it. Comments will be ignored on compiling the code in Java.
    use // for single line comment
    use /*for multi-line comments */

 

  1. Inheritance
    The concept of object oriented programming is basically developed for reusing the code segments. Inheritance is a way to do that by deriving a code segment to a class from other classes. This concept allows the programmer to reuse the fields and methods of the existing class without having to rewrite the code in a new class. In this scenario, the existing class is called the superclass and the derived class is called the subclass.

 

  1. Interfaces
    While the concept of inheritance allows the programmer to derive the code from a single parent, another way is developed in Java to cater the requirement of having multiple inheritance by creating Interfaces. In Interface it is possible to define methods which can be used in deriving classes where the implementation of the methods should be done in deriving class (Sub Class or Child Class)
  1. Objects & Classes
    Being an Object-Oriented Language, Java possesses the following fundamental features.

    1. Polymorphism
    2. Inheritance
    3. Encapsulation
    4. Abstraction
    5. Classes
    6. Objects
    7. Instance
    8. Method
    9. Data Parsing

    10. Objects
      Just like any object that we see in the real world, Java objects also have states (Properties) and Behaviours (The functionalities). We can change the state and communicate with the object through methods.
    11. Class

public class SampleClass{
String breed; int ageC String color; void barking(){ } void hungry(){ } void sleeping(){ } }

Class is the blueprint or the code base for creating the instance or the object. The following are the features of a class:

  1. Class contains variables that define properties or states of an object of it.
  2. Class can have functions / methods that decide the behaviours of the object of it.
  3. Class can contain static variables that will be same for all the objects/instances created from it.
  4. Class has constructor/s, a special method that gets called for creating an object out of it.

Scope of variables in a class:

  1. Local variables: Variables created and used inside the methods. Local variables cannot be accessed from other methods or class body.
  2. Instance Variables: Variables defined directly inside the class body can be accessed by any method defined in the same class. The variables also can be accessed through the instances created from the class.
  3. Static variables: Static variables are the variables defined in the class body, the values of which will be accessed or processed directly by the class reference.

Constructors:

Constructors are special methods in a class.  If there is no constructor defined in a class, a default constructor will be invoked to create an instance of that class.  To create an instance of a class, the constructor should be called with a ‘new’ keyword. The name of the constructor should be same as its class name.  There can be multiple constructors with variations arguments passing to it.  It is also possible in Java to create only once instance of a class.  The name of such class is Singleton.
Creating an Instance
Instances of a class is created by calling a constructor of that class by adding the keyword “new”. The object or instance of a class is created by defining a variable of that object type then assigning the object of that class by calling the constructor of the class with a keyword “new” before it.


Accessing Variables and Methods through objects
The variables (properties or states) and methods (behaviours) of an object can be accessed through the instance of the object.