Tutorial 3 - Java Variable and Data Types

Let’s learn about data types and variables used in java.



If you are new to java, you can get a basic idea about java programming form here.

Let’s start with variables.


  • What is a variable?

    • A basic definition for a Variable is a name of a memory location. It reserves an area allocated in the memory.

  • What are the variable properties?

    • A variables have 6 properties. The first 3 are important to create a program and rest are advanced.


  1.  Name
  2.  Type
  3.  Value
  4.  Scope
  5. Life Time
  6. Location (in memory)
You need to be aware of the type of variable before declaring it. It is called data types.

So what is this Data Type ?

It represents the different values to be stored in the variable.

Ex: int, String, long… etc.

Variable

Let’s start with an example.

==========================================
class Variable{

    int a = 20;    // Instance Variable

    static int b = 30;   // static variable

    void method(){

        int c = 40;      // local variable

    }

    public static void main(String[] args){

        int localVariable = 10;        // Local variable

   
    }

}

===========================================

There are three types of variables in java. They are,

·         local
·         instance
·         static

Local Variable

Local variable is declared in a method.

Instance Variable

Instance variable is declared inside the class. But it can be declared outside the method.

Static variable

Static variable is declared as static. These types of variables cannot be local.


Data Types

In java there are two categories of data types.

1. Primitive data types
2. Non-primitive data types (Reference/Object Data Types )

Primitive Data Types

There are eight primitive data types supported by Java. Primitive data types are predefined by the language named by a keyword. The detailed primitive data types as follows.

Types
Values
Default
Size
Range
byte
Signed integers
0
8 bits
-128 to 127
short
Signed integers
0
16 bits
-32768 to 32767
int
Signed integers
0
32 bits
-2147483648 to  2147483647
long
Signed integers
0
64 bits
-9223372036854775808 to  9223372036854775807
float
IEEE 754 floating point
0.0
32 bits
+/-1.4E-45 to +/-3.4028235E+38,
+/-infinity, +/-0, NAN
double
IEEE 754 floating point
0.0
64 bits
+/-1.7976931348623157E+308,+/-infinity, +/-0, NaN
char
Unicode character
\u0000
16 bits
\u0000 to \uFFFF
boolean
True, false
false
1 bit used in 32 bit integer
NA

Non-primitive data types (Reference/Object Data Types )

While Objects can and do hold primitive types, the difference is how memory is allocated.

Primitive type variables are created on the stack
Objects are created only on the heap

Creating Objects on the heap is like using malloc() in C, using a keyword called “new”

Let’s look at an example.

Example 1 – print a value:


the output will be


Example 2:


the output will be


Example 3:


the output will be


Example 6:


the output will be


Example 7 – add two numbers:


the output will be




Thank you very much for reading this tutorial. If you have any problems regarding this tutorial feel free to make a comment below or you can contact us by sending email to this address, progtpoint@gmail.com.
Follow us on facebook and twitter also subcribe us youtube.



2 comments:

  1. Tutorial 3's coverage of Java variables and data types is pivotal for beginners. Understanding these foundational concepts is crucial for effective coding. How Protect Theft Mastering variable declaration, data types, and their roles lays the groundwork for building robust programs.

    ReplyDelete

Social