Java Comments and Naming Conventions

Last updated on Dec 25 2022
Prabhas Ramanathan

The Java comments are the statements that are not executed by the compiler and interpreter. The comments can be used to provide information or explanation about the variable, method, class or any statement. It can also be used to hide program code.

Table of Contents

Types of Java Comments

There are three types of comments in Kava.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment

java 77

1) Java Single Line Comment

The single line comment is used to comment only one line.
Syntax:
1. //This is single line comment

Example:

1. public class CommentExample1 { 
2. public static void main(String[] args) { 
3. int i=10;//Here, i is a variable 
4. System.out.println(i); 
5. } 
6. }

Output:
10

2) Java Multi Line Comment

The multi line comment is used to comment multiple lines of code.
Syntax:
1. /*
2. This
3. is
4. multi line
5. comment
6. */

Example:

1. public class CommentExample2 { 
2. public static void main(String[] args) { 
3. /* Let's declare and 
4. print variable in java. */ 
5. int i=10; 
6. System.out.println(i); 
7. } 
8. }

Output:
10

3) Java Documentation Comment

The documentation comment is used to create documentation API. To create documentation API, you need to use javadoc tool.
Syntax:
1. /**
2. This
3. is
4. documentation
5. comment

6. */
Example:

1. /** The Calculator class provides methods to get addition and subtraction of given 2 numbers.*/ 
2. public class Calculator { 
3. /** The add() method returns addition of given numbers.*/ 
4. public static int add(int a, int b){return a+b;} 
5. /** The sub() method returns subtraction of given numbers.*/ 
6. public static int sub(int a, int b){return a-b;} 
7. }

Compile it by javac tool:
javac Calculator.java
Create Documentation API by javadoc tool:
javadoc Calculator.java
Now, there will be HTML files created for your Calculator class in the current directory. Open the HTML files and see the explanation of Calculator class provided through documentation comment.

Java Naming conventions

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 Microsystems 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.

Advantage 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).
Let’s see some other rules that should be followed by identifiers.

Class

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

1. public class Employee 
2. { 
3. //code snippet 
4. }

Interface

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

1. interface Printable 
2. { 
3. //code snippet 
4. }

Method

• It should start with lowercase letter.
• It should be a verb such as main(), print(), println().
• If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed().
• Example:-

1. class Employee 
2. { 
3. //method 
4. void draw() 
5. { 
6. //code snippet 
7. } 
8. }

Variable

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

1. class Employee 
2. { 
3. //variable 
4. int id; 
5. //code snippet 
6. }

Package

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

1. package com.tecklearn; //package 
2. class Employee 
3. { 
4. //code snippet 
5. }

Constant

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

1. class Employee 
2. { 
3. //constant 
4. static final int MIN_AGE = 18; 
5. //code snippet 
6. }

CamelCase 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.
So, this brings us to the end of blog. This Tecklearn ‘Java Comments and Naming Convention s’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

 

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Java Comments and Naming Conventions"

Leave a Message

Your email address will not be published. Required fields are marked *