Pages

Sunday, 8 May 2022

Naming Conventions and Data Types

Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method, etc. But, it is not forced to follow. So, it is known as convention not rule. 

These conventions are suggested by several Java communities such as Sun Micro systems and Netscape. All the classes, interfaces, packages, methods, and fields of Java programming language are given according to the Java naming convention. If you fail to follow these conventions, it may generate confusion or erroneous code

Advantages of naming conventions in java
By using standard Java naming conventions, you make your code easier to read for yourself and other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.The following are the key rules that must be followed by every identifier:
  • The name must not contain any white spaces.
  • The name should not start with special characters like & (ampersand), $ (dollar), _ (underscore).
  • Rules that should be followed by identifiers.

Class
Syntax: Access modifier class <ClassName>
              {
                methods
                constructors
                fields or variables
                blocks
                nested class
              

o It should start with the uppercase letter.
o It should be a noun such as Color, Button, System, Thread, etc.
o Use appropriate words, instead of acronyms.

Example on class and Object:
public class Main 
{
  int x = 5;
  public static void main(String[] args) 
  {
    Main myObj1 = new Main();  // Object 1
    Main myObj2 = new Main();  // Object 2
    System.out.println(myObj1.x);
    System.out.println(myObj2.x);
  }
}

output:
5
5

Object:
  1. object is an instance of class
  2. object is real world entity
  3. object occupies memory
object consists of
  • identity i.e name
  • state/attribute i.e color,age
  • behaviour i.e eat,run
Methods used to create an object : The following 5 ways are used to create an object, they are
1.new keyword
2.new instance() method
3.clone() method
4.deserialization
5.factory method

How to create an object using new keyword
Declaration    Animal buzo;
Instantiation  buzo = new;
Initialization  Animal();
In single line we can write Animal buzo = new Animal();

How to access an object 
buzo.eat();

Interface:
o It should start with the uppercase letter.
o It should be an adjective such as Runnable, Remote, ActionListener.
o Use appropriate words, instead of acronyms.

Example on Interface:
interface Animal 
{
  public void animalSound(); // interface method (does not have a body)
  public void sleep(); // interface method (does not have a body)
}

class Pig implements Animal 
{
  public void animalSound() 
  {
    System.out.println("The pig says: wee wee");
  }
  public void sleep() 
  {
    System.out.println("Zzz");
  }
}

class Main12 
{
  public static void main(String[] args) 
  {
    Pig myPig = new Pig();
    myPig.animalSound();
    myPig.sleep();
  }
}

Output:
C:\Users\Student\Desktop\java prg>javac Main12.java
C:\Users\Student\Desktop\java prg>java Main12
The pig says: wee wee
Zzz

Method
A set of codes that performs a particular task 
Advantage of it is 
  • code usability
  • code optimization
o It should start with lowercase letter.
o It should be a verb such as main(), print(), println().
o If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed().

Example on method:
public class Main 
{
  static void myMethod()
  {
    System.out.println("I just got executed!");
  }
  public static void main(String[] args) 
  {
    myMethod();
    myMethod();
    myMethod();
  }
}

output:
I just got executed!
I just got executed!
I just got executed!

Variable
o It should start with a lowercase letter such as id, name.
o It should not start with the special characters like &(ampersand), $ (dollar), _(underscore).
o If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as firstName, lastName.
o Avoid using one-character variables such as x, y, z.

Example on variables:
public class Main 
{
  public static void main(String[] args) 
  {
    int myNum;
    myNum = 15;
    System.out.println(myNum);
    float myFloatNum = 5.99f;
    System.out.println(myFloatNum);
    char myLetter = 'D';
    System.out.println(myLetter);
    boolean myBool = true;
    System.out.println(myBool);
    String myText = "Hello";
    System.out.println(myText);
  }
}
output:
15
5.99
D
true
Hello

Package
o It should be a lowercase letter such as java, lang.
o If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.

Example on package:
import java.util.*; // import the java.util package 
class Main 
{
  public static void main(String[] args) 
  {
    Scanner myObj = new Scanner(System.in);
    String userName;
    // Enter username and press Enter
    System.out.println("Enter username"); 
    userName = myObj.nextLine();   
    System.out.println("Username is: " + userName);        
  }
}
output:
Enter username
raj kumar
Username is: raj kumar

How to run package program:
package myrk;
class Mypackage
{
  public static void main(String[] args)
 {
    System.out.println("This is my package!");
  }
}
output:
C:\Users\Student\Desktop\java prg>javac -d . Mypackage.java
C:\Users\Student\Desktop\java prg>java myrk.Mypackage
This is my package!

Constant
o It should be in uppercase letters such as RED, YELLOW.
o If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY.
o It may contain digits but not as the first letter.

Example on constant :
import java.util.Scanner;  
public class Constant   
{  
   //declaring constant   
   private static final double PRICE=234.90;  
   public static void main(String[] args)  
  {  
    int unit;  
    double total_bill;  
    System.out.print("Enter the number of units you have used: ");  
    Scanner sc=new Scanner(System.in);  
    unit=sc.nextInt();  
    total_bill=PRICE*unit;  
    System.out.println("The total amount you have to 
    deposit is:"+total_bill);  
   }  
}
output:
Enter the number of units you have used: 10 
The total amount you have to deposit is: 2349.0 

Camel Case in java naming conventions
Java follows camel-case syntax for naming the class, interface, method, and variable. If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...