7. VARIABLEs presentation in java programming. Pdf

simukondasankananji8 54 views 63 slides Jul 19, 2024
Slide 1
Slide 1 of 63
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63

About This Presentation

Bcp


Slide Content

JavaVariables

Java Variables
•Variables arecontainers 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
typevariableName= value;
Where datatypeis one of Java's types (such as int or
String), variableNameis the name of the variable
(such as x or name), and valueis 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 myNumof 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.

JavaPrint
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

JavaDeclaration
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 thesame valueto multiple
variables in one line.

Types of Java
Variables

Types of Variables
There are three types of variables inJava:
•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 cannotbe 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 notget 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
•Cannotbe defined with "static" keyword

•Cannot be reinitializeddirectly within the class
class Variable1 {
int a = 10; //instant variable
a=15; // error
}

•However, an instant variable can be reinitialized
inside methodsor constructors
class Variable {
int a = 10; //instant variable
void someMethod(){
int a = 15; // allowed
}
}

•Variable cannotbe 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=newclass_name();
System.out.println(object_name.variable_name);

•But can be accessedthrough 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 cannotbe 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_idand gender, instance data is good in such
a case
•Since org_nameis 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="ChimbukaEnterprises 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 =“AnnitaKatuka”;
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 Javaspecify 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
TherearefourtypesofJavaaccessmodifiers:
•Default:Theaccesslevelofadefaultmodifieris
onlywithinthepackage.Itcannotbeaccessed
fromoutsidethepackage.Ifyoudonotspecify
anyaccesslevel,itwillbethedefault.
•Private: The access level of a private modifier is
only within the class. It cannot be accessed from
outside the class.

•Protected:Theaccesslevelofaprotected
modifieriswithinthepackageandoutsidethe
packagethroughchildclass.Ifyoudonotmake
thechildclass,itcannotbeaccessedfrom
outsidethepackage.
•Public:Theaccesslevelofapublicmodifieris
everywhere.Itcanbeaccessedfromwithinthe
class,outsidetheclass,withinthepackageand
outsidethepackage.

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
•Thestaticmodifiers are used to create class
methods and variables.

Static Variables/ Methods
•Thestatickeyword 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.
Tags