Comments

Comments are statements in source code that are ignored by the compiler.  They are included by programmers for various reasons:

  • Include author, date and email of the source code
  • Indicate what a block of code does
  • Specify future work

There are two types of comments: single line comments and multiple line comments, as shown below.

//  Eric McGregor

/*
    The following code does
    this and that ...
*/

Numeric and Character Encodings

A computer can only store and manipulate 0’s and 1’s .  All data that is passed through the system must be represented as 0’s and 1’s.

A bit can store a 0 or a 1.

Given n bits we can represent 2n different values.

A byte is a sequence of 8 bits.

A byte can hold 256 distinct values: from 00000000 to 11111111.

Representing Unsigned Decimal Values

  • Count to 20 in binary

Numeric Conversions

  • Binary – Decimal
  • Binary – Hexadecimal

Ascii Code

  • ASCII = American Standard Code for Information Interchange
  • Created by IANA
  • Characters in .txt file stored as single bytes.

Unicode

  • The most recent version of Unicode contains 136,755 different characters for 139 scripts.
  • Unicode uses up to 4 bytes per character.
  • The first 127 Unicode characters are the ASCII characters.

<print-me>

Variables

A variable is a container that holds some value.  In Java, before a variable can be used it must be defined with a variable declaration statement and an optional initialization assignment.  The variable declaration includes the type of data that can be stored in the variable and an identifier.

type identifier [= value];

Tab, newline, and space characters are considered whitespace characters.  When writing programs we place whitespace characters (at least one) between the tokens in a statement.  In most cases, multiple whitespace characters are allowed and ignored.

Java is a strongly typed programming language.  That means each variable declaration specifies a specific type which defines the values that can be stored in the variable.  A type can be either primitive or reference.

Identifiers

  • Can be made of upper and lowercase letters, numbers, underscore and dollar sign.
  • Can not begin with number
  • Can not be a Java keyword

Identifier Conventions

  • Variables and methods begin with lowercase letters
  • Classes begin with capital letters

A literal defines an explicit value.  We often initialize variables using literals.

int age = 1;
double grade = 98.6;
char initial = 'A';
String phrase = "Hello World!";
boolean foundToken = false;

Variables can also be initialized using other expressions, but we’ll get to them later.

int w = x;
int y = foo();
int z = w * x;

An assignment statement is terminated with a semi-colon.

Primitive Types

The Java primitive types are

  • byte, short, int, long
  • float, double
  • char
  • boolean

Integers (byte, short, int, and long)

  • long (64)  [-2^63, 2^63-1]
  • int (32) [-2^31, 2^31-1]
  • short (16) [-32,768, 32,767]
  • byte (8)  [-128, 127]
int age = 10;
long age = 100000000;
  • byte and shorts are promoted (actually stored as) ints

Floating-point (float, double)

  • double (64)
  • float (32)
double average = 3.04;
double count = 10.3E10;
  • double is often faster than float on modern computers
  • Many math functions return doubles

Characters (char)

  • char (16)
  • Represent Unicode symbols
char ch1, ch2;
ch1 = 88;                // ASCII decimal value for 'X'
ch2 = 'Y';

char ch3 = '\'';         // single quote character
char ch4 = '\n';         // new line character

Boolean (boolean)

  • boolean (size undefined)
  • Represents true/false
boolean flag = true;

Reference Types

A variable that has a reference type can store a reference (memory location) to where an instance of the reference type is stored.  For example we can store a sequence of characters in memory and have a reference variable hold the location to where the strings are stored.

String name = "Eric";
String directory = "\\root";
String title = "\"To Kill a Mockingbird\"";

Primitive Type Conversions

Sometimes you can assign a value of one type to a variable of another type.  If the types are compatible, Java does the conversion automatically.  For example,

int x = 10;
long y = x;

But there are not automatic conversions for all type combinations.  For example, there is no automatic conversion from double to byte or from character to boolean.

An automatic type conversion will take place if

  • The two types are compatible
  • The destination type is larger than the source type

Type Promotion

Some expressions automatically promote values to another type.  Here are the rules.

  • All byte, short and char values are promoted to int when evaluating an expression.
  • If one operand is a long, the entire expression is promoted to a long.
  • If one operand is a float, the entire expression is promoted to a float.
  • If one operand is a double, the entire expression is promoted to a double.
byte a;
. . .
a = a * 2;              // wrong - a is promoted to an int
a = (byte) (a * 2);     // ok

Casting

To perform a conversion between incompatible types or from a larger type to a smaller type you must cast one to the other using the following form: (target_type) value.  This is called narrowing.

int a;
byte b;
. . .
b = (byte) a;

When a floating-point number is cast to an integer, the fractional part is lost.  This is called truncation.

int a;
double b;
. . .
a = (int) b;