Lecture-2nd Datatypes and variables in Java


What are Datatypes and Variables in Java ?

Just like we have some rules that we follow to speak english (the grammar) , we have same rules to follow while writing a java code , the set of these rules is called a Syntax 

SYNTAX         : "Vocabulary and Grammar of the Java "
VARIABLE      : "A Variable is a container that stores a value and this value can be changed during                                             execution"


Name of the Variable : You can give any name to you variable , but it will not follow the following Characterstics that is mentioned below:

 1 "MUST NOT START WITH A DIGIT"

            int 12number = 90;   (INVALID)

 2 "NAME IS CASE SENSITIVE"

            int number = 90;   (This is in lower case)
            int NUMBER = 90;  (This is in Upper Case)

 3 "SHOULD NOT BE A KEYWORD"
   
            int Static = 90;    (All the reserve keywords are invalid)
            int public = 89;

 4 "WHITE SPACE NOT ALLOWED"

            int sachin malhotra = 22;   (This is Invalid)
            int sachinmalhotra = 22;    (This is Valid)

 5  "CAN CONTAIN ALPHABETS , $ CHARACTER AND DIGITS IF ABOVE CONDITIONS ARE          MET"
    
            int sachin2__MALHOTRA = 22;  (This is valid)

Datatypes in Java 

Primitive Datatypes : Primitive data types in Java are predefined by the Java language and named as the reserved keywords. A primitive data type does not share a state with other primitive values. Java programming language supports the following eight primitive data types.

Non-Primitive Datatypes : Unlike primitive data types, these are not predefined. These are user-defined data types created by programmers. These data types are used to store multiple values.

Range of the Types of Datatypes

"Byte" =     Takes only 1 byte
"Short" =   Takes only 2 byte
"Integer"= Takes only 4 bytes
"Float"=     Takes only 4 bytes
"Long"=     Takes only 8 bytes
"Double"= Takes only 8 bytes
"Character"= Takes 2 bytes


General Formula of representation of the range of all the datatypes in java

"-(2^8n)/2  to (2^8n)/2"

Quiz Questions:

1 write a java program to add two numbers
2 write a java program to subtract two numbers
3 write a java program to multiply two numbers
4 write a java program to divide two numbers

"Here is code of first program and i hope you will take it as a reference and can solve all the rest of the programs"

                                                                                                                                Next Page>>
Quiz Code:

package practice1;
// this is my first practice question from java data types
public class Practice1 {

    public static void main(String[] args) {
        // sum of two numbers
        
        int number1 = 2;
        int number2 = 7;
        int sum=number1+number2;
        
        System.out.println("The sum of "+number1+" and "+number2+" are : "+sum);
    }
    
}