Getting Started: Basics of Java

Components of Java Programs

Statements

Statements in Java are made up of:

Escape Sequences

String and character literals can contain special escape sequences that represent single characters that cannot be represented with a single character in code.
Escape
Sequence
Meaning
\n newline
\t tab
\b backspace
\r carriage return
\" double quote
\' single quote
\\ backslash

Basic Console Output

Basic console output in Java uses the built-in object System.out. Primary output methods: Example that illustrates print and println

In calls to print and println, multiple items can be printed in a single call through the use of the + operator, which represents string concatenation when at least one of its operands is a string. (If both operands are numeric items, the + operator is addition).

Example of string concatenation in output
Example of string concatenation vs. addition in output

Comments

Comments are used to improve the readability of code. Comments are ignored by the compiler. There are two styles of comments in Java:

Variables

Every variable has a:

Rules for names of identifiers

Built-in Data Types

Declaring Variables

Format:
  datatype variableName

Always starts with a type!

  int x;
  float y;
  char ch;
  bool answer;
  int x = 5;	        // can initialize variable on the declaration line
  char item = 'A';
  int a, b, c;           // can also list several variables in one declaration 
                         //  statement, separated by commas
  float a=3.4, b=5.1, c;  

A variable can be declared constant by using the keyword final - must initialize in the same statement

  final double PI = 3.14159;

Console Input

Input is a trickier matter than output in Java, because there is not an easy built-in object for handling console input. A class called MyInput is provided here, which contains three methods called readInt, readDouble, and readString for reading these types of data from the keyboard. For now, do not worry about how this class works. Simply use this class in programs that need to do basic console input.

The MyInput class

Arithmetic Operators

The basic operators are: + , - , * , / , %
These do arithmetic as expected (addition, subtraction, etc). Division is a special case. For integers, the / operator gives the quotient of a division, and the % operator, called the modulus operator, gives the remainder.

  int remainder = 19 % 5;    // gives the result 4
  int quotient = 19 / 5;     // gives the result 3
This example illustrates the arithmetic operations in Java

Order of Operations

Unary Operations are first, ie -1+3 evaluates the -1 first
Then  *, /, and % have equal precedence
After any of the above have been evaluated, addition and subtraction is done.
Associativity on arithmetic operators at the same level are as usual -- left to right.

Assignment Operator

Format:
  variable = expression
Note that the right side can be an expression (i.e. a computation involving values, variables, operators, etc), which can be evaluated down to a single value. The left side must be a storage location (i.e. a variable).
  // Valid assignment examples
  x = 5;
  y = 2.34;
  option = 'X';
  x = a*(3/y)+z;

  3 = x;	  // ILEEGAL.  (backwards)
  a + 2 = b;	  // ILLEGAL.  Not a variable on the left
The assignment operator's associativity is right to left:
  a = b = c = d = 5;
In the above statement, the operation d = 5 executes first, and the value of the assignment (5) is returned, to be used as the operand for the next assignment operator.

Shortcut Operators

  v += e;    means    v = v + e; 
  v -= e;    means    v = v - e; 
  v *= e;    means    v = v * e; 
  v /= e;    means    v = v / e; 
  v %= e;    means    v = v % e; 

Increment and Decrement

++x and x++ used to increment x (equivalent to x = x + 1)
--x and x-- used to decrement x (equivalent to x = x - 1)

Pre-decrement and post-decrement work in a similar fashion.

Note: This only matters if the variable is actually used in another expression. These two statements by themselves have the exact same effect:

  x++;	// increment x
  ++x;  // increment x
Examples of pre-increment and post-increment
  int a = 5, b = 3;
  int c = b * ++a;	// After this statement, c is 18, a is 6

  int x = 6, y = 4;
  int d = x - y++;	// After this statement, d is 2, y is 5

Type Conversions

When working with mixed types, conversions can take one of two forms:
  int i1 = 5, i2;
  short s = 3;
  double d1 = 23.5, d2;
  float f = 12.3;
  byte b = 10;

  d2 = i1;	  // automatically allowed
  i1 = b;	  // automatically allowed
  s = (short)i1;  // requires cast operation (some data may be lost)
  i2 = (int)d1;	  // requires cast operation (decimal data may be lost)

  d2 = f + d1;	  // automatically allowed
  i2 = b + s;	  // automatically allowed

More Code Examples

These are simple program examples from the textbook (chapter 2)