Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Similar documents
Number Representation & Conversion

Operators and Expressions

Zheng-Liang Lu Java Programming 45 / 79

Lecture Set 4: More About Methods and More About Operators

Expressions & Flow Control

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

JAVA OPERATORS GENERAL

Chapter 3: Operators, Expressions and Type Conversion

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Exercises Software Development I. 08 Objects II. Generating and Releasing Objects (Constructors/Destructors, this, Object cloning) December 3rd, 2014

Prof. Navrati Saxena TA: Rochak Sachan

Lecture Set 4: More About Methods and More About Operators

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java Programming Language. 0 A history

CMPT 125: Lecture 3 Data and Expressions

3. Java - Language Constructs I

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Introduction to Programming Using Java (98-388)

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Primitive Data Types: Intro

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Program Fundamentals

Java Primer 1: Types, Classes and Operators

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Chapter 6 Primitive types

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

ESc101 : Fundamental of Computing

CS111: PROGRAMMING LANGUAGE II

1.00 Lecture 4. Promotion

Selenium Class 9 - Java Operators

Chapter 2: Data and Expressions

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Java Fall 2018 Margaret Reid-Miller

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Primitive Data, Variables, and Expressions; Simple Conditional Execution

More Programming Constructs -- Introduction

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Programming with Java

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CS 112 Introduction to Programming

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

CS260 Intro to Java & Android 03.Java Language Basics

The SPL Programming Language Reference Manual

CSC 1214: Object-Oriented Programming

CS 231 Data Structures and Algorithms, Fall 2016

CS61B Lecture #14: Integers

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Example: Tax year 2000

PROGRAMMING FUNDAMENTALS

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Special Topics: Programming Languages

Computer Science II (20082) Week 1: Review and Inheritance

Chapter 2: Using Data

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Integer Representation

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

1.3b Type Conversion

Full file at

JAVA Programming Fundamentals

Chapter 2: Data and Expressions

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

ECE 122 Engineering Problem Solving with Java

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Programming Language Basics

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS110D: PROGRAMMING LANGUAGE I

Chapter 2: Data and Expressions

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Declaration and Memory

Expressions and Casting

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

CS Programming I: Primitives and Expressions

Computers Programming Course 5. Iulian Năstac

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

The PCAT Programming Language Reference Manual

Selected Questions from by Nageshwara Rao

CIS 110: Introduction to Computer Programming

Exercises Software Development I. 06 Arrays. Declaration, Initialization, Usage // Multi-dimensional Arrays. November 14, 2012

COMP6700/2140 Data and Types

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Chapter 2: Using Data

Transcription:

Exercises Software Development I 05 Conversions and Promotions; Lifetime, Scope, Shadowing November 5th, 2014 Software Development I Winter term 2014/2015 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute for Pervasive Computing Johannes Kepler University Linz riener@pervasive.jku.at

Conversions and Promotions

Operations on Mixed Data Types: Conversions and Promotions Every expression in Java has a specific data type... Can be deduced from the structure of the expression and the types of the literals, variables, and methods mentioned in the expression It is possible, however, to write an expression in a context where the type of the expression is not appropriate in some cases this leads to an error at compile time in other cases the context may be able to accept a type that is related to the type of the expression; (during the assignment, the compiler performs an implicit casting conversion rather than requesting the programmer to specifiy an explicit conversion) short shortvar = 1000; // int literal, is implicitly casted to short Java specifies five conversion contexts in which conversion of expressions may occur: casting conversion, String conversion, assignment conversion, numeric promotion, method invocation conversion Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 3

Operations on Mixed Data Types: Conversion Contexts - Example public class ConversionContexts { public static void main(string[] args) { // Casting conversion of a float literal to type int (without the cast operator, this // would be a compile-time error, because this is a narrowing conversion) int i = (int)12.5f; // String conversion of i's int value System.out.println("(int)12.5f==" + i); // Assignment conversion of i's value to type float (this is a widening conversion) float f = i; // String conversion of f's float value System.out.println("after float widening: " + f); // NUMERIC PROMOTION of i's value to type float (this is a binary numeric promotion); // after promotion, the operation is float*float System.out.print(f); f = f * i; // Two string conversions of i and f System.out.println("*" + i + "==" + f); // Method invocation conversion of f's value to type double, needed because the method // Math.sin accepts only a double argument double d = Math.sin(f); Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 4

I Operations on Mixed Data Types: Numeric Promotions Numeric promotion is applied to the operands of an arithmetic operator Numeric promotions are used to convert the operands of a numeric operator (on literals and variables) to a common type so that an operation can be performed example: numeric operation involving different data types 17.0 double + 4 int two kinds of numeric promotions: unary numeric promotion and binary numeric promotion numeric promotion contexts allow the use of - an identity conversion - a widening conversion - an unboxing conversion Data type conversion is also required on data types "shorter" (smaller) than int (float, double). Why? Numeric operations are only defined for int (or larger types), never on byte, char, or short type conversion table! numeric promotion: "widening conversion" of all operands smaller than int ("smallest" target data type is int), operation is executed after the conversion Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 5

Operations on Mixed Data Types: Numeric Promotions Numeric promotion is applied to the operands of an arithmetic operator Example byte b = 100; byte c = 100, d; d = b * c; Is this operation valid in Java? no, operation is illegal - result of the multiplication operation is of type int - result assignment requires "narrowing conversion" (explicit type cast) to data type byte ok, but what about: d = (byte) b*c;? - illegal; the unary cast operator has higher priority than the multiplication operator correct operation: d = (byte)(b*c); Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 6

I Operations on Mixed Data Types: Numeric Promotions Conversion rules One operand double second operand converted to double Else: one operand float second operand converted to float Else: one operand long second operand converted to long Else: both operands converted to int Affected operators multiplicative operators *, /, % addition, subtraction +, - numeric comparison <, <=, >, >= numeric parity/imparity ==,!= bitwise operations &,, ^ Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 7

Operations on Mixed Data Types: Widening Conversion - Example public class Widening Conversion { public static void main(string[] args) { int big = 1234567890; float approx = big; System.out.println(big - (int)approx); // Expected: ~0, actual output: -46 System.out.println((float)big - approx); // Expected: ~0, actual output: 0.0 information was lost during the conversion from type int to type float (values of type float are not precise to nine significant digits) information lost two times difference is now 0.0 ; output is 1.23456794E9 Widening and Narrowing Primitive Conversion The following examples combines both widening and narrowing conversions byte bytea = 65; char chara; chara = 65; bytea = 65; chara = (byte)65; chara = bytea; OK OK OK first, the byte is converted to an int via widening conversion then the resulting int is converted to a char by narrowing conversion NO! Type mismatch: cannot convert from byte to char! Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 8

Operations on Mixed Data Types: Widening Conversion - Example public class Widening Conversion { public static void main(string[] args) { int big = 1234567890; float approx = big; System.out.println(big - (int)approx); // Expected output: ~0 // Actual output: -46! information was lost during the conversion from type int to type float (values of type float are not precise to nine significant digits) Widening and Narrowing Primitive Conversion The following example combines both widening and narrowing conversions byte bytea = 65; char chara; chara = bytea; first, the byte is converted to an int via widening conversion then the resulting int is converted to a char by narrowing conversion Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 9

Operations on Mixed Data Types: Numeric Promotions Conversion rules (detailed) A widening conversion does not lose information about the overall magnitude of a numeric value A widening conversion from an integral type to another integral type, or from float to double (FP-strict expression only), does not lose any information at all; the numeric value is preserved exactly non FP-strict float to double widening conversion may lose information about the overall magnitude of the converted value. A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value A widening conversion of a signed integer value to an integral type simply sign-extends the two's-complement of the integer value (fill-up) A widening conversion of a char to an integral type zero-extends the representation of the char value to fill the wider format Despite the fact that loss of precision may occur, a widening primitive conversion never results in a run-time exception Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 10

Operations on Mixed Data Types: Unary Numeric Promotion public class UnaryPromotion { public static void main(string[] args) { byte b = 2; int a[] = new int[b]; // dimension expression promotion char c = '\u0001'; a[c] = 1; // index expression promotion a[0] = -c; // unary numeric promotion System.out.println("a: " + a[0] + "," + a[1]); b = -1; int i = ~b; // bitwise complement promotion System.out.println("~0x" + Integer.toHexString(b) + "==0x" + Integer.toHexString(i)); i = b << 4L; // shift promotion (left operand) System.out.println("0x" + Integer.toHexString(b) + "<<4L==0x" + Integer.toHexString(i)); Program output: a: -1,1 ~0xffffffff==0x0 0xffffffff<<4L==0xfffffff0 Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 11

Operations on Mixed Data Types: Unary Numeric Promotion public class UnaryPromotion { public static void main(string[] args) { byte b = 2; int a[] = new int[b]; // dimension expression promotion char c = '\u0001'; a[c] = 1; // index expression promotion a[0] = -c; // unary numeric promotion System.out.println("a: " + a[0] + "," + a[1]); b = -1; int i = ~b; // bitwise complement promotion System.out.println("~0x" + Integer.toHexString(b) + "==0x" + Integer.toHexString(i)); i = b << 4L; // shift promotion (left operand) System.out.println("0x" + Integer.toHexString(b) + "<<4L==0x" + Integer.toHexString(i)); Program output: a: -1,1 ~0xffffffff==0x0 0xffffffff<<4L==0xfffffff0 Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 12

Operations on Mixed Data Types: Binary Numeric Promotion public class BinaryPromotion { public static void main(string[] args) { int i = 0; float f = 1.0f; double d = 2.0; // First int * float is promoted to float * float, then float == double is // promoted to double == double if (i * f == d) System.out.println("oops"); // A char & byte is promoted to int & int byte b = 0x1f; char c = 'G'; int control = c & b; System.out.println(Integer.toHexString(control)); // Here int:float is promoted to float:float f = (b==0)? i : 4.0f; System.out.println(1.0/f); Program output: 7 0.25 Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 13

I Operations on Mixed Data Types: Character Promotions Recall: It is possible to perform numeric calculations using characters Example 1 int x = 'a' 'a'; int y = 'b' 'a'; Example 2 char f = 'a' + 5; Example 3 char c = 'a'; c = c + 5; c = c + (char)5; c = (char) (c + 5); // 97 97 = 0 // 98 97 = 1 // ok; 'a'...char literal, // 5...int literal // ok, correct assignment // error, result type 'int' // error, extended to int, result type 'int' // char + char = int // ok, int result with explicit cast to char Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 14

I Operations on Mixed Data Types: Binary+Character Promotions public class BinaryCharPromotion { public static void main(string[] args) { // int + int literals; implicit cast to short (value fits!) short s = 5 + 5; System.out.println(s); // int + int literals; implicit cast to byte (value fits!) byte b1 = 27 + 100; System.out.println(b1); // int + int literals; result = 128? (value does not fit into byte)? byte b2 = 28 + 100; System.out.println(b2); // continued on next slide... Program output: 10 127 Type mismatch: cannot convert from int to byte at BinaryPromotion.main(BinaryPromotion.java:12) Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 15

I Operations on Mixed Data Types: Binary+Character Promotions char f = 'a' + 5; // char in Java: 16bit (Unicode) System.out.println(f); // operation on 'int' type (automatic widening+narrowing conversion) f = 'a' - 'a' + 65535; // maximum value for 16bit/char = 2^16-1 = 65535 System.out.println(f); // max. value for char+1 -> overflow or automatic extension char -> int? f = 'a' - 'a' + 65536; System.out.println(f); Program output: f = character in Unicode table at position 65535 Type mismatch: cannot convert from int to char... Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 16

Operations on Mixed Data Types: Operator Differences i+=f, i=i+f? public class OperatorDifferences { public static void main(string[] args) { int i = 10; float f = 1.23f; // += operator has strong binding to 'i' (double function: result, operand) i += f; System.out.println(i); // i = i + (int)f; // "normal" widening conversion: int -> float i = i + f; // i(int) = i(float) +(float) f(float) System.out.println(i); // i(int) = 10.0f + 1.23f = 11.23f Program output: 11 Type mismatch: cannot convert from float to int at OperatorDifferences.main(OperatorDifferences.java:12) Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 17

Lifetime, Scope, Shadowing

I Declaration: Variables, Constants Variables Declaration introduces a new variable with name, data type, (initial value) Before its first use (in general at the begin of the program, right after public static void main() Preferred "local", not "global" declaration (i.e., start of the innermost block that uses this variable) not mixed with assignments Constants Declaration introduces a new constant with name, data type, fixed value In general at the very begin of the program (before main method), e.g. public class ConstantDeclaration { static final int FACTOR = 10; // integer constant, value 10 static final boolean NO = false; // boolean constant, value false public static void main(string[] args) { NO = true; //??? Compilation error: The final field // ConstantDeclaration.NO cannot be assigned Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 19

Declaration: Variables, Constants - Example public class ConstantDeclaration { static final int FACTOR; // =0; //error: the blank final field FACTOR may not have been initialized static final int FACTOR2 = 10; public static void main(string[] args) { final int FACTOR3 = 20; FACTOR3 = 30; //error: the final local variable FACTOR3 cannot be assigned... System.out.println(FACTOR); System.out.println(FACTOR2); System.out.println(FACTOR3); Program output: 0 10 20 Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 20

I Declaration: Variables, Constants - Example public class ConstantDeclaration2 { static int decfact = 10; // variable, global, not a constant! static int hexfact = binfact * 8; // error: 'binfact' not yet declared static int binfact = 2; // ok, variable, global final static int OCTFACT = binfact * 4; // ok, constant, global, uses 'binfact' to inizialize // Notice: Use global variables only if more than just one method accesses it; // declare variables in general locally (i.e. within a block or method). public static void main (String[] arg) {// variable 'arg', type String[] int i = decfact, j = 2 * i; // two variables 'i', 'j'; local // Notice: global variable 'decfact' used to initialize 'i'; once 'i' is // declared it can already be used to initialize 'j' int num = Input.readInt(); System.out.println("num: " + num + ", decfact: " + decfact + ", i: " + i); while (num > 0) { int decfact = binfact; // local 'decfact' overlaps global 'decfact'; shadowing num = num / decfact; // local 'decfact' used in the division! System.out.println("num: " + num + ", decfact: " + decfact); Program output: 3 num: 3, decfact: 10, i: 10 num: 1, decfact: 2 num: 0, decfact: 2 Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 21

I Declaration of Variables, Constants: Lifetime (Durability) Lifetime of Variables is the time from its creation (in memory) until the point in time where it is released again (deleted in memory, "freed" by garbage collector), i.e., lifetime is the time the variable is accessible In Java, lifetime of variables is distinguished in three contexts local variables are declared in methods and blocks and are created for each execution of the method or block; after the execution of the method/block completes, local (non-final) variables are no longer accessible; method parameter exist from the procedure entry until its end as long as the procedure is active, i.e., in memory and executed global (static) variables [class variables, not instance variables] are created when the class is loaded at runtime, and exist as long as the class exists (i.e., from start to termination of the program) instance variables [members of a class] are created for each object of the class (i.e., every object of the class will have its own copies of these variables); the values of these variables at any given time constitute the state of the object; instance variables exist as long as the corresponding object they belong to exists Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 22

Declaration of Variables, Constants: Scope Scope of a variable is the part of the program where this variable can be accessed Scope is defined for local variables: the block { in which the variable has been declared; accessible from the point of declaration until the end of the block global variables (class variables), constants: accessible throughout the entire class/in the whole class parameter (special form of local variable): in the whole body of a function/ procedure public class Scope { static int a; // global variable, can be used in the whole class/program static int b; // same as before public static void main (String[] arg) { //parameter 'arg' can be used in 'main' int c; // local variables 'c','d'; defined only within 'main' (from this int d // position until the closing '' of main) if (c == d) { int e = 3; // variable 'e' might be used here // 'e' is no longer valid here Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 23

I Declaration of Variables, Constants: Shadowing What exactly is shadowing in Java? nothing but redeclaring a variable that has been already declared somewhere else; the effect of shadowing is to hide the previously declared variable in such a way that it may look as though you are using the hidden variable, but you are actually using the shadowing variable Most common is to "Hide an instance variable by shadowing it with a local variable" shadowing does not limit the lifetime of a variable, i.e., a global variable might be temporarily not visible/accessible at a certain program position, but still exists after leaving, e.g., this block, the local variable has disappeared, the global variable is visible again shadowing might limit the visibility of a (global) variable Shadowing happens most of the times by accident causes hard-to-find bugs Local variables and function parameters must not be shadowed Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 24

Declaration of Variables, Constants: Shadowing - Examples import java.lang.math.*; public class ShadowingMotivation { static double x = 3.0; static double y; global variables 'x', 'y' public static void main(string[] args) { y = Math.powerOfTwo(x); System.out.println(y); public class Math { private static double poweroftwo(double y) { double x; x = y * y; local variable 'x', (local) parameter 'y' return x; // poweroftwo declared private: code invisible outside! Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 25

Declaration of Variables, Constants: Shadowing - Examples public class NotShadowing { public static void main (String[]arg){ int i = 0; int sum = 0; while (i > 0) { int i = 4; this declaration is not allowed (even if code is never reached ) local variable must not be shadowed by another local var. for (int i = 1; i <= 100; i++) { for (int i = 20 ; i!= 10; i--) { sum = sum + i; // 'i' is undefined here (i.e., outside the 'for'-block) System.out.println("sum: " + sum); second declaration of 'i' is not allowed already a 'i' variable in this scope (=block) for (int i = 1; i <= 70; i++) {... for (int i = 52 ; i!= 10; i--) {... this is finally allowed (but is not shadowing!) 2 blocks with each local declarations of 'i' Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 26

Declaration of Variables, Constants: Shadowing - Examples public class ConstantShadowing { static int a = 10; // global variable static final int b = 100; // global constant public static void main (String[] arg) { int a = 5; // local variable (shadowing global 'a') int b = 50; // local variable (shadowing global constant 'b') System.out.println(a); System.out.println(b); declaration allowed; a global constant 'b' might be redeclared as local variable 'b' Program output: 5 50 Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 27

The Big Picture: Visibility, Scope, Shadowing public class LifeTimeScope { static int a; a = 10; global variable public static void main (String[]arg) { int a = 5; int b; Person p = new Person(); local variables // a redeclared a, c not defined here if (a < 10) { int c = a; int a = 3; b = poorcalc(a); System.out.print(b); local a (=5) // global a shadowed! new local variable c, value=5 not allowed (local-local shadowing) call uses local a (=5) p = null; p.name = "Franz"; public static int poorcalc (int a) { int b; b = 2 * a; return b; local variables/parameter (independent block) Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 28

The Big Picture: Visibility, Scope, Shadowing public class LifeTimeScope { static int a; a = 10; public static void main (String[]arg) { int a = 5; int b; Person p = new Person(); scope a (global) if (a < 10) { int c = a; int a = 3; scope c scope b scope a (local) b = poorcalc(a); System.out.print(b) p = null; p.name = "Franz"; public static int poorcalc (int a) { int b; b = 2 * a; return b; scope b scope a (local) Software Development I // Exercises // 05 Conversions and Promotions; Variables and Constant Declarations // 29

Exercises Software Development I 05 Conversions and Promotions; Lifetime, Scope, Shadowing November 5th, 2014 Software Development I Winter term 2014/2015 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute for Pervasive Computing Johannes Kepler University Linz riener@pervasive.jku.at