Java Variables - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Java Variables

Variables in Java

Variable is a named memory location is used to store/hold data temporarily.

During program execution we can modify that data.

Variable can be created by using data types.

We have two types of data types.

1) primitive datatypes

2) non-primitive datatypes

https://smarttechguides.blogspot.com/2022/01/java-variables.html

primitive data-types

1) byte-----0

2) short----0

3) Int ------0

4) long-----0

5) float ----0.0

6) double--0.0

7) char-----empty space

8) boolean--false

Note: primitive datatypes by default it holding above mentioned values

 

non-primitive data-types

Array----------null

Class ---------null

Interface-----null

Enum---------null

String---------null

etc..

non-primitive types are called referenced type because they refer to object. 

primitive types are predefined in Java, but non-primitives are created by programmer not decided by Java (except String).

non-primitive variables can hold null values. but primitive variables can't hold null values.  


Note: reference variable stores reference of object, not direct values where as primitive variables store data directly.


Limitation of variables:

It can store single value at a time.

If we assign new value old value replaced with new value.

 

Types Of Variables (Scope)

Based on class scopes variables are divided into two types

1) Local Variables

2) Instance Variables

 

1. LOCAL VARIABLES

A local variable is defined inside a method block.

A block begins with an opening curly brace and ends with a closing curly brace.

The scope of the variable is limited within the block.

In other words, local variables are visible only in the block (method) in which they are declared.

 

Note: local variable/inside method block we can’t declare variables as static it leads to compilation error.

 

2. INSTANCE VARIABLES

We must create Instance variables/class level variables only if we want to access a value throughout the class from all its methods.

Again, class level variable is divided into two types

1) static variables

2) non-static variables

 

Static variable

The key word ‘static’ is prefixed before the variable to represent them as static variables.

Static variable gets memory location when class is loaded into JVM, they get memory with respect to class name.

 

Class Employee{

static String companyName=”tata tree”;

               public static void main(String[] args) {

               System.out.println(Employee.companyName);    

}


Static variable only one copy of this variable is maintained for all the objects.

 

Example: in employee management app company name common for all employees’ teams, so we can declare company name as static at class level. Only one copy of object created.

 

Non-static variable

Non-static variable is class level variable.

variable not having static keyword is called non-static variable. 

Non-static variables get memory location when object is created using new key word.


Class Employee{

               String companyName=”tata tree”;//non-static

               public static void main(String[] args) {

         Employee object=new Employee();

               System.out.println(object.companyName);           

}

 

Non-static variable not allowed into static block.

But static variable can be allowed into static and non-static block.

 

Final variables

Once variable initialized as final the value can’t be changed. If we try to change its value

It leads to compile time error.

Final int idcardNumber=10001222;

 

Note: Any constant value which can be assigned to the variable is called literal.

No comments:

Post a Comment