Variables

Variables are convenient handles for accessing memory. We don't need to mess with memory addresses:

Figure 104. Variables: Handles to memory Slide presentation Create comment in forum

Declaring a variable requires a type name like double and an identifier:

Figure 105. Variable declaration Slide presentation Create comment in forum
double pi;

We may assign values to variables or build expressions like pi * 2.0 * 2.0 :

Figure 106. Declare and use Slide presentation Create comment in forum
double pi;      // Variable seclaration
...
pi = 3.1415926; // Assigning value to variable

// Print a circle's area of radius 2.0
System.out.println(pi * 2.0 * 2.0);

Figure 107. Declaration and initialization Slide presentation Create comment in forum
Separate declaration and initialization
double pi; // Declaration of variable pi
...
pi = 3.1415926; // Value assignment
Combined declaration and initialization
double pi = 3.1415926;

Figure 108. Multiple variables of same type Slide presentation Create comment in forum
int a;
int b = 22;
int c;

is equivalent to either of:

Compact Multiple lines
int a, b = 22, c;
int a,
    b = 22,
    c;

Figure 109. Variable naming conventions Slide presentation Create comment in forum
  • Start with a small letter like africa rather than Africa.

  • Use camel case e.g. myFirstCode.
    Variable naming conventions
  • Do not start with _ or $.


Figure 110. Constants Slide presentation Create comment in forum
final double pi = 3.1415926;
...
pi = 1.0; // Compile time error: Constant cannot be modified

Figure 111. Variable names (identifier) Slide presentation Create comment in forum
Rules Legal Illegal
  • Start with a letter, _ or $

  • May be followed by letters, digits, _ or $

  • Must not match:

  • $test

  • _

  • count

  • blue

  • 2sad

  • switch

  • true


Figure 112. Java keywords. Slide presentation Create comment in forum
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

Figure 113. Case sensitivity Slide presentation Create comment in forum

Variable names are case sensitive:

int count = 32;
int Count = 44;
System.out.println(count + ":" + Count);

Resulting output:

32:44

exercise No. 13

Legal variable names Create comment in forum

Q:

Which of the following names are legal variable names? Complete the following table and explain your decision with respect to the Language Fundamentals / variables section of [Kurniawan] .

Identifier is legal? (yes / no) Explanation / remark
for
sum_of_data
sumOfData
first-name
ABC
42isThesolution
println
B4
AnnualSalary
"hello"
_average
ανδρος
$sum

Tip

You may want to prepare a simple Java program testing the above names.

A:

Consider:

public static void main(String[] args) {
   int 42isThesolution = 6; // Syntax error on token "42", delete this token
}

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.

Identifier is legal? (yes / no) Explanation / remark
for no for is a Java keyword.
sum_of_data yes -
sumOfData yes -
first-name no Operators like - or + must not appear in variable names.
ABC yes Best practices: Discouraged variable name, non-constant variables should start with lowercase letters.
42isThesolution no Identifiers must not start with a number.
println yes println is a method of System.out.println(...) but is no Java keyword.
B4 yes Best practices: Discouraged variable name, non-constant variables should start with lowercase letters.
AnnualSalary yes Best practices: Legal but discouraged variable name: Non-constant variables should start with lowercase letters.
"hello" no String delimiters must not be part of an identifier.
_average yes Best practices: Should be reserved for system code.
ανδρος yes Perfectly legal Greek Unicode characters.
$sum yes Best practices: Using the $ sign should be reserved for system code.
Figure 114. Define before use Slide presentation Create comment in forum
Correct:
double f;
f = -4.55;
Wrong
f = -4.55;
double f;

Figure 115. Type safety Slide presentation Create comment in forum
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

Figure 116. Compile time analysis Slide presentation Create comment in forum
byte b120 = 120; // o.K., static check
byte b128 = 128; // Wrong: Exceeding 127

Performing static range check

int a = 120;
byte b120 = a;

No static check on variable value


Figure 117. Forcing conversions Slide presentation Create comment in forum
long l = 4345;

int i = (int) l; // Casting long to int
System.out.println("i carrying long: " + i);

double d = 44.2323;
i = (int) d; // Casting double to int
System.out.println("i carrying double: " + i);
i carrying long: 4345
i carrying double: 44

Figure 118. Watch out! Slide presentation Create comment in forum
long l = 3000000000000000L;
int i = (int) l;
System.out.println("i carrying long:" + i);

double d = 44300000000000.0;
i = (int) d;
System.out.println("i carrying double:" + i);
i carrying long:-296517632
i carrying double:2147483647

Figure 119. Casting long to int Slide presentation Create comment in forum

Figure 120. C: Don't worry, be happy ... Slide presentation Create comment in forum
#include <stdio.h>

void main(void) {
  double measure = 65234.5435;
  short velocity;
  velocity = measure;
  printf("Velocity=%d\n", velocity);
}

Uups:

Velocity=-302

Figure 121. ... and watch the outcome Slide presentation Create comment in forum
... and watch the outcome
Development costs:

~$7 billion.

Rocket and cargo

~$500 million.

related video


Figure 122. From the report Slide presentation Create comment in forum

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.


exercise No. 14

Assignment and type safety Create comment in forum

Q:

We consider:

int i = -21;
double d = i;

This snippet shows perfectly correct Java code. However Figure 115, “Type safety ” suggests we should not be allowed to assign an int to a double value due to type safety.

Even worse: Swapping types yields a compile time error:

double d = 2.4;
int i = d; // Error: Incompatible types

Questions to answer:

  1. Explain on syntax level why is d = i is allowed but i = d causes an error? Hint: Read about widening and narrowing conventions.

  2. Whats the ratio behind this design decision? Hint: What would happen for larger double values?

A:

The first code snippet uses the widening conversion: When assigning d = i the Java compiler implicitly converts a four byte int into an eight byte double value.

Turning a double into an int is more cumbersome: The expression i = 3.5 could be evaluated by agreeing on a specific rounding prescription. But what about i = 3457357385783573478955345.45? A Java int's maximum value is 2 31 - 1 exceeding the former double value.

Conclusion: Java disallows double to int assignments unless using a so called cast (explicit type conversion):

Caveats using explicit casts:

  1. double d = 2.6;
    int i = (int) d; // Explicit cast double to int
    System.out.println("Result: " + i);
    Result: 2

    Notice the result of 2 rather than 3 as you might have expected assuming some common rounding convention. This does not happen unless you explicitly use methods like e.g. Math.round().

  2. double d = 3_000_000_000.;
    int i = (int) d; // Explicit cast double to int
    System.out.println("Result: " + i);
    Result: 2147483647

    This truncation of 3 billion to 2147483647 is due to the latter being the largest possible int value to je represented in Java.

Java provides meta information on types:

Figure 123. Maximum and minimum values Slide presentation Create comment in forum

exercise No. 15

Inventing tinyint. Create comment in forum

Q:

Suppose Java was offering a hypothetic signed integer type tinyint using 3-bit 2-complement representation.

  1. Write down all possible decimal values among with their corresponding binary representation.

  2. Perform the following additions on binary level:

    • 1 + 2

    • -4 + 2

    • -1 - 2

    • 3 + 1

    Hint by example: -4 + 3 is:

     1   100
     2  +011
    --  ----
     ?   111  

    What is 3-bit two complement 111 in decimal?

A:

  1. Given n=3 in three bit two complement notation as usual possible values range from - 2 ( 3 - 1 ) to 2 ( 3 - 1 ) - 1 :

    Decimal Two-complement
    3 011
    2 010
    1 001
    0 000
    -1 111
    -2 110
    -3 101
    -4 100

    Thus the example hint value 111 is indeed decimal -1 equalling -4 + 3 as expected.

  2. 1 + 2
     1   001
     2  +010
    --  ----
     3   011
    -4 + 2
    -4   100
     2  +010
    --  ----
    -2   110
    -1 - 2
    -1     111
    -2    +110
    --  ------
    -3    1101 (See comment below)

    In 3-bit two complement representation the leftmost bit at position 4 will be discarded. The actual result is thus 101 as expected.

    3 + 1
     3   011
     1  +001
    --  ----
    -4   100 Surprise! See comment below

    This is actually an overflow: In 3-bit two complement representation the highest representable decimal value is 3 being equal to 2 3 - 1 - 1 . Trying to add a value of 1 leads to disaster and in suitable circumstances may cause a US$370 spacecraft plummeting from the sky! Excerpt from the Ariane 5 flight 501 failure report:

    The internal SRI software exception was caused during execution of a data conversion from 64-bit floating point to 16-bit signed integer value. The floating point number which was converted had a value greater than what could be represented by a 16-bit signed integer.

exercise No. 16

An int's minimum and maximum value Create comment in forum

Q:

In this exercise we look at an int's the largest and smallest (negative) possible value.

A Java int is internally being represented by 4 bytes. 00000000_00000000_00000000_00000101 2 for example represents the decimal value 5.

Java implements negative values using Two's complement representation. We provide some values:

Table 1. 4 Byte Two's complement representation of int values.
Two complement representation Decimal representation
00000000_00000000_00000000_00000000 0
01111111_11111111_11111111_11111111 2 32 - 1 - 1 (Maximum)
10000000_00000000_00000000_00000000 - 2 32 - 1 (Minimum)
11111111_11111111_11111111_11111111 -1

Use int literals in binary representation like e.g. 0B1100 in the Language Fundamentals / Literals section of [Kurniawan] in order to write an int's minimum and maximum possible value to standard output.

public static void main(String[] args) {

  int minumum = ... , //TODO: provide values by
  maximum = ...;  // binary int literals

  System.out.println("Minimum:" + minumum);
  System.out.println("Maximum:" + maximum);
}

A:

We insert Two's complement representations of minimum and maximum int values according to Table 1, “4 Byte Two's complement representation of int values.”.

public static void main(String[] args) {

  int minumum = 0B10000000_00000000_00000000_00000000,
      maximum = 0B01111111_11111111_11111111_11111111;

  System.out.println("Minimum int value:" + minumum);
  System.out.println("Maximum int value:" + maximum);
}

BTW: The JDK does provide maximum value, minimum value and related information for char, byte, short and int primitive data types within their corresponding Character, Byte, Short and Integer classes. You may want to execute:

System.out.println("int minimum:" + Integer.MIN_VALUE);
System.out.println("int maximum:" + Integer.MAX_VALUE);

System.out.println("int bytes:" + Integer.BYTES);
System.out.println("int size:" + Integer.SIZE);
Figure 124. Dynamic typing in PERL Slide presentation Create comment in forum
$test = 44;  # Assigning an int
print $test, "\n";

$test = "Jim"; # Assigning a string
print $test, "\n";

$cmp = 43.55; # A float

if ($test == $cmp) { # comparing string against float
   print "Equal\n";
} else {
   print "Different\n";
}
44
Jim
Different

Figure 125. Using final Slide presentation Create comment in forum
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'
Even better:
final double PI = 3.141592653589793; // Coding style (Best practices):
                                     // Using capital letters for
                                     // constant variable.

Figure 126. Two categories of variables Slide presentation Create comment in forum
Primitive type
int a = -15;

Possible types: All eight primitive Java types.

Reference type (based on classes)
GpsPosition start = new GpsPosition(48.7758, 9.1829);

Possible types: Arbitrary built-in or user defined classes.


Figure 127. Reference type examples Slide presentation Create comment in forum
GpsPosition start = new GpsPosition(48.7758, 9.1829);
String name = "Simon";
LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);