Java Variable
What is a variable?
Ans. It is nothing but a placeholder of data. It is a memory location. Example int a, b; Here a and b is are variable of data type int.
How many types of variables are there in Java?
It has three types. A local variable, an Instance variable, and a Class variable.
- Local Variable: It is defined within the method and constructor. These variables' visibility remains within the method and constructor.
- Instance Variables: Define inside a class. It is available for every object of the class.
- Class variable: when a variable defines with the keyword static inside a class is known as the class variable.
- All objects of the class share static variables because of only a single copy of these available in the memory.
java variable naming conventions camelcase
Java variable is written as firstName. This is a camel case, as we write any variable or method, we use all
small characters of the first word of the variable and the second first letter will start with a capital letter.
State the difference between token and identifier, variable.
Ans. The smallest individual unit of the program is called Token. e.g: Identifiers, keywords, Literals, etc. whereas an identifier is a name given to different parts of a program, it is also known as variables. e.g. variable, functions, classes, etc.
Variable: A memory location that stores value is known as a variable. It is like a box in memory where we keep or store values. E.g. int a;
Explain the Instance Variable. Give an example.
Ans. Variables associated with the instance of a class are called instance variables. E.g.
class ABC
{
int a,b,c; // Instance variable
void sum()
{
a = 20;
b = 30;
c = a + b;
System.out.println(c);
}
}
class ABC
{
int a,b,c; // Instance variable
void sum()
{
a = 20;
b = 30;
c = a + b;
System.out.println(c);
}
}
Another example
import java.io.*;
public class TaxCalculator
{
// Instance Variables
int pan;
String name;
double taxableincome;
double tax;
void input()throws IOException
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
System.out.println("Enter name and taxable income:");
name=br.readLine();
taxableincome=Double.parseDouble(br.readLine());
}
void computeData()
{
if(taxableincome<=60000)
tax = 0;
else if(taxableincome>60000 && taxableincome<=150000)
tax = taxableincome*0.05;
else if(taxableincome>150000 && taxableincome<=500000)
tax = taxableincome*0.1;
else
tax=taxableincome*0.2;
}
void displayData()
{
System.out.println("Name=" + name);
System.out.println("Taxable Income=" + taxableincome);
System.out.println("Tax Paid=" + tax);
}
public static void main(String args[])throws IOException
{
TaxCalculator ob=new TaxCalculator();
ob.input();
ob.computeData();
ob.displayData();
}
}
import java.io.*;
public class TaxCalculator
{
// Instance Variables
int pan;
String name;
double taxableincome;
double tax;
void input()throws IOException
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
System.out.println("Enter name and taxable income:");
name=br.readLine();
taxableincome=Double.parseDouble(br.readLine());
}
void computeData()
{
if(taxableincome<=60000)
tax = 0;
else if(taxableincome>60000 && taxableincome<=150000)
tax = taxableincome*0.05;
else if(taxableincome>150000 && taxableincome<=500000)
tax = taxableincome*0.1;
else
tax=taxableincome*0.2;
}
void displayData()
{
System.out.println("Name=" + name);
System.out.println("Taxable Income=" + taxableincome);
System.out.println("Tax Paid=" + tax);
}
public static void main(String args[])throws IOException
{
TaxCalculator ob=new TaxCalculator();
ob.input();
ob.computeData();
ob.displayData();
}
}
Q. What is the final variable?
When a variable defines with the final keyword then its value can be changed throughout the whole program.
Example
class Circle{
final double pi=3.14;//final variable cannot be change
double a, r;
void area(){
r = 3.5;
a = r*r*pi;
System.out.println("Area=" + a)
}
public static void main(String args[]){
Circle obj=new Circle();
obj.area();
}
}//end of class
Q. Can the‘ main’ method be declared final?
Yes, the main method can declare as final.
public class Example
{
public static final void main(String args[])
{
int a=6;
System.out.print((++a)+(a--));
}
}
Q. What is a static variable?
A static variable is shared by all instances of a class. It is also known as a class variable and it is declared with the keyword ‘static’. Instances (objects) of the same class share a single copy of static variables.
class School
{
int rollno;
String section;
String name;
static String schoolName ="InspireSkills Edu"; // Static variable
// constructor
School(int r, String s, String n){
rollno = r;
section = s;
name = n;
}
void display ()
{
System.out.println(rollno + " " + section + " " + name + " " + schoolName);
}
public static void main(String args[])
{
School ob1 = new School(1, "A", "Mukesh");
School ob2 = new School(2, "B", "Aryan");
ob1.display();
ob2.display();
}
}
/*
* A shop will give a discount of 10% if the cost of the purchased quantity is more than 1000.
* Ask the user for quantity, Suppose, one unit will cost 100. Judge and print total cost for the user.
*/
import java.util.*;
public class Discount
{
public static void main(String args[])
{
double qty, cost=0.0, tqty,dis;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Quantity:");
qty=sc.nextDouble();
tqty=qty*100;
if(tqty>=1000)
{
dis=tqty*10/100.0; // Percentage
cost=tqty-dis;
System.out.println("Cost="+cost);
}
else
{
cost=qty*100;
System.out.println("Cost="+cost);
}
}
}
More Post
History of java