Variable and Objects
What is Variable in Java
In Java Variable is a name given to a memory location that stores data. It acts as Contains to hold Values that Canused and changed during program execution Variable make it store,retrieve and manipulate information while running program, Each Variable has data type that defined what kind Value it Can store Such as int,float, String.
Variables are containers for storing data values. It acts like a name given to a value stored in memory.
This can be classified into two types : local and global variable.
Ex:
int age = 21;
Here int - data type
age - variable name
21 - value assigned to a variable
Local variable:
Created when a block is entered into the storage, and then it calls and destroys the block just after exiting from the function (TBH)
A local variable is declared inside a function or block and can only be accessed from within that function or block.
Eg:
public class Home{
public static void main(String[] args){
String name = "Flower";
int age = 21;
System.out.println(name);
System.out.println(age);
Output:
Flower
21
Global variable:
Key Concepts:
Variable Types: Variables are categorized into global (Field) and local.
Global variables can be static or non static.
Global Variable Definition: A global variable is declared outside of any function or accessible block and can be accessed from anywhere in the code.
Risks of Global Variables:
- They can be accidentally overwritten.
- They persist throughout the program, consuming memory.
- In large
codebases, they can cause naming conflicts.
It is declared outside the methods or blocks and can be accessed anywhere in the program. This can be further divided into two types : static and non-static.
Static variable: This variable is declared using static keyword inside a class. It is shared by all objects in class and has 1 memory copy.
Eg:
public class House{
public static String name = "Dinesh";
static int Doorno = 30;
public static void main(String[] args){
System.out.println(name);
System.out.println(Doorno);
}
}
Output:
Dinesh
30
Non-static: This variable is declared to a specific object information. It has multiple memory copy based on object.
Ex:
public class House{
String name = "Flower";
int age = 21;
public static void main(String args[]){
System.out.println(name);
System.out.println(age);
}
}
Objects
What is the Object Class?
An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which as you know, interact by invoking methods.
*An object consists of *:
State : It is represented by attributes of an object. It also reflects the properties of an object.
Eg: what the objects have, Student have a first name, last name, age, etc.
Behavior : It is represented by the methods of an object. It also reflects the response of an object with other objects.
Eg: what the objects do, Student attend a course "Java for beginners".
Identity : It gives a unique name to an object and enables one object to interact with other objects.
Eg: what makes them unique, Student have Student-ID-number, or an email which is unique.
State
State is basically of two types:
- Statically Typed Programming Language
- Dynamically Typed Programming Language
Statically Typed Programming Language:
In statically typed programming languages, type checking occurs at compile time. At compile time, source code in a specific programming language is converted to a machine-readable format. This means that before source code is compiled, the type associated with each and every single variable must be known. Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.
Dynamically Typed Programming Language
In dynamically typed languages, type checking takes place at runtime or execution time. This means that variables are checked against types only when the program is executing. Some examples of programming languages that belong to this category are Python, JavaScript, Lisp, PHP, Ruby, Perl, Lua, and Tcl.
Top comments (0)