0% found this document useful (0 votes)
7 views

Variable

Uploaded by

Fatima Tamboli
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Variable

Uploaded by

Fatima Tamboli
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Variable:

Java Variables are basically memory allocations, It is


the place where we store values which can later be
manipulated by the program. Each variable has a
specific type, which determines its memory and the
data that can store in that variable.
Declaration of variables:
• Datatype – The Datatype is the type of data in which the variable is
stored.
• Variable_name – It is the name of the variable.
• Value – The initial value stored in the variable.
• Example-
• float simpleInterest; //Declaring float variable
• int time = 10, speed = 20; //Declaring and Initializing integer variable
Types of Variables in Java:

• Local Variable in Java :


• A local variable is defined within a method, block, or constructor, and
its scope remains limited to a particular block.
• We can also use local variables with the same name in different
functions. In this case, the name of the variable is the same but they
act differently it is similar like two persons having the same name.
int CourseDetails
course
public
public
package
public
=obj.NumberOfCourses();
static
10;//declaring
void
class
obj
JavaVariablesDemo;
void
Example-
NumberOfCourses()
=CourseDetails
main(String
{}newlocal
CourseDetails();
variable
args[])
{ course

Example-
package JavaVariablesDemo;
public class CourseDetails {
public void NumberOfCourses()
{
int course = 10;//declaring local variable course
System.out.println("Number of courses at DataFlair are : " + course);
}
public static void main(String args[])
{
CourseDetails obj = new CourseDetails();
obj.NumberOfCourses();
}
}
Instance Variables in Java:

• Instance variables are declared in a class, outside any


method, constructor, or block, it is of the static type.
• These variables are created when an object is created with
the help of keyword new and destroyed when the object is
destroyed
class Marks
{
//These variables are instance variables.
//These variables are in a class and are not inside any function
int engMarks;
int mathsMarks;
int phyMarks;
}
class MarksDemo
{
public static void main(String args[])
{
//first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;
//second object
Marks obj2 = new Marks();
obj2.engMarks = 80;
obj2.mathsMarks = 60;
obj2.phyMarks = 85
//displaying marks for first object
System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);
//displaying marks for second object
System.out.println("Marks for second object:");
System.out.println(obj2.engMarks);
System.out.println(obj2.mathsMarks);
System.out.println(obj2.phyMarks);
}
}
Static Variables in Java:
We declare static keyword in Java same as the
instance variables, i.e. outside any object, block, or
constructor, the difference in the syntax is that it uses
the keyword static with it.
Example:
• import java.io.*;
• class Emp {
• // static variable salary
• public static double salary;
• public static String name = "Harsh";
• }
• public class EmpDemo
• {
• public static void main(String args[]) {
• //accessing static variable without object
• Emp.salary = 1000;
• System.out.println(Emp.name + "'s average salary:" + Emp.salary)
• }
• }
LITERALS

• Literals are number, text, or anything that represent a value.


• In other words, Literals in Java are the constant values assigned to
the variable. It is also called a constant
• For example,
• int x = 100;
• So, 100 is literal.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy