Program is a collection of 1) data(variables) 2) methods (operation).
- What is the variable ?
Ans- Variable is used to store data, such as a number, or a string of characters. Basically it is used to store some value(Data). Ex- int i = 5; So here i is a variable which store integer. - What is operation/method/function ?
Ans- It's a kind of process which will do some operation on variables and will give some output/result. Ex- addition of two numbers. - Variable follow three step process-
i) Declaration (reserve some memory), ex- int i;
ii) Initialization (initialize the variable with some value), ex- i=0;
iii) Utilization (use the variable)., ex- i+6; - Based on memory allocation java provide 8 types of data type-
- byte (1 byte storage),
- short (2 byte storage),
- int (4 byte storage),
- long (8 byte storage),
- float (4 byte storage),
- double (8 byte storage),
- char (2 byte storage),
- boolean (2 byte storage),
- byte, short, int and long can store only integer variable. ex- 2
- float and double used to store decimal number. ex- 3.0
- char used to store a to z alphabet.
- boolean can have true or false. It can't have 0 or 1.
- A program has keyword, identifiers and literals.
ex- int i = 3;
int is a keyword.
i is a identifiers.
3 is a literals. - Keyword is a word which is predefined.
- Identifiers are words which are user defined. It can be alphanumeric but can't be only numeric. 1st letter should always be alphabet.
- Convention for identifiers- Always start 1st word with small letter but rest of the word should start with Caps letter.
ex- javaSeleniumTraining - Convention for class name- all the word should start with caps letter.
ex- JavaTraining - Note- If any local variable has not been initialized before using it then it will give compilation error.
- Ex-
public class JavaTraining{
public static void main(String[] args){
System.out.println("Start Here");
int x;
x=4;
int y = 5;
System.out.println("value of x: "+x);
System.out.println("value of y: "+y);
}
} - + sign work as concatenation when using between String and integer or say to concatenate two values in java use + sign.
- ; indicate the end of statement.
For your practice-
1) Try to write the code to print the sum of two numbers.
2) Try to write the code to print the area of square whose one arm is 5cm.