Java Variables: Declaration, Initialization, and Types

Day 13 : Variables 1. Variables are the containers in which we store data. 2. In java we can create variables with the help of datatype. 3.To give the name of variable should follow the Camel Case convention. Variable declaration and initialization statement: - 1.Declare a variable and store the value simultaneously. Syntax:- datatype variablename = value / expression ; E.g. :- int num=100; String name = “Shubham”; ⚠️ We cannot use a variable without declaring it. Variables are classified into two types 1. Class level Variables/Instance Variable:- The variables which are declared in the class block are known as Class level variable or Instance Variable. We can use a Instance variable without initialization because Instance variables Default values are assigned by the JVM. # In Java There is no Concept called Global Variable. They are classified into two types, i. Static Variable ii. Non-static Variable Examples: 1. class Demo {   String name = "Ajit Pawar";   // instance variable    main(String[] args) {    Demo d = new Demo();    System.out.println(d.name);   } } 2. class Demo{ static int a=10; //static variable main(){ s.o.p(a); // 10 } } 2. Local Variables:- The variables which are declared in the method block or any other block other than class block is known as local variables. For Ex. In Method block , constructor block, control flow statements block etc. Examples. 1. class Example{ main(){ int a =10; s.o.p(a); // 10 } } 2. class Example{ main(){ { int a =10; } s.o.p(a); // CTE } } #Java  #CoreJava  #JavaProgramming  #JavaVariables  #LearnJava  #CodingJourney  #ProgrammingStudent  #DailyLearning  #ComputerScience  

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories