Dev.to AI ๐Ÿค– Ai ๐Ÿ‘ 0 ๐Ÿ“– 2 min read

Java Learning series | Day - 3

What is variable ? It is Container used to store the value with a unique name. Java is statically typed , so every variable must be declared a specific data type before it can be used. Syntax String name = "Ajay"

Java Learning series | Day - 3

What is variable ?

It is Container used to store the value with a unique name.

  • Java is statically typed , so every variable must be declared a specific data type before it can be used. Syntax
String name = "Ajay";
int age = 20;

here,
Right side of assignment operator (=) is value,
left side of assignment operator (=) is variable,
Here the Value(Ajay) is stored in the variable (name),
In memory the value is only converted into bit and stored, not an variable, varaible is an identication of the value were we stored in the memory.

Rule of creating a variable?

  • Variable are Case Sensitive (age and Age are differect).
  • Variable name should not start with digits,It must begin with a valid identifier-start character, such as a letter, _, or $.
  • No reserved keyword are used as variable name.
  • Variable name cannot contain space.
  • Variable name should be meaningful name.

Type of variables

Local variables

  • Local varibales are created only inside the methods , object or block
  • The scope of the local variable only within the methods, object and block
  • The memory for local variale are created when the method is called.
  • There is no static and non-static modifier for local variables.
  • There is no default value for the local variable , so we want to initializes while declaring.
public void method(){
string name = "Ajay";
int age = 20;
}

Global variables

  • Global variables are declared inside the class , but outside the methods , block , objects.
  • It can be accessed by all non static methods in a class
  • The memory for global variables are created initially when the variable is declared.
  • There is an default value for the global variables.
  • There are two modifiers in the global variables.
  1. Static Modifier(class variables)
  2. Non-Static Modifier(Instance Variables)

1) Static modifier

  • variables are declared with static keyword.
  • Common information(class Information) are declared as static modifier.
  • It was accessed by the class name.
  • Automatically gets a default value.
public class Cars{
static String name = "NewHome";
static int price = 500000;

public static void main(String[] args){

System.out.println(Cars.name);
}

}

2) Non-Static variables

  • Variables are declared without static keyword.
  • Non-Common information(object information) are declared as non-static modifier.
  • It was accessed by object name.
  • Automatically gets a default value.
public class Students{

String name = "Ajay";
int age = 20;

public static void main(String[] args){

Students NewStd = new Students()

System.out.println(NewStd.name);
System.out.println(NewStd.age);

}

}

What is object ?

  • Object are the memory references of the class that has indenpendent state ,behaviour and identity
  • State: Represented by attributes (fields/variables).
  • Behavior: Represented by methods (functions).
  • Identity: A unique identifier maintained by the Java Virtual Machine (JVM) to distinguish it from other objects in memory, even if they have the exact same state
  • Non-Static variables is created for every each object.

Object Delcaration

  • using class name the object must be created.
public class Cars{

String model;
int pricel;

public static void main(String[] args){

Cars MyCar = new Cars();   #
MyCar.model = "XuV";
MyCar.price = 600000;


MyCar.drive()    # object method
}
}
๐Ÿ“ฐ Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.