Fundamental Programming Structures in Java
Fundamental Programming Structures in Java
Storage
Type Requirement Range
approximately
double 8 bytes ±1.79769313486231570E+308 (15
significant decimal digits)
The Character Type
Single quotes are used to denote char constants.
The char type denotes characters in the
Unicode encoding scheme.
Special characters
Escape Sequence Name Unicode Value
\b backspace \u0008
\t tab \u0009
\n linefeed \u000a
\r carriage return \u000d
\" double quote \u0022
\' single quote \u0027
\\ backslash \u005c
Ternary operator, ?:
condition ? e1 : e2
Evaluates to e1 if the condition is true, to e2 otherwise.
Bitwise Operators
& ("and")
| ("or")
^ ("xor")
~ ("not")
>> right shift
<< left shift
Operator Purpose
+ addition of numbers, concatenation of Strings
+= add and assign numbers, concatenate and
assign Strings
- subtraction
-= subtract and assign
* multiplication
*= multiply and assign
/ division
/= divide and assign
% take remainder
%= take remainder and assign
++ increment by one
-- decrement by one
Operator Purpose
> greater than
>= greater than or equal to
< less than
<= less than or equal to
! boolean NOT
!= not equal to
&& boolean AND
|| boolean OR
== boolean equals
= assignment
~ bitwise NOT
?: conditional
Operator Purpose
| bitwise OR
|= bitwise OR and assign
^ bitwise XOR
^= bitwise XOR and assign
& bitwise AND
&= bitwise AND and assign
>> shift bits right with sign extension
>>= shift bits right with sign extension
and assign
<< shift bits left
<<= shift bits left and assign
>>> unsigned bit shift right
>>>= unsigned bit shift right and assign
Strings
Strings are sequences of characters, such as "Hello".
Java does not have a built-in string type. Instead, the
standard Java library contains a predefined class called,
String.
String e = ""; // an empty string
String greeting = "Hello";
Concatenation
String expletive = "Expletive";
String PG13 = "deleted";
String message = expletive + PG13;
Substrings
String greeting = "Hello";
String s = greeting.substring(0, 4);
Control Flow
Java, like any programming language, supports both
conditional statements and loops to determine control flow.
The Java control flow constructs are identical to those in
C and C++, with some exceptions. There is no goto, but
there is a “labeled” version of break that you can use to
break out of a nested loop
Conditional Statements
if
if- else
while
do… while
for
switch
Big Numbers
If the precision of the basic integer and
floating-point types is not sufficient, you can turn to a
couple of handy classes in the java.math package, called
BigInteger and BigDecimal.
These are classes for manipulating numbers with an
arbitrarily long sequence of digits.
Use the static valueOf method to turn an ordinary
number into a big number:
BigInteger a = BigInteger.valueOf(100);
You cannot use the familiar mathematical operators such
as + and * to combine big numbers. Instead, you must use
methods such as add and multiply in the big number classes.
BigInteger c = a.add(b); // c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));
// d = c * (b + 2)
Arrays
An array is a data structure that stores a collection of
values of the same type.
You access each individual value through an
integer index.
Declaration of an array a of integers:
int[] a; or int a[];
However, this statement only declares the variable a.
It does not yet initialize a with an actual array. You use
the new operator to create the array.
int[] a = new int[100];
Java has a shorthand to create an array object and supply
initial values at the same time.
int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
Sorting an Array
If you want to sort an array of numbers, you can
use one of the sort methods in the Arrays class:
int[] a = new int[10000];
...
Arrays.sort(a)
This method uses a tuned version of the QuickSort
algorithm that is claimed to be very efficient on
most data sets.
Multidimensional Arrays
Multidimensional arrays use more than one index
to access array elements. They are used for tables and other
more complex arrangements.
Declaring a matrix in Java is simple enough.
For example:
double[][] balance;
The initialization as follows:
balance = new double[NYEARS][NRATES];
A shorthand notion for initializing multidimensional arrays
without needing a call to new.
For example;
int[][] magicSquare =
{
{16, 3, 2, 13},
{5, 10, 11, 8},
{9, 6, 7, 12},
{4, 15, 14, 1}
};