Java - Name Conventions, Literals , 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.
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 {}
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
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.
getNumbers()
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;
Constant Variables
Constant variables should start with uppercase and special characters "_" is allowed.
public static final int MY_MARK_ENGLISH = 89;
Package
Package names should be written by all lowercases.
package com.one.test;
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.
- \b (backspace),
- \t (tab),
- \n (line feed),
- \f (form feed),
- \r (carriage return),
- \" (double quote),
- \' (single quote),
- \\ (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.
- \b (backspace),
- \t (tab),
- \n (line feed),
- \f (form feed),
- \r (carriage return),
- \" (double quote),
- \' (single quote),
- \\ (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;
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 acheived 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 [];
or
type[] variableName;
One-Dimensional Array declaration have two components
data-type followed by name-of-array
type arrayName[];
or
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[];
or
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];
or
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;
System.out.println(numberArray[0]);
System.out.println(numberArray[1]);
System.out.println(numberArray[2]);
System.out.println(numberArray[3]);
System.out.println(numberArray[4]);
}
}
Output :
5
4
3
2
1
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";
System.out.println(stringArray[0]);
System.out.println(stringArray[1]);
System.out.println(stringArray[2]);
System.out.println(stringArray[3]);
System.out.println(stringArray[4]);
}
}
Output :
name1
name2
name3
name4
name5
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++)
{
System.out.println(arr[i]);
}
}
}
Output :
0
1
2
3
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at ArrayLen.main(ArrayLen.java:8)
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++)
{
System.out.println(stringArray[i]);
}
}
}
Output :
name1
name2
name3
name4
name5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at StringArr.main(StringArr.java:8)
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 exeception 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){
System.out.println(m);
}
}
}
Output :
1
2
3
4
5
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] + " ");
}
System.out.println();
}
}
}
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;
System.out.println(var[0][0][0]);
System.out.println(var[1][1][1]);
System.out.println(var[2][2][2]);
}
}
Output :
10
20
30
Note : Hi i am new for blogging, if you think my blog is useful, kindly SUBSCRIBE in the website and also share this to your friends. And if anything is wrong in the tutorial please correct me and kindly forgive me. Thanks everyone.
No comments:
Post a Comment