Java Programming Language Format

Java Naming Conventions

Java is a case sensitive programming language, where in java applications there is a separate recognition for lowercase letters and uppercase letters.

abc != ABC

In Java applications, to use lowercase letters and uppercase letters JAVA has provided the following conventions.

1. All Java class names, abstract classes names, interfaces names and enums names must start with uppercase letters and the subsequent symbols are also uppercase letters.

String
StringBuffer
InputStreamReader

2. All Java variable names must start with a lowercase letter and the subsequent symbols must be uppercase letters.

in, out, err
pageContext, bodyContent
tempEmpAddr

3. All Java method names must be started with a lowercase letter and the subsequent symbols must be uppercase letters.

EX:
concat()
forName()
getInputStream()

4. In java, all constant variables must be provided in Uppercase letters only.

EX:
MIN_PRIORITY
NORM_PRIORITY

5. In Java, all package names must be provided in lowercase letters only.

EX:
java.io
java.awt.event
javax.servlet.jsp.tagext

Note: The above conventions are mandatory to follow while using predefined libraries, these are optional for user defined libraries, but suggestible for user defined libraries also.

EX:
String str = new String(“abc”); —-> Valid
Exception e = new Exception(); —--> Valid
Thread t = new Thread(); —--------> Valid
Stringbuffer sb = new stringbuffer(“abc”); —> Invalid, Compilation Error


EX:
class employee{
}
Status: Valid, but not suggestible.
class Employee{
}
Status: Valid and Suggestible.


Java Programming Format

To prepare a simple java program we have to use the following format in java files.

1 Comment Section

In Java application development, before writing code it is convention to provide some description about the project that includes authorName, Objective of the implementation, Project details, module details, ……… in the java file, for this we have to use the comment section where we have to use comments.

Comment: Non executable part in java programming is a comment.

Note: In Java applications, comments and their description is not for executing the code , it is for understanding the developers about the code.  

There are three types of comments in Java.

  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment

Single Line Comment: It allows the description in a single line.

Syntax:
// —------description—------
EX:
class Calculator{
   int add(int fval, int sval){// It will add two integer values.
       Return fval+sval;
   }
}

Multi Line Comment: It allows the complete description in more than one line.

Syntax:
/*
—----
—-Description—---
—----
*/
class Calculator{
   /*
      Method Name : add
      Purpose     : Itadds two integer values
      Parameters  : firstValue, secondValue
      Parameters Types : int, int
      Return Type  : int 
   */
   public int add(int firstValue, int secondValue){
       return firstValue + secondValue;
   } 
}

Generate code Templates in Eclipse IDE

https://stackoverflow.com/questions/7958530/adding-author-name-in-eclipse-automatically-to-existing-files

Documentation Comment: It allows the complete description in more than one page.

Syntax:
/**
  *------
  *------ 
   —--
   —--
  *------
  *------
*/

In Java, Documentation comments are useful to provide some description about the programming elements like variables, methods, classes,…. which we used in the present application while creating API documentation for the present application.

API documentation: API documentation is a document , it may be a text document or it may be a PDF document or it may be an html document or a doc file,… , it will provide some description about the programming elements which we used in the present application.

Note: To provide description about all java predefined elements JAVA has provided API documentation.

https://docs.oracle.com/javase/8/docs/api/  JAVA API Documentation 

To Prepare the API documentation for the applications, JAVA has provided a predefined command in the form of “javadoc”.

javadoc fileName.java

Note: javadoc command is able to prepare API documentation in the form of Html files.

Note: Out of all the types of comments in java , only Documentation comments provided description will be included in the API documentation that we created from the javadoc command. 

Using ‘javadoc’ command 

public class Employee{
//Single Line Comment: It represents Employee Number 
public int eno;
/*Multi Line Comment: It represents Employee Name
*/
public String ename;
/**
*Documentation Comment : It represents Employee Salary
*/
public float esal;
public String eaddr;
// Single Line Comment: It represents Employee constructor without data
public Employee(){
}
/*
Multi Line Comment: It represents Employee Constructor with eno. 
*/
public Employee(int eno){
}
/**
*Documentation Comment: It represents Employee Constructor with eno and ename.
*/
public Employee(int eno, String ename){
}
public Employee(int eno, String ename, float esal){
}
public Employee(int eno, String ename, float esal, String eaddr){
}
// Single Line Comment: It represents to store data in the Database.
public void add(int eno, String ename, float esal, String eaddr){
}
/*
Multi Line Comments : It represents Search an Employee from the Database. 
*/
public void search(int eno){
}
/**
*Documentation Comments: It represents Updating an Employee in the Database.
*/
public void update(int eno, String ename, float esal, String eaddr){
}
public void delete(int eno){
}
}

To generate API documentation, use javadoc executable.

In Java applications, to provide description JAVA 1.5 version has provided a new feature in the form of “Annotations” 

Annotation based javadoc comments

Q) In Java applications, to provide description we have already comments then what is the requirement to use Annotations?

In Java applications, if we provide comments with some description then the Lexical Analyser will remove the comments and its description from java application as part of the compilation, so Comments and its description is available with the Java application up to .java file only, it will not be available to the java applications up to .class file and up to RUNTIME of the applications.

As per the requirements like below, we have to bring the description upto RUNTIME of the application :

  1. To simplify the Debugging process.
  2. To Simplify Testing process.
  3. For the Server side applications.
  4. For the Database related applications

——-

We have to bring description or metadata up to RUNTIME of the applications.

In Java applications, to bring description up to .java files, up to .class files and up to RUNTIME of the applications we have to use Annotations.

Q)In Java applications, to bring description at runtime of the applications already we have XML documents, then what is the requirement to use Annotations?

In Java applications, if we use XML documents then we are able to get the following problems.

  1. We must learn XML tech .
  2. Every time we have to check whether the XML documents are in a well formed format or not.
  3. Every time we have to check whether the XML documents are available at the proper locations or not.
  4. Every time we have to check whether we are using the right parsing mechanism or not  to read data from the XML documents.

To overcome all the above problems we need a Java alternative that is “Annotations”.

EX: 
public class LoginServlet extends HttpServlet{
   —--
}
Web.xml
—------- 
<web-app>
  <servlet>
      <servlet-name>ls</servlet-name>
<servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
       <servlet-name>ls</servlet-name>
       <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

—————————————-
@WebServlet(“/login”)
Public class LoginServlet extends HttpServlet{
}

Note: IN JAVA / J2EE Applications , Annotations are the best replacement for the XML documents.

XML Based Tech              Annotation Based tech
—----------------------------------------------------
1. Up to JDK1.4 —----------------> JDK1.5
2. JDBC3.x —---------------------> JDBC4.x
3. Servlets 2.4 —-----------------> Servlets 3.x
4. Struts 1.x —-------------------> Struts2.x
5. JSF 1.x —-----------------------> JSF 2.x
6. EJBs2.x —-----------------------> EJBs3.x
7. Spring 2.4 —--------------------> Spring 2.5
—-----
—-----

https://www.baeldung.com/javadoc


Package Statement

Def: 

  1. Package the collection of inter related classes and interfaces as a single unit.
  2. Package is a folder that contains .class files which represents the related classes and interfaces.

Advantages:

  1. Modularization
  2. Abstraction
  3. Security
  4. Shareability
  5. Reusability

Types:

  1. Predefined Packages:

These packages are defined by the Java programming Language and provided along with the java software.

EX:
java.io
java.util
java.sql 

  1. User defined Packages  :

These packages are defined by the developers as per their applications requirement.

To define User defined Packages we have to use the following statement.

package packageName;

Where the packageName may be a single name or the combination of the parent package name and child package name.

EX:
package p;
package p1.p2.p3;

In Java applications, to use a package declaration statement then we have to follow the following conditions.

  1. Package Statement must be the first statement in the java file file after the comments section.
  1. Package name must be unique, it must not be shareable and it must not be Reusable.

Q) Is it possible to provide more than one package statement in a single java file?

No, it is not possible to provide more than one package declaration statement in java applications, because a package declaration statement must be the first statement, if we provide more than one package declaration statements then only one package declaration statement must be the first statement and all other package statements are not first statements.

EX:
package p1; —--> Valid
package p2; —--> Invalid
package p3; —--> Invalid

To provide package names, JAVA has provided a suggestion to include the company domain name in reverse in the package name.

www.durgasoft.com
com.durgasoft

EX:
package com.durgasoft.icici.transactions.deposit;
com.durgasoft: Company domain name in reverse.
icici : project name / client name
transactions : Module name. 
deposit: sub module name.


Import Statement

The main purpose of import statements is to import the classes and interfaces of a particular package into the present java file.

Syntaxes :

  1. import packageName.*;

It is able to import all from the specified package.

Ex:
import java.io.*;
import java.util.*;

  1. import packageName.memberName;

It is able to import only the specified member from the specified package.

Ex:
import java.io.BufferedReader;
import java.util.ArrayList;

Note: In Java files, we are able to provide at most one package declaration statement, but we are able to provide any number of import statements.

Ex: 
Filename : abc.java
package p1;    → valid
package p2;    → invalid
package p3;    → invalid
import java.io.*;   → valid
import java.util.*; → valid
import java.sql.*;  → valid

Q) Is it possible to use classes and interfaces of a particular package without importing the respective package?

Yes, it is possible to use classes and interfaces of a particular package without importing respective packages but we have to use “fully qualified names” of the classes and interfaces.

Ex : java.sql.Connection
java.util.ArrayList

Note: Writing classes names and interfaces names along with their package names is called fully qualified names.

A Java program with import statement:

import java.io.*;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

A Java program without import statement:

java.io.BufferedReader br = new java.io.BufferedReader(
new java.io.InputStreamReader(System.in));

Note: In Java applications, it is always suggestible to import packages instead of writing fully qualified names.

Q) Tell me the situation where we must use Fully qualified names for the classes and interfaces in java applications?

In java applications, if we have more than one class with the same name in two different packages and if we use that class in the present java file after importing both the packages then the compiler will get an ambiguous situation to get the class from which package. In this context, to resolve the confusion we must use Fully qualified names to the classes.

EX:

In Java, Date is a class exists in java.util package and java.sql package, if we import both the packages in the present java file and if we use Date class name in the present java file then the compiler will get confusion that to pick the Date class from which package. In this context, to resolve the confusion we have to use Fully qualified names to the Date class.

Note: In Java applications, always it is suggestible to import individual classes when compared with * notation in import statement.

EX:
import java.io.*;
import java.util.*;
import java.sql.*;
public class Test{
   public static void main(String[] args){
     BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
ArrayList al = new ArrayList();
     Connection con = DriverManager.getConnection(“----”);
—------
   }
}

Status: not Suggestible, no clarity about the classes and their packages.

import java.io.BufferedReader;
import java.io.InputStreamReader
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.DriverManager
public class Test{
   public static void main(String[] args){
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    ArrayList al = new ArrayList();
    Connection con = DriverManager.getConnection(“----”);
—------
   }
}

Status: Suggestible, it will give more clarity of the classes and their packages.


Classes / Interfaces section

In Java applications, to represent real world entities, we will use classes and interfaces.

Ex : Student, Employee, Customer, Product, Account, ………
class Employee {
-—-----
}
class Product {
-—-----
}

In Java applications, there are no limitations to write number of classes and interfaces, we are able to write any number of classes and interfaces as per the application requirement.


Main Class section

In Java applications, we are able to write any number of classes as per the application requirement, out of these classes we are able to select a class to provide main() method, here the selected class for main() method is called Main class, it is mandatory in Java applications.

Q) What is the requirement of the main() method in Java applications ?

  1. main() method is an entry point to the JVM to enter into the application and to start application execution.
  2. To provide and manage applications logic which must be recognized and executed by JVM, we have to provide a main() method.
  3. To define starting point and ending point to the application execution we have to use main() method, because main() method starting point is the starting point of the application execution and main() method ending point is the ending point of the application execution.

Ex:

class Test {
public static void main(String[] args) {
………
………
}
}