Variables
Variables are convenient handles for accessing memory. We don't need to mess with memory addresses:
Declaring a variable requires a type name like double
and an identifier:
|
{type name} {variable name} ; |
We may assign values to variables or build expressions like pi * 2.0 * 2.0
:
double pi; // Variable declaration
pi = 3.1415926; // Assigning value to variable
// Print a circle's area of radius 2.0
System.out.println(pi * 2.0 * 2.0);
- Separate declaration and initialization
double pi; // Declaration of variable pi pi = 3.1415926; // Value assignment
- Combined declaration and initialization
double pi = 3.1415926;
int a;
int b = 22;
int c;
being equivalent to either of:
Compact | Multiple lines |
---|---|
|
|
abstract continue for new switch
assert default if package synchronized
boolean do goto private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
-
Start with a small letter like
africa
rather thanAfrica
. -
Use “camel case” e.g.
myFirstCode
. -
Do not start with
_
or$
.
Modifier final
prohibits
changes:
final double PI = 3.1415926;
...
PI = 1.0; // Compile time error: Constant cannot be modified
Note
final
variables by convention are
being capitalized
Variable names are case sensitive:
int count = 32; int Count = 44; System.out.println(count + ":" + Count);
Resulting output:
32:44
No. 6
Legal variable names
Q: |
Which of the following names are legal variable names? If so, would you actually use them in your code? Complete the following table and explain your decision with respect to the “Language Fundamentals” / “variables” section of [Kurniawan] .
TipYou may want to prepare a simple Java™ program testing the above names. |
||||||||||||||||||||||||||||||||||||||||||
A: |
Consider:
Unfortunately this message does not explain the underlying cause: It does not hint towards an illegal identifier name. One indication of a sound compiler implementation is its ability to provide meaningful, self explanatory error messages.
|
int i = 2;
int j = i; // o.K.: Assigning int to int
long l = i; // o.K.: Widening conversion
i = l; // Wrong: Narrowing
boolean b = true;
i = b; // Error: int and boolean are incompatible types
i = 4.3345 // Error: Cannot assign double to int
i = "Hello"; // Even worse: Assigning a String to an int
|
Performing static range check |
|
No static check on |
No. 7
Benefits of final
Q: |
We reconsider our code from Figure 80, “Compile time analysis ”:
Adding
Why does introducing a TipRead Static
program analysis. Why does |
||
A: |
Modifying variable
Thus the compiler is able to perform a compile time range check assuring the value of 120 to be within the interval [-128, 127]. |
|
i carrying long: 4345 i carrying double: 44 |
|
i carrying long:-296517632 i carrying double:2147483647 |
«C» programming language miracles:
|
Uups: Velocity=-302 |
|
The cause of the failure was a software error in the inertial reference system.
Specifically, a 64 bit floating point number relating to the horizontal velocity of the rocket with respect to the platform was converted to a 16 bit signed integer.
The number was larger than 32,767, the largest integer possible in a 16 bit signed integer, and thus the conversion failed.
Related video explanation
No. 8
«C» vs. Java.
Q: |
We reconsider the «C» code example from Figure 84, “Don't worry, be happy ... ”:
Rewrite the same code in Java. You will encounter a problem. Tip |
A: |
Naively rewriting the snippet in Java™ yields a compile time error:
A type cast is being required:
NoteThis allows for compilation by resolving the syntax error.
However the possible overflow problem i.e.
converting a |
No. 9
Assignment and type safety
Q: |
We consider:
This snippet shows correct Java™ code.
However Figure 79, “Type safety ” suggests we should
not be allowed to assign an Even worse: Swapping types yields a compile time error:
Questions to answer:
|
||||
A: |
Caveats using explicit casts:
|
Java™ provides meta information on types:
No. 10
Inventing tinyint
.
Q: |
Suppose Java™ was offering a hypothetic
signed integer type
|
||||||||||||||||||
A: |
|
No. 11
An int
's
minimum and maximum value
Q: |
In this exercise we look at an A Java™ Java™ implements negative values using Two's complement representation. We provide some values: Table 1. 4 Byte Two's
complement representation of
int
values.
Use
|
||||||||||
A: |
We insert Two's
complement representations of minimum and maximum int
values according to Table 1, “4 Byte Two's
complement representation of
BTW: The JDK™ does provide maximum
value, minimum value and related information for
|
|
44 Jim Different |
|
$a + $b = 5 $jim + $a = 2 |
//Bad!
double pi = 3.141592653589793;
...
pi = -4; // Woops, accidential and erroneous redefinition
//Good
final double PI = 3.141592653589793;
...
PI = -4; // Compile time error:
// Cannot assign a value to final variable 'pi'
GpsPosition start = new GpsPosition(48.7758, 9.1829);
String name = "Simon";
LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);