SlideShare a Scribd company logo
Java Variables
Java Variables
•Variables are containers for storing data
values.
•In Java, there are different types of variables, for
example: String - stores text, such as "Hello".
String values are surrounded by double quotes.
int - stores integers (whole numbers), without
decimals, such as 123 or -123.
In Java, there are different types of variables, for
example:
•String - stores text, such as "Hello". String values
are surrounded by double quotes
•int - stores integers (whole numbers), without
decimals, such as 123 or -123
•float - stores floating point numbers, with
decimals, such as 19.99 or -19.99
•char - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
•boolean - stores values with two states: true or
false
Task
•Define the term data type.
•Explain briefly each of the following data
types and give examples:
oPrimitive
oNon-primitive
Declaring (Creating) Variables
•To create a variable, you must specify the type and
assign it a value:
Syntax
type variableName = value;
Where data type is one of Java's types (such as int or
String), variableName is the name of the variable
(such as x or name), and value is the actual value
assigned to the variable (such as 15 or John).
Note:
•Variable name should begin with a lowercase
letter.
•In mixed case, the first letter of each internal
word should be uppercase
Example 1 (To create a variable that
should store text)
Create a variable called name of type String and
assign it the value "John":
Example 2 (To create a variable that should
store a number)
Create a variable called myNum of type int and
assign it the value 15:
Example 3 (You can also declare a variable
without assigning the value)
Example 4
Note:
•If you assign a new value to an existing variable,
it will overwrite the previous value.
Final Variables
•If you don't want others (or yourself) to
overwrite existing values, use the final keyword
(this will declare the variable as "final" or
"constant", which means unchangeable and read-
only)
Example
Other Types
A demonstration of how to declare variables of
other types.
Java Print
Variables
Display Variables
Example 1
•The println() method is often used to display
variables.
•To combine both text and a variable, use the +
character:
Example 2
•You can also use the + character to add a variable
to another variable.
Example 3
•For numeric values, the + character works as a
mathematical operator (notice that we use int
(integer) variables here).
Activity
Carryout the operation for the stored values of x
and y
•x stores the value 5
•y stores the value 6
Java Declaration
of Multiple
Variables
Declare Many Variables
•To declare more than one
variable of the same type, you
can use a comma-separated list.
Instead of writing:
You can simply write:
One Value to Multiple Variables
•You can also assign the same value to multiple
variables in one line.
Types of Java
Variables
Types of Variables
There are three types of variables in Java:
•local variable
•instance variable
•static variable
Local Variable
•A variable declared inside the body of the
method is called local variable
•The variable can only be accessed/ used within
that method, and the other methods in the class
aren't even aware that the variable exists
•A local variable cannot be defined with "static"
keyword
• A local variable is accessed directly
class Variable {
public static void main(String args[]) {
int b = 20; //local variable
System.out.println (b); /* The code runs,
and 20 is displayed*/
}
}
• Does not get a default value
class Variable {
public static void main(String args[]) {
int b; //local variable
System.out.println (b); /* The code does not run,
"variable b might not have been initialized“
is displayed*/
}
}
Instance Variable
•It is variable that has a value that is specific to a
particular case/ occasion and is not shared among
instances.
•A variable declared inside the class but outside
the body of the method is called an instance
variable
•Cannot be defined with "static" keyword
•Cannot be reinitialized directly within the class
class Variable1 {
int a = 10; //instant variable
a=15; // error
}
•However, an instant variable can be reinitialized
inside methods or constructors
class Variable {
int a = 10; //instant variable
void someMethod(){
int a = 15; // allowed
}
}
• Variable cannot be accessed directly, but
through objects
class Variable {
int a = 10; //instant variable
public static void main(String args[]) {
System.out.println (a); /* code does not
run because an object is not created*/
}
How to create an object
class_name object_name = new class_name();
System.out.println (object_name.variable_name);
• But can be accessed through objects
class Variable {
int a = 10; //instant variable
public static void main(String args[]) {
Variable obj = new Variable (); // object creation
System.out.println (obj.a); /* The code runs, and
10 is displayed*/
}
}
•Always get a default value, and the default is 0/
null, unless they are assigned with another value.
int data;
data=0;
class Variable {
int a; //instant variable
public static void main(String args[]) {
Variable obj = new Variable (); // object creation
System.out.println (obj.a); /* The code runs,
and 0 is displayed*/
}
}
class Variable {
String a; //instant variable
public static void main(String args[]) {
Variable obj = new Variable (); // object creation
System.out.println (obj.a); /* The code runs, and
null is displayed*/
}
}
Static variable
•Is a variable that belongs to the class itself rather than
to any specific instance of a class.
•It is used to refer to the common property of all
objects
•It cannot be local
•It makes a program memory efficient (i.e, it saves
memory)
•Always get a default value, and the default is 0/
null, unless they are assigned with another value.
String name;
name = nill;
•It is accessed directly
How it works
Class Employee1{
String org_name;
String emp_name;
int emp_id;
gender;
}
•If there are many employees in the the
organisation, all instance data members will get
memory each time an object is created
•Since all employees have a unique emp_name,
emp_id and gender, instance data is good in such
a case
•Since org_name is a common property for all the
objects/ instances, it is supposed to be made a
static variable so that it is loaded only once in
memory
•You can create a single copy of the static variable
and share it among all the instances of the class.
•Memory allocation for static variables happens
only once when the class is loaded in the
memory.
public class Employee1 {
//Variables declared
String org_name="Chimbuka Enterprises Limited";
String emp_name;
int emp_id;
void display(){
System.out.println(org_name+" "+emp_name+" "+emp_id);
}
public static void main(String[] args) {
//Creating objects
Employee1 e1=new Employee1();
e1.emp_name =“Annita Katuka”;
e1.emp_id=970985777;
e1.gender=‘F’
Employee1 e2=new Employee1();
e2.emp_name =“Edward Dipa”;
e2.emp_id=958777445;
e2.gender=‘M’
//Calling display method
e1.display();
e2.display();
}
Modifiers
•The modifiers in Java specify the accessibility
or scope of a field/ variable, method, constructor,
or class.
•Using the modifiers the scope or accessibility of
classes, methods, constructors, and other
members could be set.
•The access level of fields, constructors, methods,
and class could be changed by applying the
access modifier on it.
Access Modifiers
Access Modifiers: control the access level
Modifiers For Classes
•Default: The class is only accessible by classes
in the same package. This is used when you don't
specify a modifier.
•Public: The class is accessible by any other
class.
Modifiers For Attributes, Methods and
Constructors
There are four types of Java access modifiers:
•Default: The access level of a default modifier is
only within the package. It cannot be accessed
from outside the package. If you do not specify
any access level, it will be the default.
•Private: The access level of a private modifier is
only within the class. It cannot be accessed from
outside the class.
•Protected: The access level of a protected
modifier is within the package and outside the
package through child class. If you do not make
the child class, it cannot be accessed from
outside the package.
•Public: The access level of a public modifier is
everywhere. It can be accessed from within the
class, outside the class, within the package and
outside the package.
Understanding the access modifiers in
Java by a simple table
Non-Access Modifiers
Non-access modifiers: provide/ achieve many
other functionalities.
Non-Access Modifiers For Classes
•final: The class cannot be inherited by other
classes
•abstract: The class cannot be used to create
objects (To access an abstract class, it must be
inherited from another class.)
Non-Access Modifiers For Attributes,
Methods and Constructors
•final: Attributes and methods cannot be
overridden/modified
•Static: Attributes and methods belongs to the
class, rather than an object
•Abstract: Can only be used in an abstract
class, and can only be used on methods.
•The method does not have a body, for example
abstract void run();.
•The body is provided by the subclass (inherited
from).
The Static Modifier
•The static modifiers are used to create class
methods and variables.
Static Variables/ Methods
•The static keyword is used to create variables
and methods that belong to a class rather than an
instance of a class
•A single copy of the static variable is shared
among all the instances of the class
•A static method can be called without
creating an object of the class
•Static variables and static methods are also
known as class variables and class methods
respectively
•Memory allocation for static variables happens
only once when the class is loaded in the
memory.

More Related Content

Similar to 7. VARIABLEs presentation in java programming. Pdf (20)

PPTX
class as the basis.pptx
Epsiba1
 
PPTX
Module 1.pptx
YakaviBalakrishnan
 
PDF
Java data types, variables and jvm
Madishetty Prathibha
 
PPT
Java-Variables_about_different_Scope.ppt
JyothiAmpally
 
PPT
Md03 - part3
Rakesh Madugula
 
PPTX
Learning Java 2 - Variables, Data Types and Operators
MinhNguyen1493
 
PPTX
Constant a,variables and data types
mithilesh kumar
 
PPTX
Core java
Shivaraj R
 
PPTX
Core java
Sun Technlogies
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
Java fundamentals
Jayfee Ramos
 
PDF
java-basics-1.pdf jfjf hjghjgkj df jfjf hjghjgkj df jfjf hjghjgkj df jfjf hjg...
Indu32
 
PPTX
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
DOCX
Computer Programming 2
VasanthiMuniasamy2
 
PPTX
Introduction to Java Basics Programming-II.pptx
SANDHYAP32
 
PDF
ITFT - Java
Blossom Sood
 
PPTX
intro_java (1).pptx
SmitNikumbh
 
PPTX
Introduction to Java.pptx sjskmdkdmdkdmdkdkd
giresumit9
 
PPTX
javaClasses.pptx
MattMarino13
 
class as the basis.pptx
Epsiba1
 
Module 1.pptx
YakaviBalakrishnan
 
Java data types, variables and jvm
Madishetty Prathibha
 
Java-Variables_about_different_Scope.ppt
JyothiAmpally
 
Md03 - part3
Rakesh Madugula
 
Learning Java 2 - Variables, Data Types and Operators
MinhNguyen1493
 
Constant a,variables and data types
mithilesh kumar
 
Core java
Shivaraj R
 
Core java
Sun Technlogies
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
Java fundamentals
Jayfee Ramos
 
java-basics-1.pdf jfjf hjghjgkj df jfjf hjghjgkj df jfjf hjghjgkj df jfjf hjg...
Indu32
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
Computer Programming 2
VasanthiMuniasamy2
 
Introduction to Java Basics Programming-II.pptx
SANDHYAP32
 
ITFT - Java
Blossom Sood
 
intro_java (1).pptx
SmitNikumbh
 
Introduction to Java.pptx sjskmdkdmdkdmdkdkd
giresumit9
 
javaClasses.pptx
MattMarino13
 

Recently uploaded (20)

PDF
07/25 - LOUIS VUITTON - DUBAI - UAE_MAURO MANCINI PM ANTEFIXE
Mauro Mancini
 
PDF
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
PPTX
SAMPLE FILE OF-PPT-FINAL-ORAL-DEFENSE.pptx
Yvez2
 
PDF
The Role of Logos as Identity Shapers (IFIC Logo)
Md. Mehedi Hasan Asif
 
PDF
Dynamic Visuals for NJ Commercial Spaces
Yantram Animation Studio Corporation
 
PPTX
feminist gnsudnshxujenduxhsixisjxuu.pptx
rowvinafujimoto
 
PPTX
CHINA CONVENTION CENTRE casestudy and analysis
FullygamesTech
 
PDF
Presentation of design made by power point
habibikuw002
 
PPTX
英国硕士毕业证伦敦城市大学学位证书City毕业完成信办理
Taqyea
 
DOCX
Redefining Master Plans for creating sustainable cities-Jharkhand Conference...
JIT KUMAR GUPTA
 
PDF
Case Study on good and bad acoustics in auditorium
Disha Agrawal
 
PDF
Uber Driver Hackday Sprint Solving Ride Cancellations
YellowSlice1
 
PPTX
the very teaching plan extra ordinary.pptx
PamelaOdibeli1
 
PPTX
DIASS-DIAGNOSTIC-TEST.pptxnjkhbuyygygccd
mcsprima2023
 
PPT
Origin of the solar system acording .ppt
ReignLachica
 
PPTX
N-doped FSHC 2nrdddddddddddddddddrrrd.pptx
71762306019
 
PDF
IPC_Reference_manual_Vol_1_Final (1).pdf
AbrahamFekede1
 
PPTX
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
PPTX
In the sweet by and by, We shall meet on that beautiful shore; In the sweet b...
kuysniya14
 
PPTX
High-Rise Interior Mastery by Top 3D Visualization Experts
Yantram Animation Studio Corporation
 
07/25 - LOUIS VUITTON - DUBAI - UAE_MAURO MANCINI PM ANTEFIXE
Mauro Mancini
 
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
SAMPLE FILE OF-PPT-FINAL-ORAL-DEFENSE.pptx
Yvez2
 
The Role of Logos as Identity Shapers (IFIC Logo)
Md. Mehedi Hasan Asif
 
Dynamic Visuals for NJ Commercial Spaces
Yantram Animation Studio Corporation
 
feminist gnsudnshxujenduxhsixisjxuu.pptx
rowvinafujimoto
 
CHINA CONVENTION CENTRE casestudy and analysis
FullygamesTech
 
Presentation of design made by power point
habibikuw002
 
英国硕士毕业证伦敦城市大学学位证书City毕业完成信办理
Taqyea
 
Redefining Master Plans for creating sustainable cities-Jharkhand Conference...
JIT KUMAR GUPTA
 
Case Study on good and bad acoustics in auditorium
Disha Agrawal
 
Uber Driver Hackday Sprint Solving Ride Cancellations
YellowSlice1
 
the very teaching plan extra ordinary.pptx
PamelaOdibeli1
 
DIASS-DIAGNOSTIC-TEST.pptxnjkhbuyygygccd
mcsprima2023
 
Origin of the solar system acording .ppt
ReignLachica
 
N-doped FSHC 2nrdddddddddddddddddrrrd.pptx
71762306019
 
IPC_Reference_manual_Vol_1_Final (1).pdf
AbrahamFekede1
 
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
In the sweet by and by, We shall meet on that beautiful shore; In the sweet b...
kuysniya14
 
High-Rise Interior Mastery by Top 3D Visualization Experts
Yantram Animation Studio Corporation
 
Ad

7. VARIABLEs presentation in java programming. Pdf

  • 2. Java Variables •Variables are containers for storing data values. •In Java, there are different types of variables, for example: String - stores text, such as "Hello". String values are surrounded by double quotes. int - stores integers (whole numbers), without decimals, such as 123 or -123.
  • 3. In Java, there are different types of variables, for example: •String - stores text, such as "Hello". String values are surrounded by double quotes •int - stores integers (whole numbers), without decimals, such as 123 or -123
  • 4. •float - stores floating point numbers, with decimals, such as 19.99 or -19.99 •char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes •boolean - stores values with two states: true or false
  • 5. Task •Define the term data type. •Explain briefly each of the following data types and give examples: oPrimitive oNon-primitive
  • 6. Declaring (Creating) Variables •To create a variable, you must specify the type and assign it a value: Syntax type variableName = value; Where data type is one of Java's types (such as int or String), variableName is the name of the variable (such as x or name), and value is the actual value assigned to the variable (such as 15 or John).
  • 7. Note: •Variable name should begin with a lowercase letter. •In mixed case, the first letter of each internal word should be uppercase
  • 8. Example 1 (To create a variable that should store text) Create a variable called name of type String and assign it the value "John":
  • 9. Example 2 (To create a variable that should store a number) Create a variable called myNum of type int and assign it the value 15:
  • 10. Example 3 (You can also declare a variable without assigning the value)
  • 11. Example 4 Note: •If you assign a new value to an existing variable, it will overwrite the previous value.
  • 12. Final Variables •If you don't want others (or yourself) to overwrite existing values, use the final keyword (this will declare the variable as "final" or "constant", which means unchangeable and read- only)
  • 14. Other Types A demonstration of how to declare variables of other types.
  • 16. Example 1 •The println() method is often used to display variables. •To combine both text and a variable, use the + character:
  • 17. Example 2 •You can also use the + character to add a variable to another variable.
  • 18. Example 3 •For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here).
  • 19. Activity Carryout the operation for the stored values of x and y •x stores the value 5 •y stores the value 6
  • 21. Declare Many Variables •To declare more than one variable of the same type, you can use a comma-separated list.
  • 23. You can simply write:
  • 24. One Value to Multiple Variables •You can also assign the same value to multiple variables in one line.
  • 26. Types of Variables There are three types of variables in Java: •local variable •instance variable •static variable
  • 27. Local Variable •A variable declared inside the body of the method is called local variable •The variable can only be accessed/ used within that method, and the other methods in the class aren't even aware that the variable exists
  • 28. •A local variable cannot be defined with "static" keyword
  • 29. • A local variable is accessed directly class Variable { public static void main(String args[]) { int b = 20; //local variable System.out.println (b); /* The code runs, and 20 is displayed*/ } }
  • 30. • Does not get a default value class Variable { public static void main(String args[]) { int b; //local variable System.out.println (b); /* The code does not run, "variable b might not have been initialized“ is displayed*/ } }
  • 31. Instance Variable •It is variable that has a value that is specific to a particular case/ occasion and is not shared among instances.
  • 32. •A variable declared inside the class but outside the body of the method is called an instance variable •Cannot be defined with "static" keyword
  • 33. •Cannot be reinitialized directly within the class class Variable1 { int a = 10; //instant variable a=15; // error }
  • 34. •However, an instant variable can be reinitialized inside methods or constructors class Variable { int a = 10; //instant variable void someMethod(){ int a = 15; // allowed } }
  • 35. • Variable cannot be accessed directly, but through objects class Variable { int a = 10; //instant variable public static void main(String args[]) { System.out.println (a); /* code does not run because an object is not created*/ }
  • 36. How to create an object class_name object_name = new class_name(); System.out.println (object_name.variable_name);
  • 37. • But can be accessed through objects class Variable { int a = 10; //instant variable public static void main(String args[]) { Variable obj = new Variable (); // object creation System.out.println (obj.a); /* The code runs, and 10 is displayed*/ } }
  • 38. •Always get a default value, and the default is 0/ null, unless they are assigned with another value. int data; data=0;
  • 39. class Variable { int a; //instant variable public static void main(String args[]) { Variable obj = new Variable (); // object creation System.out.println (obj.a); /* The code runs, and 0 is displayed*/ } }
  • 40. class Variable { String a; //instant variable public static void main(String args[]) { Variable obj = new Variable (); // object creation System.out.println (obj.a); /* The code runs, and null is displayed*/ } }
  • 41. Static variable •Is a variable that belongs to the class itself rather than to any specific instance of a class. •It is used to refer to the common property of all objects •It cannot be local •It makes a program memory efficient (i.e, it saves memory)
  • 42. •Always get a default value, and the default is 0/ null, unless they are assigned with another value. String name; name = nill;
  • 43. •It is accessed directly
  • 44. How it works Class Employee1{ String org_name; String emp_name; int emp_id; gender; }
  • 45. •If there are many employees in the the organisation, all instance data members will get memory each time an object is created •Since all employees have a unique emp_name, emp_id and gender, instance data is good in such a case •Since org_name is a common property for all the objects/ instances, it is supposed to be made a static variable so that it is loaded only once in memory
  • 46. •You can create a single copy of the static variable and share it among all the instances of the class. •Memory allocation for static variables happens only once when the class is loaded in the memory.
  • 47. public class Employee1 { //Variables declared String org_name="Chimbuka Enterprises Limited"; String emp_name; int emp_id; void display(){ System.out.println(org_name+" "+emp_name+" "+emp_id); }
  • 48. public static void main(String[] args) { //Creating objects Employee1 e1=new Employee1(); e1.emp_name =“Annita Katuka”; e1.emp_id=970985777; e1.gender=‘F’ Employee1 e2=new Employee1(); e2.emp_name =“Edward Dipa”; e2.emp_id=958777445; e2.gender=‘M’ //Calling display method e1.display(); e2.display(); }
  • 49. Modifiers •The modifiers in Java specify the accessibility or scope of a field/ variable, method, constructor, or class.
  • 50. •Using the modifiers the scope or accessibility of classes, methods, constructors, and other members could be set. •The access level of fields, constructors, methods, and class could be changed by applying the access modifier on it.
  • 51. Access Modifiers Access Modifiers: control the access level
  • 52. Modifiers For Classes •Default: The class is only accessible by classes in the same package. This is used when you don't specify a modifier. •Public: The class is accessible by any other class.
  • 53. Modifiers For Attributes, Methods and Constructors There are four types of Java access modifiers: •Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default. •Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
  • 54. •Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package. •Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
  • 55. Understanding the access modifiers in Java by a simple table
  • 56. Non-Access Modifiers Non-access modifiers: provide/ achieve many other functionalities.
  • 57. Non-Access Modifiers For Classes •final: The class cannot be inherited by other classes •abstract: The class cannot be used to create objects (To access an abstract class, it must be inherited from another class.)
  • 58. Non-Access Modifiers For Attributes, Methods and Constructors •final: Attributes and methods cannot be overridden/modified •Static: Attributes and methods belongs to the class, rather than an object
  • 59. •Abstract: Can only be used in an abstract class, and can only be used on methods. •The method does not have a body, for example abstract void run();. •The body is provided by the subclass (inherited from).
  • 60. The Static Modifier •The static modifiers are used to create class methods and variables.
  • 61. Static Variables/ Methods •The static keyword is used to create variables and methods that belong to a class rather than an instance of a class •A single copy of the static variable is shared among all the instances of the class
  • 62. •A static method can be called without creating an object of the class •Static variables and static methods are also known as class variables and class methods respectively
  • 63. •Memory allocation for static variables happens only once when the class is loaded in the memory.