[go: up one dir, main page]

0% found this document useful (0 votes)
10 views27 pages

AMath 05 Chapter 1

The document discusses the scope of variables, which determines their accessibility and lifetime within a program, and explains that scope is defined by the placement of variable declarations within curly braces. It also covers typecasting, including implicit and explicit casting, and the use of wrapper classes to convert primitive types to objects. Additionally, it outlines access modifiers (default, public, protected, private) that control the visibility of class members to prevent unintended modifications.

Uploaded by

Blu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views27 pages

AMath 05 Chapter 1

The document discusses the scope of variables, which determines their accessibility and lifetime within a program, and explains that scope is defined by the placement of variable declarations within curly braces. It also covers typecasting, including implicit and explicit casting, and the use of wrapper classes to convert primitive types to objects. Additionally, it outlines access modifiers (default, public, protected, private) that control the visibility of class members to prevent unintended modifications.

Uploaded by

Blu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

SCOPE OF VARIABLES

CASTING
ACCESS MODIFIERS
PREPARED BY:

DANILYN A. FLORES
SCOPE OF VARIABLES
In addition to a variable’s data type and name, a variable
has scope.
Scope
 determines where in the program the variable is accessible.
 also determines the lifetime of a variable

Lifetime
 how long the variable can exist in memory

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


SCOPE CONTINUATION…
The scope is determined by where the variable declaration is
placed in the program.

Simply, scope is anything between the curly braces.

The outer pair of curly braces is called the outer block, and the
inner pair of curly braces is called the inner block.
3

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


SCOPE CONTINUATION…

If variables are declared in the outer block, they are visible (or usable) by the
program lines inside the inner blocks. However, if variables are declared in the
inner block, they are not visible (or usable) in the outer blocks.

A variable’s scope is inside the block where it is declared, starting from the
point where it is declared, and in the inner blocks.

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


SCOPE CONTINUATION…

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


SCOPE CONTINUATION…

Knowing the scope and lifetime of variables is important to avoid


programming errors.

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING
Typecasting
or simply casting is the process of converting a data of a
certain data type to another data type.

Casting Primitive Types


Casting between primitive types enables to convert the value of one
data from one type to another primitive type. This commonly occurs
between numeric types.
7

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING CONTINUED...
There is one primitive data type that cannot be casted: boolean

2 Types of Casting:
Implicit – data is converted from a smaller type to a larger type
Explicit – data is converted from a larger type to a smaller type
explicit cast is not always done because of possible loss of
precision
8

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING CONTINUED...
For example:
int numInt = 10;
double numDouble = numInt; //implicit cast

char valChar = ‘A’;


int valInt = valChar;

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING CONTINUED...
double valDouble = 10.12;
int valInt1 = (int)valDouble; //explicit cast

double x = 10.2;
int y = 2;
int result = (int)(x/y);

10

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING CONTINUED...

Converting Primitive Types to Objects


Primitive types and objects are very different things in Java and
they cannot be automatically casted or used interchangeably.

But there is a way to convert primitive types to objects: using


Wrapper Classes.

11

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING CONTINUED...
Wrapper Classes
Have the same name as the data types, except that the 1st
letter is capital
• double – Double, int – Integer
Data types and their classes are treated differently in Java
Using wrapper classes, an object that holds the same value as
the primitive data type can be created
12

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


CASTING CONTINUED...
For example:

Integer dataCount = new Integer(7801);


int newCount = dataCount.intValue();

String Pennsylvania = “65000”;


int penn = Integer.parseInt(Pennsylvania);
13

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS

Enables levels of restriction to access data in programs such as


properties and methods of classes to avoid side effects.

Side Effect
Modification in a variable’s value outside it’s scope that could affect other
variables

14

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

4 Types of Access Modifiers:


1. default
Only classes in the same package can have access to the class
variables and methods.
There is no keyword used for this type of access. It is applied in the
absence of an access modifier.

15

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

For example:
public class StudentRecord {
String name;
String getName() {
return name; }
}
16

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

2. public
Class members are accessible to anyone, both inside and outside the
class. Any object that interacts with the class can have access to the
public members of the class.

17

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…
For example:
public class StudentRecord {
public String name;
public String getName() {
return name; }
}

18

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

3. protected
Only the methods of the class and the subclasses of that class have
access to the class members.

19

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

For example:
public class StudentRecord {
protected String name;
protected String getName() {
return name; }
}
20

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

4. private
Class members are accessible only by the class they are defined in.

21

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


ACCESS MODIFIERS CONTINUED…

For example:
public class StudentRecord {
private String name;
private String getName() {
return name; }
}
22

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


SUMMARY
Scope (lifetime) determines where in the program the variable is
accessible.
Typecasting (casting) is the process of converting a data of a certain
data type to another data type: implicit or explicit
Wrapper Class is used to cast primitive types to objects
Access Modifiers enables levels of restriction to access data in
programs to avoid side effects which is the modification in a variable’s
value outside it’s scope that could affect other variables: default, public,
private, protected 23

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


FORMATIVE ASSESSMENT 1
•For items 1 – 3, refer to the given code below.
1 import javax.swing.JOptionPane;
2 public class Power{
3 public static void main (String[] args) {
4 int num = 0;
5 int pow = 0;
6 int numpow = 0;
7 String label = "";
8 String c = "";
9 do{
10 num = Integer.parseInt(JOptionPane.showInputDialog("Enter a whole number:"));
11 pow = Integer.parseInt(JOptionPane.showInputDialog("Enter Power (also another whole
12 number):"));
13 numpow = num;
14 for(int i = 1; i<pow; i++) {
15 numpow*=num; }
16 label = num + " to the power of "+ pow +" = ";
17 JOptionPane.showMessageDialog(null, label + numpow);
18 c = JOptionPane.showInputDialog("Do you wish to continue?... YES or NO ");} 24
19 while(c.equalsIgnoreCase("YES")); } }
SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS
FORMATIVE ASSESSMENT 1

1. Determine and label the scope of each variable in the given code.

2. What casting type/s is/are used?

3. What access modifier/s is/are used?

25

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS


REFERENCES

[1] Amilbahar, S.J., Flores, D.A. (2010). Computer Programming II. Unpublished Workbook, University of Southern Mindanao,
Kabacan, Cotabato.
[2] Burd, B. (2017). Beginning Programming with Java For Dummies (5th Ed). New Jersey: John Wiley and Sons, Inc.
[3] Flask, R. (ND). Java for Beginners 2nd Edition [PDF File]. Retrieved from
http://staff.um.edu.mt/__data/assets/pdf_file/0010/57169/jn.pdf
[4] Mayfield, B. (2016). From Problem Analysis to Program Design Lab Manual (3rd ed.). Philippines: Cengage Learning Asia Pte
Ltd.
[5] https://funprogramming.org/50-What-are-global-and-local-variables.html
[6] https://www.geeksforgeeks.org/type-conversion-in-c/
[7] https://www.w3schools.com/java/java_wrapper_classes.asp
[8] https://www.w3schools.com/java/java_data_types.asp

SCOPE OF VARIABLES, CASTING, ACCESS MODIFIERS 26


END
THANK YOU!
#WEHEALASONE

You might also like