Sunday, 22 April 2018

Java - Variables and Types

2.1  Variables

Computer programs manipulate (or process) data. A variable is used to store a piece of data for processing. It is called variable because you can change the value stored.
More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value.
  • A variable has a name (aka identifier), e.g., radiusareaage, and height. The name is needed to uniquely identify each variable, so as to assign a value to the variable (e.g., radius=1.2), as well as to retrieve the value stored (e.g., radius*radius*3.1419265).
  • A variable has a type. Examples of Java type are:
    • int: meant for integers (whole numbers) such as 123 and -456.
    • double: meant for floating-point (real numbers), such as 3.1416-55.661.2e3-4.5E-6, having a optional decimal point and fractional part.
    • String: meant for texts such as "Hello""Good Morning!". Strings are enclosed within a pair of double quotes.
    • char: meant for a single character, such as 'a''8'. A char is enclosed by a pair of single quotes.
  • A variable can store a value of that particular data type. It is important to take note that a variable in most programming languages is associated with a type, and can only store value of the particular type. For example, a int variable can store an integer value such as 123, but NOT real number such as 12.34, nor string such as "Hello".
  • The concept of type was introduced in the early programming languages to simplify interpretation of data made up of binary numbers (0's and 1's). The type determines the size and layout of the data, the range of its values, and the set of operations that can be applied.
The following diagram illustrates three types of variables: intdouble and String. An int variable stores an integer (or whole number or fixed-point number); a double variable stores a floating-point number or real number; a String variable stores texts.
variable

2.2  Identifiers (or Names)

An identifier is needed to name a variable (or any other entity such as a method or a class). Java imposes the following rules on identifiers:
  • An identifier is a sequence of characters, of any length, comprising uppercase and lowercase letters (a-z, A-Z), digits (0-9), underscore "_", and dollar sign "$".
  • White space (blank, tab, newline) and other special characters (such as +-*/@&, commas, etc.) are not allowed. Take note that blank and dash (-) are not allowed, i.e., "max value" and "max-value" are not valid names. (This is because blank creates two tokens and dash crashes with minus sign!)
  • An identifier must begin with a letter (a-z, A-Z) or underscore (_). It cannot begin with a digit (0-9) (because that could confuse with a number). Identifiers begin with dollar sign ($) are reserved for system-generated entities.
  • An identifier cannot be a reserved keyword or a reserved literal (e.g., classintdoubleifelsefortruefalsenull).
  • Identifiers are case-sensitive. A rose is NOT a Rose, and is NOT a ROSE.
Caution: Programmers don't use blank character in names. It is either not supported (e.g., in Java and C/C++), or will pose you many more challenges.
Variable Naming Convention
A variable name is a noun, or a noun phrase made up of several words with no spaces between words. The first word is in lowercase, while the remaining words are initial-capitalized. For example, thefontSizeroomNumberxMaxyMinxTopLeft and thisIsAVeryLongVariableName. This convention is also known as camel-case.
Recommendations
  1. It is important to choose a name that is self-descriptive and closely reflects the meaning of the variable, e.g., numberOfStudents or numStudents, but not n or x, to store the number of students. It is okay to use long names!
  2. Do not use meaningless names like abcdijki1j99.
  3. Avoid single-letter names like ijkabc, which is easier to type but often meaningless. Exception are common names like xyz for coordinates, i for index. Long names are harder to type, but self-document your program. (I suggest you spend sometimes practicing your typing.)
  4. Use singular and plural nouns prudently to differentiate between singular and plural variables.  For example, you may use the variable row to refer to a single row number and the variable rows to refer to many rows (such as an array of rows - to be discussed later).

2.3  Variable Declaration

To use a variable in your program, you need to first "introduce" it by declaring its name and type, in one of the following syntaxes. The act of declaring a variable allocates a storage (of size capable of holding a value of the type).
SyntaxExample
// Declare a variable of a specified type
type identifier;
// Declare multiple variables of the same type, separated by commas
type identifier1, identifier2, ..., identifierN;
// Declare a variable and assign an initial value
type identifier = initialValue;
// Declare multiple variables with initial values
type identifier1 = initValue1, ..., identifierN = initValueN;
 
int option;
 
double sum, difference, product, quotient;
 
int magicNumber = 88;
 
String greetingMsg = "Hi!", quitMsg = "Bye!";
Take note that:
  • Java is a "strongly type" language. A variable is declared with a type. Once the type of a variable is declared, it can only store a value belonging to this particular type. For example, an int variable can hold only integer such as 123, and NOT floating-point number such as -2.17 or text string such as "Hello".
  • Each variable can only be declared once.
  • You can declare a variable anywhere inside the program, as long as it is declared before used.
  • The type of a variable cannot be changed inside the program, after it is declared.
  • A variable declaration statement begins with a type, and works for only that type. In other words, you cannot declare variables of two different type in a single declaration statement.

2.4  Constants (final Variables)

Constants are non-modifiable variables, declared with keyword final. Their values cannot be changed during program execution. Also, constants must be initialized during declaration. For examples:
final double PI = 3.1415926;  // Need to initialize
Constant Naming Convention: Use uppercase words, joined with underscore. For example, MIN_VALUEMAX_SIZE.

2.5  Expressions

An expression is a combination of operators (such as addition '+', subtraction '-', multiplication '*', division '/') and operands (variables or literals), that can be evaluated to yield a single value of a certain type. For example,
1 + 2 * 3           // evaluated to int 7
 
int sum, number;
sum + number        // evaluated to an int value
 
double principal, interestRate;
principal * (1 + interestRate)  // Evaluated to a double value

2.6  Assignment

An assignment statement:
  1. assigns a literal value (of the RHS) to a variable (of the LHS), e.g., x = 1; or
  2. evaluates an expression (of the RHS) and assign the resultant value to a variable (of the LHS), e.g., x = (y + z) / 2.
The syntax for assignment statement is:
SyntaxExample
// Assign the literal value (of the RHS) to the variable (of the LHS)
variable = literalValue;
// Evaluate the expression (RHS) and assign the result to the variable (LHS)
variable = expression;
 
number = 88;
 
sum = sum + number;
The assignment statement should be interpreted this way: The expression on the right-hand-side (RHS) is first evaluated to produce a resultant value (called r-value or right-value). The r-value is then assigned to the variable on the left-hand-side (LHS) or l-value. Take note that you have to first evaluate the RHS, before assigning the resultant value to the LHS. For examples,
number = 8;           // Assign literal value of 8 to the variable number
number = number + 1;  // Evaluate the expression of number + 1,
                      //  and assign the resultant value back to the variable number
8 = number;           // INVALID
number + 1 = sum;     // INVALID
In Java, the equal symbol '=' is known as the assignment operator. The meaning of '=' in programming is different from Mathematics. It denotes assignment of the LHS value to the RHS variable, instead of equality of the RHS and LHS. The RHS shall be a literal value or an expression that evaluates to a value; while the LHS must be a variable.
Note that x = x + 1 is valid (and often used) in programming. It evaluates x + 1 and assign the resultant value to the variable xx = x + 1 illegal in Mathematics.
While x + y = 1 is allowed in Mathematics, it is invalid in programming (because the LHS of an assignment statement must be a variable).
Some programming languages use symbol ":=", "->" or "<-" as the assignment operator to avoid confusion with equality.

2.7  Primitive Types

In Java, there are two broad categories of typesprimitive types (e.g., intdouble) and reference types (e.g., objects and arrays). We shall describe the primitive types here and the reference types (classes and objects) in the later chapters on "Object-Oriented Programming".
TYPEDESCRIPTION
byteInteger8-bit signed integer
The range is [-2^7, 2^7-1] = [-128, 127]
short16-bit signed integer
The range is [-2^15, 2^15-1] = [-32768, 32767]
int32-bit signed integer
The range is [-2^31, 2^31-1] = [-2147483648, 2147483647] (≈9 digits)
long64-bit signed integer
The range is [-2^63, 2^63-1] = [-9223372036854775808, +9223372036854775807] (≈19 digits)
floatFloating-Point
Number
32-bit single precision floating-point number
(6-7 significant decimal digits, in the range of ±[≈10^-45, ≈10^38])
double64-bit double precision floating-point number
(14-15 significant decimal digits, in the range of ±[≈10^-324, ≈10^308])
charCharacter
Represented in 16-bit Unicode '\u0000' to '\uFFFF'.
Can be treated as 16-bit unsigned integers in the range of [0, 65535] in arithmetic operations.
booleanBinary
Takes a value of either true or false.
The size of boolean is not defined in the Java specification, but requires at least one bit.
primitive types
Built-in Primitive Types
Primitive type are built-in to the languages. Java has eight primitive types, as listed in the above table:
  • There are four integer types: 8-bit byte, 16-bit short, 32-bit int and 64-bit long. They are signed integers in 2's complement representation, and can hold an integer value of the various ranges as shown in the table.
  • There are two floating-point types: 32-bit single-precision float and 64-bit double-precision double, represented as specified by IEEE 754 standard. A float can represent a number between ±1.40239846×10^-45 and ±3.40282347×10^38, approximated. A double can represented a number between ±4.94065645841246544×10^-324 and ±1.79769313486231570×10^308, approximated. Take note that not all real numbers can be represented by float and double. This is because there are infinite real numbers even in a small range of say [1.1, 2.2], but there is a finite number of patterns in a n-bit representation. Most of the floating-point values are approximated to their nearest representation.
  • The type char represents a single character, such as '0''A''a'. In Java, char is represented using 16-bit Unicode (in UCS-2 format) to support internationalization (i18n). A char can be treated as a 16-bit unsigned integer (in the range of [0, 65535]) in arithmetic operations. For example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or 41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal).
  • Java introduces a new binary type called "boolean", which takes a value of either true or false.
Example: The following program can be used to print the maximumminimum and bit-length of the primitive types. The maximum, minimum and bit-size of int are kept in constants INTERER.MIN_VALUEINTEGER.MAX_VALUEINTEGER.SIZE.
/* 
 * Print the minimum, maximum and bit-length for primitive types
 */
public class PrimitiveTypesMinMax {
   public static void main(String[] args) {
      // int (32-bit signed integer)
      System.out.println("int(min) = " + Integer.MIN_VALUE);
      System.out.println("int(max) = " + Integer.MAX_VALUE);
      System.out.println("int(bit-length) = " + Integer.SIZE);
      // byte (8-bit signed integer)
      System.out.println("byte(min) = " + Byte.MIN_VALUE);
      System.out.println("byte(max) = " + Byte.MAX_VALUE);
      System.out.println("byte(bit-length)=" + Byte.SIZE); 
      // short (16-bit signed integer)
      System.out.println("short(min) = " + Short.MIN_VALUE);
      System.out.println("short(max) = " + Short.MAX_VALUE);
      System.out.println("short(bit-length) = " + Short.SIZE);
      // long (64-bit signed integer)
      System.out.println("long(min) = " + Long.MIN_VALUE); 
      System.out.println("long(max) = " + Long.MAX_VALUE);
      System.out.println("long(bit-length) = " + Long.SIZE);
      // char (16-bit character or 16-bit unsigned integer)
      System.out.println("char(min) = " + (int)Character.MIN_VALUE);
      System.out.println("char(max) = " + (int)Character.MAX_VALUE);
      System.out.println("char(bit-length) = " + Character.SIZE);
      // float (32-bit floating-point)
      System.out.println("float(min) = " + Float.MIN_VALUE);
      System.out.println("float(max) = " + Float.MAX_VALUE);
      System.out.println("float(bit-length) = " + Float.SIZE);
      // double (64-bit floating-point)
      System.out.println("double(min) = " + Double.MIN_VALUE);
      System.out.println("double(max) = " + Double.MAX_VALUE);
      System.out.println("double(bit-length) = " + Double.SIZE);
   }
}
The expected outputs are:
int(min) = -2147483648
int(max) = 2147483647
int(bit-length) = 32
byte(min) = -128
byte(max) = 127
byte(bit-length)=8
short(min) = -32768
short(max) = 32767
short(bit-length) = 16
long(min) = -9223372036854775808
long(max) = 9223372036854775807
long(bit-length) = 64
char(min) = 0
char(max) = 65535
char(bit-length) = 16
float(min) = 1.4E-45
float(max) = 3.4028235E38
float(bit-length) = 32
double(min) = 4.9E-324
double(max) = 1.7976931348623157E308
double(bit-length) = 64
String
Another commonly-used type is String, which represents texts (a sequence of characters) such as "Hello, world"String is not a primitive type, and will be further elaborated later. In Java, a char is enclosed by single quotes (e.g., 'A''0'), while a String is enclosed by double quotes (e.g., "Hello"). For example,
String message = "Hello, world!"; // strings are enclosed in double-quotes
char gender = 'm';                // char is enclosed in single-quotes
Choice of Data Types for Variables
As a programmer, you need to choose variables and decide on the type of the variables to be used in your programs. Most of the times, the decision is intuitive. For example, use an integer type for counting and whole number; a floating-point type for number with fractional part, String for text message, char for a single character, and boolean for binary outcomes.
Rules of Thumb
  • Use int for integer and double for floating point numbers. Use byteshortlong and float only if you have a good reason to choose that specific precision.
  • Use int for counting and indexing, NOT floating-point type (float or double). This is because integer type are precise and more efficient in operations.
  • Use an integer type if possible. Use a floating-point type only if the number contains a fractional part.
Data Representation
Read "Data Representation" if you wish to understand how the numbers and characters are represented inside the computer memory. In brief, It is important to take note that char '1' is different from int 1byte 1short 1float 1.0double 1.0, and String "1". They are represented differently in the computer memory, with different precision and interpretation. For example, byte 1 is "00000001"short 1is "00000000 00000001"int 1 is "00000000 00000000 00000000 00000001"long 1 is "00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001"float 1.0 is "0 01111111 0000000 00000000 00000000"double 1.0 is "0 01111111111 0000 00000000 00000000 00000000 00000000 00000000 00000000"char '1' is "00000000 00110001", and String "1"is a complex object.
There is a subtle difference between int 0 and double 0.0.
Furthermore, you MUST know the type of a value before you can interpret a value. For example, this value "00000000 00000000 00000000 00000001" cannot be interpreted unless you know its type (or its representation).
Example (Variable Names and Types): Paul has bought a new notebook of "abc" brand, with a processor speed of 3.2GHz, 4 GB of RAM, 500GB hard disk, with a 15-inch monitor, for $1650.45. He has chosen service plan 'B' among plans 'A', 'B' and 'C', plus on-site servicing. Identify the data types and name the variables.
The possible variable names and types are:
String name = "Paul";
String brand = "abc";
double processorSpeedInGHz = 3.2;  // or float
double ramSizeInGB = 4;      // or float
int harddiskSizeInGB = 500;  // or short
int monitorInInch = 15;      // or byte
double price = 1650.45;
char servicePlan = 'B';
boolean onSiteService = true;
Exercise (Variable Names and Types): You are asked to develop a software for a college. The system shall maintain information about students. This includes name, address, phone number, gender, date of birth, height, weight, degree pursued (e.g., B.Sc., B.A.), year of study, average GPA, with/without tuition grant, is/is not a scholar. Each student is assigned a unique 8-digit number as id. Identify the variables. Assign a suitable name to each variable and choose an appropriate type. Write the variable declaration statements.

2.8  Literals for Primitive Types and String

literal, or literal constant, is a specific constant value, such as 123-4563.14-1.2e3'a'"Hello", that is used in the program source. It can be assigned directly to a variable; or used as part of an expression. They are called literals because they literally and explicitly identify their values. We call it literal to distinguish it from a variable.
Integer (intlongshortbyte) literals
A whole number, such as 123 and -456, is treated as an int by default. In Java, the range of 32-bit int literals is -2,147,483,628 (-2^31) to 2,147,483,627 (2^31-1). For example,
int number = -123;
int sum = 1234567890;     // This value is within the range of int
int bigSum = 8234567890;  // ERROR: this value is outside the range of int
An int literal may precede with a plus (+) or minus (-) sign, followed by digits. No commas or special symbols (e.g., $ or space) is allowed (e.g., 1,234 and $123 are invalid). No preceding 0 is allowed too (e.g., 007 is invalid).
You can use a prefix '0' (zero) to denote a value in octal, and prefix '0x' (or '0X') for a value in hexadecimal, e.g.,
int number = 1234;   // Decimal
int number = 01234;  // Octal 1234, Decimal 2322
int number = 0x1abc; // hexadecimal 1ABC, decimal 15274
(JDK 1.7) From JDK 7, you can use prefix '0b' or '0B' to specify a value in binary. You are also permitted to use underscore (_) to break the digits into groups to improve the readability. But you must start and end the literal with a digit. For example,
int number1 = 0b01010000101000101101000010100010;
int number2 = 0b0101_0000_1010_0010_1101_0000_1010_0010;  // break the digits with underscore (JDK 1.7)
int number3 = 2_123_456;  // break the digits with underscore (JDK 1.7)
long literal above the int range requires a suffix 'L' or 'l' (avoid lowercase, which confuses with the number one), e.g., 123456789012L-9876543210l. In Java, the range of 64-bit long literals is -9,223,372,036,854,775,808L (-2^63) to 9,223,372,036,854,775,807L (2^63-1). For example,
long bigNumber = 1234567890123L;  // Suffix 'L' needed
long sum = 123;                   // int 123 auto-casts to long 123L
No suffix is needed for byte and short literals. But you can only use integer values in the permitted range. For example,
byte smallNumber = 12345;     // ERROR: this value is outside the range of byte.
byte smallNumber = 123;       // This is within the range of byte
short midSizeNumber = -12345;
Floating-point (doublefloat) literals
A number with a decimal point, such as 55.66 and -33.44, is treated as a double by default. You can also express them in scientific notation, e.g., 1.2e3-5.5E-6, where e or E denotes the exponent in base of 10. You could precede the fractional part or exponent with a plus (+) or minus (-) sign. Exponent values are restricted to integer. There should be no space or other characters in the number.
You can optionally use suffix 'd' or 'D' to denote double literals.
You MUST use a suffix of 'f' or 'F' for float literals, e.g., -1.2345F. For example,
float average = 55.66;      // Error! RHS is a double. Need suffix 'f' for float.
float average = 55.66f;
Character (char) Literals and Escape Sequences
A printable char literal is written by enclosing the character with a pair of single quotes, e.g., 'z''$', and '9'. In Java, characters are represented using 16-bit Unicode, and can be treated as a 16-bit unsigned integers in arithmetic operations. In other words, char and 16-bit unsigned integer are interchangeable. You can also assign an integer in the range of [0, 65535] to a char variable.
For example,
char letter = 'a';                 // Same as 97
char anotherLetter = 98;           // Same as the letter 'b'
System.out.println(letter);        // 'a' printed
System.out.println(anotherLetter); // 'b' printed instead of the number
anotherLetter += 2;                // 100 or 'd'
System.out.println(anotherLetter); // 'd' printed
Non-printable and control characters can be represented by a so-called escape sequence, which begins with a back-slash (\) followed by a pattern. The commonly-used escape sequences are:
Escape SequenceDescriptionUnicode in Hex (Decimal)
\nNewline (or Line-feed)000AH (10D)
\rCarriage-return000DH (13D)
\tTab0009H (9D)
\"Double-quote0022H (34D)
\'Single-quote0027H (39D)
\\Back-slash005CH (92D)
\uhhhhUnicode number hhhh (in hex),
e.g., \u000a is newline, \u60a8 is 您, \u597d is 好
hhhhH
Notes:
  • Newline (000AH) and Carriage-Return (000DH), represented by the escape sequence \n, and \r respectively, are used as line delimiter (or end-of-line, or EOL). Take note that Unixes and Mac use \n (0AH) as EOL, while Windows use \r\n (0D0AH).
  • Horizontal Tab (0009H) is represented as \t.
  • To resolve ambiguity, characters back-slash (\), single-quote (') and double-quote (") are represented using escape sequences \\\' and \", respectively. E.g.,
    • To represent a back-slash char, you need to write '\\', instead of '\', as a back-slash begins an escape sequence. Similarly, to include a back-slash in a double-quoted string, you need to write \\.
    • To represent a single-quote char, you need to write '\'' to distinguish it from the closing single-quote. But you can write double-quote directly, i.e., '"'.
    • To place a double-quote in a double-quoted string, you need to use \" to distinguish it from the closing double-quote, e.g., "\"hello\"". But you can write single-quote directly, e,g, "'hello'".
  • Other less commonly-used escape sequences are: \a for alert or bell, \b for backspace, \f for form-feed, \v for vertical tab. These may not be supported in some consoles.
String Literals
String literal is composed of zero of more characters surrounded by a pair of double quotes, e.g., "Hello, world!""The sum is: ". For example,
String directionMsg = "Turn Right";
String greetingMsg = "Hello";
String statusMsg = "";   // an empty string
String literals may contains escape sequences. Inside a String, you need to use \" for double-quote to distinguish it from the ending double-quote, e.g. "\"quoted\"". Single-quote inside a String does not require escape sequence. For example,
System.out.println("Use \\\" to place\n a \" within\ta\tstring");
Use \" to place
 a " within a string
Exercise: Write a program to print the following animal picture using multiple System.out.println(). Take note that you need to use escape sequences to print special characters.
          '__'
          (oo)
  +========\/
 / || %%% ||
*  ||-----||
   ""     ""
boolean Literals
There are only two boolean literals, i.e., true and false. For example,
boolean done = true;
boolean gameOver = false;
Example on Literals
/*
 * Test literals for various primitive types
 */
public class LiteralTest {
   public static void main(String[] args) {
      String name = "Tan Ah Teck"; // String is double-quoted
      char gender = 'm';           // char is single-quoted
      boolean isMarried = true;    // boolean of either true or false
      byte numChildren = 8;        // Range of byte is [-127, 128]
      short yearOfBirth = 1945;    // Range of short is [-32767, 32768]. Beyond byte
      int salary = 88000;          // Beyond the ranges of byte and short
      long netAsset = 8234567890L; // Need suffix 'L' for long. Beyond int
      double weight = 88.88;       // With fractional part
      float gpa = 3.88f;           // Need suffix 'f' for float
   
      // println() can be used to print value of any type
      System.out.println("Name is " + name);
      System.out.println("Gender is " + gender);
      System.out.println("Is married is " + isMarried);
      System.out.println("Number of children is " + numChildren);
      System.out.println("Year of birth is " + yearOfBirth);
      System.out.println("Salary is " + salary);
      System.out.println("Net Asset is " + netAsset);
      System.out.println("Weight is " + weight);
      System.out.println("GPA is " + gpa);
   }
}
The expected outputs are:
Name is Tan Ah Teck
Gender is m
Is married is true
Number of children is 8
Year of birth is 1945
Salary is 88000
Net Asset is 8234567890
Weight is 88.88
GPA is 3.88

No comments:

Post a Comment

Java - Operations

3.1  Arithmetic Operators Java supports the following arithmetic operators: Operator Description Usage Exampl...