Identifiers in Java

Page 1

IdentifiersInJava–RulesandBestPractices

Introduction

Intheworldofprogramming,identifiersplayacrucialroleinidentifyingand referencingvariouselementswithinaprogram.InJava,identifiersserveas namesforclasses,methods,variables,andotherprogramentities UnderstandingtheconceptofidentifiersisessentialforanyJavadeveloper.

Imagineyouareworkinginalibraryandyouhavetoorganizebookson differentshelves.Eachshelfrepresentsacategoryofbookssuchasfiction, non-fiction,science,history,etc.Now,tomakeiteasierforthelibraryvisitors tofindthebooktheyarelookingfor,youneedtolabeleachshelfwitha uniquenamethatrepresentsthecategoryofbooksonthatshelf.

InJava,anidentifierrepresentsthenameassignedtoavariable,method,or class,describingitspurposeorfunctionalitywithinthecode.Justlikethe

JAVAROADMAP

labelsontheshelvesinthelibrary,identifiersinJavamustbeuniqueand descriptive.

Sobasically,werefertothenamesofyourJavavariablesasIdentifiers.Inthis blogpost,wewillexploreidentifiersinJava,theirrulesandconventions,and provideexamplestoillustratetheirusage.Let’sdivein!

WhatAreIdentifiersInJava

InJava,youuseidentifiersassequencesofcharacterstonamevariables, classes,methods,orotherprogramelements.Itactsasauniquelabelthat distinguishesoneentityfromanother.Rememberthatidentifiersare case-sensitive,treatinguppercaseandlowercaselettersasdistinct.

RulesForNamingIdentifiersInJava

Javahasspecificrulesandconventionsfornamingidentifiers.

Herearesomekeyguidelinestokeepinmind:

a)Thefirstcharactermustbealetter(A-Zora-z),currencycharacter($)or anunderscore( ).Itcannotbeadigitoranyspecialcharacter.

b)Afterthefirstcharacter,youcanuseletters,digits(0-9),currency characters,orunderscores

c)Javakeywords(reservedwords)suchasint,class,andpubliccannotbe usedasidentifiers.

d)Identifiersshouldnotexceedthemaximumlengthof255characters.

e)Avoidusingunderscoresastheinitialorfinalcharacterinidentifiers,as theyhavespecificconventionsforspecialpurposes.

f)CamelCaseconventioniswidelyusedforclassandmethodnames,while lowercaseletterswithunderscores(snake case)aretypicallyusedfor variableandconstantnames.

ExamplesofIdentifiers

Let’sexploresomeexamplesofvalididentifiersinJava:

a)VariableIdentifiers:

● intage;

● doublesalary;

● Stringmessage;

● booleanisComplete;

b)ClassId

● classCar;

● classPersonDetails;

● classDatabaseConnection;

c)MethodIdentifiers:

● publicvoidcalculateTotal();

● privateStringgetName();

● protectedintmultiplyNumbers(inta,intb);

VISIT
BLOG.GEEKSTER.IN FOR THE REMAINING QUESTIONS

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.