Java - Basics and First Program part1


Java - Basics and First Program part1


  • Java program contains many of objects that interact with each other via methods. Java coding is usually composed with the following:
Class

  • Defined as a template/container that holds and explains the activities / states of its object.

What is class in java?


Object :

  • Is an instance of a class and have states and activities.

Methods : 

  • Container for code logics where data is handled / modified / updated.

Instance Variables : 

  • Object’s current state is defined by these instance variables as each object has unique set of instance variables

Basic Syntax :

It is very important to keep in mind the following points:

  • Case Sensitivity- Java is case sensitive language, and it should be noted that Hello Worldand helloworld are different.
  • Class Names - The first letter of each word in the class name should be in Upper Case. Example class HelloWorld
  • Method Names– First letter of initial word in the method name should start with a Lower Case letter and for the following words, the first letter should be in Upper Case. Example public void myFirstMethod ().
  • Program File Name– should be the same as the class name with same case and a '.java' extension. Example: Assume 'HelloWorld' is the class name, then filename should be as 'HelloWorld.java'.
  • public static void main(String args[])– Execution starts from the main() method which is a mandatory part of every Java program.


  1. "public" - indicates that main() can be accessed publicly.
  2. "static" - indicates that main() doesn't belong to any specific object and hence its unique.
  3. "void" - indicates that main() returns no value.
  4. "main" - indicates the name of the function.
  5. "String[]"- indicates an array of String.
  6. "args" is the name of the String[] (within the body of main()). Any other name can also be used as it is just a variable name.

Java Identifiers :

  • Class Names, variables and methods are denoted as identifiers. Keynotes on identifier follows:
  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character identifiers can have any combination of characters.
  • A keyword cannot be used as an identifier.
  • Most importantly identifiers are case sensitive.
  • Valid identifiers: age, $salary, _value, __1_value.
  • Invalid identifiers: 123abc, -salary

Java Variables Types :

  1. Local Variables
  2. Class Variables (Static Variables)
  3. Instance Variables (Non-static variables)

Java Keywords :

  • Reserved words or keywords in Java are as follows:


Java Inheritance :

  • Classes’ properties can be acquired from other classes
  • This methodology permits to reutilize the variables and the methods of the parent class without having to rewrite the code in the derived class. 
  • The parent class is named as the “Superclass” and the derived class is named as the “Subclass”.

Java Interfaces :

  • Interface is nothing but an agreement between objects on how to correspond with each other.
  • Interfaces play a major role along with inheritance
  • An interface, quite similar to the parent class, defines the method’s signature
  • These methods can be implemented by its subclass.