Java – Name Conventions, Literals and Arrays

Class Name conventions 

What is a naming convention ?

Naming convention is the convention for naming certain things.

What is naming convention in java ?

There is a set of protocol must be followed for naming class, interface, method, variable, package, constants.

Java uses CamelCase format inside the code for denoting class, interface, method, variable, package, constants.

Java Class :

Class name should start with uppercase which can be noun, first letter uppercase and first letter of internal word can also be capitalised.

class StudentMarks {}

Java Interface :

Interface name should start with uppercase which can be noun, first letter uppercase and first letter of internal word can also be capitalised.

Interface StudentInfo

Java Method

Method name should start with lowercase letter and it should be a verb / action. it can have first letter lowercase following word with uppercase.


Java Variables

Variable names should be meaningful, easily understandable should start with lowercase.

Variables should not start with special characters like “_” or “$” sign.

int a = 10;
int limit = 20;
int num = 45;

Java Constant Variables

Constant variables should start with uppercase and special characters “_” is allowed.

public static final int MY_MARK_ENGLISH = 89;

Java Package 

Package names should be written by all lowercases.

import com.two.test;

Java Literal

What is a literal in programming language ?

Suppose we have a variable int a = 10;

The value of the variable a will represent different values while execution of the program, but the variable name “a” remains constant throughout the program.

There are 5 types of Java Literals listed below.

1. Java Integer Literals 

Values of the integer data types like byte, short, int, and long can be created from integer literals.

Integer literal can be of 

1. Decimal type 

This have Base 10, whose digits consists of the numbers from 0 to 9.

2. Hexadecimal type 

This have Base 16, whose digits consist of the numbers 0 to 9 and also the letters A through F.

3. Binary types

This have Base 2, whose digits consists of the numbers 0 and 1. 

Note : we can create binary literals in Java SE 7 and later version.

int octalType = 0800; //octal literal
int hexaType = 0x100; //hexadecimal literal
int decimalType = 256; // decimal literal
int binaryType = 0a110; // binary literal

2. Java Float/Double literals

Values of the floating point data types float and double can be created from Float/Double literals.

Usually Float values must be appended with “F” or “f” and Double values must be appended with “D” or “d”.

This can be denoted with a decimal point and represented with exponent “E” or “e”.

double varName1 = 12.345e2;
float varName2  = 1234.5f;
double varName3 = 1234.5;

3. Java Character Literals

Values of the char data type can be created from character literals.

It can be denoted as single character within single quote.

char a = 'A';

Java supports some of the escape sequences for character literal listed below.

  1. \b (backspace), 
  2. \t (tab),  
  3. \n (line feed), 
  4. \f (form feed), 
  5. \r (carriage return), 
  6. \” (double quote),  
  7. \’ (single quote), 
  8. \\ (backslash).

4. Java String Literals

Values of the String data type can be created from String literals.

Group of characters is represented as String literals in Java

It can be denoted as group of characters within double quotes.

String country = "India";

Same like char literals java supports the same escape sequences for String literals too.

  1. \b (backspace), 
  2. \t (tab),  
  3. \n (line feed), 
  4. \f (form feed), 
  5. \r (carriage return), 
  6. \” (double quote),  
  7. \’ (single quote), 
  8. \\ (backslash).

5. Java Boolean Literals

Values of the boolean data type can be created from boolean literals.

Boolean literals have only two possible values “true” or  “false” .

boolean var1 = true;
boolean var2 = false;

6. Java NULL Literals

NULL literal is always represented as “null”.

Its mainly used to avoid more number of references to the object.

Suppose an java object which is referenced by “abc” , to avoid more number of references to the same object we can assign it as “null”

abc = null;

Java Arrays

What is an array?

Something which belongs to same type is ordered in series is called as array.

What is an array in java ?

An array is a group of same-typed variables that refers to a common name.

Why array is used in java ?

An array is used to store a collection of variables/data of the same type.

If we want to iterate series number of similar variables/data, which can be achieved by storing variables in array.

Since java uses collection of same type variables/data to be accessed array concept is implemented.

Arrays in Java :

Arrays are considered as objects in java, so we can easily find the length of the array by arraymember.length();

Array will be stored in the allocated memory location dynamically.

Each element in an array is ordered and each have the index value starting from 0.

Array in java can be used as static member, a local variable and also parameters for the java methods.

Array can contain primitive data types as well as the objects of the class.

If an Array is of primitive data type the actual values are stored in contiguous memory locations.

And of an Array is an objects of a class, the actual objects are stored in heap segment

There are two types of array

1. One-Dimentional Array

Basic syntax :

type variableName [];


type[] variableName;

One-Dimensional Array declaration have two components

data-type followed by name-of-array

type arrayName[];


type[] arrayName;

We can create an array with all the primitive data-types.

int intArray[]; 
byte byteArray[];
short shortsArray[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];


int[] intArray;
byte[] byteArray;
short[] shortsArray;
boolean[] booleanArray;
long[] longArray;
float[] floatArray;
double[] doubleArray;
char[] charArray;

Instantiating an Array

Once after declaring the One-Dimensional Array , we can instantiate the array by

int intArray[];
intArray = new int[5];


int[] intArray;
intArray = new int[5];

And Array of the 5 elements will be declared as

intArray[0] = 5;
intArray[1] = 4;
intArray[2] = 3;
intArray[3] = 2;
intArray[4] = 1;

Printing the values of an Array.

public class arrayList{

        public static  void main(String args[])
        int [] numberArray;
        numberArray = new int[5];
        numberArray[0] = 5;
        numberArray[1] = 4;
        numberArray[2] = 3;
        numberArray[3] = 4;
        numberArray[4] = 5;

Output :






Example : String array

public class StringArray{

        public static  void main(String args[])
        String[] stringArray;
        stringArray = new String[5];
        stringArray[0] = "name1";
        stringArray[1] = "name2";
        stringArray[2] = "name3";
        stringArray[3] = "name4";
        stringArray[4] = "name5";

Output :






An Array values can be defined directly while declaring

int[] arr = new int[5]{0,1,2,3,4};

In this case printing array elements can be done by iterating the array values with the help of “ArrayLength”

And Array-length can be found by Array.length();

Example : 3

Array element can be directly initialised

int[] arr = {0,1,2,3,4};

Sample code :

public class ArrayLen
   public static void main (String[] args)
        int[] arr = {0,1,2,3,4};
        for (int i = 0; i <= arr.length; i++)

Output :






Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 at ArrayLen.main(

Example : 4

public class StringArr{
        public static  void main(String args[])
        String[] stringArray = {"name1","name2","name3","name4","name5"};
        for (int i = 0; i <= stringArray.length; i++)

Output :






Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5at StringArr.main(

While compiling the program, the compiler compiles the code successfully.

But while executing the program the JRE/JVM stores the variables in memory location, throws exception when the program looks for 5th element.

arr.length will give the length of the array. So, for an array of 5 elements, it will give 5. So, i in for loop must go till 4 only ( i < a.length ).

So it throws exception “java.lang.ArrayIndexOutOfBoundsException: 5”

If we implement the code with for-each loop, JDK/JVM will not throw any exception.

Example : 5

public class ArrayLen{
  public static void main(String[] args){
    int[] ar = {1,2,3,4,5};
    for (int m : ar){

Output :






2. Two-Dimensional Array

Two Dimensional array is defined as Array of Arrays.

Example : 6 

public class TwoDArray {
    public static void main(String[] args) {

        int[][] values = new int[4][4];
        values[0][0] = 1;
        values[1][1] = 2;
        values[3][2] = 3;

        for (int i = 0; i < values.length; i++) {
            int[] var = values[i];
            for (int x = 0; x < sub.length; x++) {
                System.out.print(var[x] + " ");

Output :

1 0 0 0

0 2 0 0

0 0 0 0

0 0 3 0

3. Three-Dimensional Array

Three dimensional Array is similar use case of 2D Array.

Example : 7

public class ThreeDArray {
    public static void main(String[] args) {

        byte[][][] var  = new byte[3][3][3];
        space[0][0][0] = 10;
        space[1][1][1] = 20;
        space[2][2][2] = 30;


Output :