Header Ads

Header ADS

Datatype and Variable in Java


DATATYPE:- 

In Java every variable and every expression has some type.Each and every datatypes is clearly define.

                                                                Primitive Datatype
                                                  
                                       ↙                     ↘
                               Numeric                              Non-Numeric
                                              ↙     ↘                         ↙     ↘        
                        Integral         Floating Point             Char            Boolean
                          👉    Byte                👉    Float
                          👉    Short               👉    Double
                          👉    Int
                          👉    Long

🍥Byte:- Byte is a best choice if we want to handle data in term of stream either from the file or from the network

🍥Short:- This is most rarely used datatype in Java.it is short is best suitable for 16 bits processor like 8086 but these processor are completely outdated and hence corresponding short datatype is also outdated datatype.

🍥Int:- 4 bit, the most probably datatype is int.

🍥Long:- Sometime int may not to enough to hold big value then we should go for long datatype.

🍥Boolean:- ex:-
                     int x=0;
                    if (x){
                        Printf("HI");
                            }
                        else{
                         Printf("bye");
                            }
In C it will produced output ("bye") because x is considered as false value, but in Java it will through error compile time error in compatible type found: Int, required: Boolean.

To solve this problem in Java we may used boolean datatype boolean b, allowed values are true or false.

🍥Char:- Char is used to contain only one character size (2 bytes).

VARIABLE:- 

A Java variable is a piece of memory that can contain a data value. A variable thus has a data type. Data types are covered in more detail in the text on Java data types.

Variables are typically used to store information which your Java program needs to do its job. This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi step calculations etc.

TYPE OF VARIABLE:-

👉 Instance Variable
👉 Static Variable
👉 Local Variable

1. Instance Variable:- If the value of the variable is varied from object to object such type of variable are called instance variable.
 
        →  For every object a separate copy of instance variable will be created.
         Instance variable should be declared with in the class directly but outside of a method, Constructor or any block.

Note:- We can not be access instance variable directly from static area. but we can access by using object reference.

Instance variable are also know as object level variable.

2. Static Variable:-  If the value of the variable is not varied from object to object then it is not recommended to declare variable as instance variable.

  Static variable should declare within the class directly but outside of any method or block or constructor.

→ Static variable are also know as class level variable or fields.

3. Local Variable:- Sometime to meet the temporary require of the program,we can declare variable inside a method or block or constructor. Such type of variable are called Local variable, temporary variable, stack automatic variable.



                          

No comments

Powered by Blogger.