Java Coding Convention
- Class: Class name starts with upper case and every inner word also starts with upper case. This convention is also known as camel case convention. Examples: String, StringBuffer, InputStreamReader
- Interface: Interface name also starts with upper case and every inner word also starts with upper case. This convention is also known as camel case convention.
Examples: Serialization, RandomAccess
- Methods: method name starts with lower case and every inner word starts with upper case. This convention is known as mixed case.
Example: toString();
charA()
- Variables: Same rule as for methods
Example: pageContext
- Packages: Classes and interfaces: every character for package name should be lower case
- Constants: Every character must be upper case
Example: MAXPRIORITY
- These coding conventions are Mandatory for predefined library and optional for user defined library.
- Class contains variables, methods, constructors, instance blocks, static blocks
- Every name in java (class name, variable name, method name, constructor name) is called identifier
Identifier: A name in java program is called identifier, which can be used for identification purpose. It can be method name, variable name, class name or label name. A name in java program is identifier.
Rules for defining java identifier:
- The only allowed character in java identifiers are a to z, A to Z, 0 to 9, $, _. If we are using any other character than these, we will get compile get compile time error. Example total number (valid), total # (invalid)
- Identifiers can not starts with digits total123 (valid) but 123total (invalid)
- Java identifiers are case sensitive. Of course java language itself is treated as case sensitive programming language.
Class Test
{
int number = 10;
int Number = 20;
int NUMBER = 30;
}
- We can not use reserved words as identifiers for example, int if = 20 (invalid)
- All predefined java class name and interface names, we can use identifiers. Even though it is valid, but it is not a good programing practice because it reduces readability and creates confusion
- In java, some words are reserved to represent some meaning or functionalities such type of words are called reserved words. In java, there are total 53 reserved words.
Keywords for data type:
Byte, short, int, long, float, double, Boolean, char (8)
Keywords for control flow:
If, else, switch, case, default, while, do, for, break, continue, return (11)
Keywords for modifiers:
Public, private, protected, static, final, abstract, synchronized, negative, strictfp, transient, volatile (11)
Keywords for exception handling:
Try, catch, finally, throw, throws, assert (6)
Class related keywords:
Class, interface, extends, implements, package, import (6)
Object related keywords:
New, instanceof, super, this (4)
Java comments: single line comment, multiline comment, and documentation comment (used to prepare API, Application programming interface documents, it contains detailed description about how to use products).
Comments are used to provide detailed description about application and Comments are ignored during compile time.
Data type (Lecture 8):
- The data type represents the type of the variable
- The data type decides memory size of the variable
- The data type decides range value of variables
Java contains eight primitive data types
Data type | size | Default value | To represent | ||
byte | 1 byte (8 -bits) | 0 | Numeric values | ||
small | 2 bytes | 0 | |||
int | 4 bytes | 0 | |||
long | 8 bytes | 0 | |||
float | 4 bytes | 0.0 | Point values | ||
double | 8 bytes | 0.0 | |||
char | 2 bytes | Single space | Single character | ||
boolean | No size | false | True or false |
For range use
where, n is the bit
Flow control statements (Lecture 9):
- Conditional statement: if, if-else, switch
- Iteration statement: while, for, dowhile
- Transfer statement: break, continue, goto, return, try
- if statement: The statement gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.
if(condition){
statement;
}
public class IfStatmentDemo {
public static void main(String[] args) {
int x = 10;
if (x < 50) {
System.out.println("integer x is less than 50");
}
}
}
- if else statement: The statement inside "if" would execute if the condition is true, and the statement inside "else" would execute if the condition is false
if(condition){
statement;
}
else if(condition){
statement;
} else{
}
public class IfElseStatementDemo {
public static void main(String[] args) {
int x = 99
if (x<100 && x>=10) {
System.out.println("x is two digit number");
}
else if(x<1000 && x>=99) {
System.out.println("x is three digit number");
}
else if (x<10000 && x>999) {
System.out.println("x is four digit number");
}
else {
System.out.println("number is not between 10 and 10000");
}
}
}
Output: x is two digit nubmer
Switch case statement: switch case statement is used when we have number of choices and we may need to perform different task for each choice.
Switch case without break:
public class SwitchStatmentDemo {
public static void main(String[] args) {
int i = 2;
switch(i) {
case 1:
System.out.println("case 1");
case 2:
System.out.println("case 2");
case 3:
System.out.println("case 3");
case 4:
System.out.println("case 4");
default:
System.out.println("default");
}
}
}
Output:
case 2
case 3
case 4
default
Switch case with a break:
public class SwitchStatmentDemo {
public static void main(String[] args) {
int i = 2;
switch(i) {
case 1:
System.out.println("case 1");
break;
case 2:
System.out.println("case 2");
break;
case 3:
System.out.println("case 3");
break;
case 4:
System.out.println("case 4");
break;
default:
System.out.println("default");
}
}
}
Output:
case 2
For loop:loops are used to execute a set of statements until a particular condition is satisfied.
for{initialization, condition, increment/decrements){
statements;
}
public class ForLoopDemo {
public static void main(String[] args) {
for (int i = 0; i< 5; i++) {
System.out.println(i);
}
}
}
Output:
0
1
2
3
4
Enhanced for loop: example
public class EnhancedForLoopDemo {
public static void main(String[] args) {
int[] x = {10, 20, 30, 40};
for (int x1 : x) {
System.out.println(x1);
}
}
}
Enhanced for loop: example 2
public class EnhancedForLoopDemo {
public static void main(String[] args) {
String[] x = {"hi", "hello", "how", "are", "you"};
//for(data type variable: reference variable)
for (String x1 : x) {
System.out.println(x1);
}
}
}
While loop: while loop checks the condition initially and executes the content.
while(condition){
statement;
}
public class WhileLoopStatement {
public static void main(String[] args) {
int i = 0;
while(i<5) {
System.out.println(i);
i++;
}
}
}
Do while loop:The do while loop executes the content of the loop before checking the condition of the while.do-while executes the content once even if the condition is false.
do{
statement;
}
while(condition)
public class DoWhileLoop {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i = i +1;
} while (i<5);
}
}
== operator | .equal() |
|
|
public class Test {
public static void main(String[] args) {
String s1 = new String("Durga");
String s2 = new String("Durga");
System.out.println(s1 == s2); //reference comparison, i.e. addresswise they are not equal
System.out.println(s1.equals(s2)); // content comparison
}
}
Output:
false
true
- .equals() method present in object class meant for reference comparison only based on our requirement we can override for content comparison
- In String class, all wrapper class and all collection classes, .equals() method is overridden for content comparison
No comments:
Post a Comment