Java Language
Naorem Karline Singh
Outline
Java Language Basics:
1. Syntax
2. Types
3. Variables
4. Arrays
5. Operators
6. Control flow
Arrays
An array is a group of liked-typed variables referred to by a common name,
with individual variables accessed by their index.
Arrays are:
1) declared
2) created
3) initialized
4) used
Also, arrays can have one or several dimensions.
Array Declaration
Array declaration involves:
1) declaring an array identifier
2) declaring the number of dimensions
3) declaring the data type of the array elements
Two styles of array declaration:
type array-variable[];
or
type[] array-variable;
Array Creation
After declaration, no array actually exists.
In order to create an array, we use the new operator:
type array-variable[];
array-variable = new type[size];
This creates a new array to hold size elements of type type, which
reference will be kept in the variable array-variable.
Array Initialization
int[] monthDays;
monthDays = new int[12];
monthDays = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
Array Initialization
Arrays can be initialized when they are declared:
int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
Comments:
1) there is no need to use the new operator
2) the array is created large enough to hold all specified elements
Array Indexing
Later we can refer to the elements of this array through their indexes:
array-variable[index]
The array index always starts with zero!
The Java run-time system makes sure that all array indexes are in the
correct range, otherwise raises a run-time error.
Example: Array Use
class Array {
public static void main(String args[]) {
int monthDays[];
monthDays = new int[12];
monthDays[0] = 31;
monthDays[1] = 28;
monthDays[2] = 31;
monthDays[3] = 30;
monthDays[4] = 31;
monthDays[5] = 30;
…
monthDays[12] = 31;
System.out.print(“April has ”);
System.out.println(monthDays[3] +“ days.”);
}
}
Example: Array Initialization
class Array {
public static void main(String args[]) {
int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print(“April has ”);
System.out.println(monthDays[3] +“ days.”);
}
}
Multidimensional Array
Multidimensional arrays are arrays of arrays:
1) declaration
int array[][];
2) creation
int array = new int[2][3];
3) initialization
int array[][] = { {1, 2, 3}, {4, 5, 6} };
Multidimensional Array
class Array {
public static void main(String args[]) {
int array[][] = { {1, 2, 3}, {4, 5, 6} };
int i, j, k = 0;
for(i=0; i<2; i++) {
for(j=0; j<3; j++)
System.out.print(array[i][j] + " ");
System.out.println();
}
}
}
Example: Arrays
1) Write a program that creates an array of 10 integers with the initial values of 3.
2) What's the index of the first and the last component of a one hundred component array?
3) What will happen if you try to compile and run the following code?
public class Q {
public static void main(String argv[]){
int var[]=new int[5];
System.out.println(var[0]);
}
}
Outline
Java Language Basics:
1. Syntax
2. Types
3. Variables
4. Arrays
5. Operators
6. Control flow
Operators Types
Java operators are used to build value expressions.
Java provides a rich set of operators:
1) assignment
2) arithmetic
3) relational
4) logical
5) bitwise
6) other
Operators and Operands
Each operator takes one, two or three operands:
1) a unary operator takes one operand
j++;
2) a binary operator takes two operands
i = j++;
3) a ternary operator requires three operands
i = (i>12) ? 1 : i++;
Assignment Operator
A binary operator:
variable = expression;
It assigns the value of the expression to the variable.
The types of the variable and expression must be compatible.
The value of the whole assignment expression is the value of the expression
on the right, so it is possible to chain assignment expressions as follows:
int x, y, z;
x = y = z = 2;
Arithmetic Operators
Java supports various arithmetic operators for:
1) integer numbers
2) floating-point numbers
There are two kinds of arithmetic operators:
1) basic: addition, subtraction, multiplication, division and modulo
2) shortcut: arithmetic assignment, increment and decrement
Table: Basic Arithmetic Operators
Arithmetic Assignment Operators
Instead of writing
variable = variable operator expression;
for any arithmetic binary operator, it is possible to write shortly
variable operator= expression;
Benefits of the assignment operators:
1) save some typing
2) are implemented more efficiently by the Java run-time system
Table: Arithmetic Assignments
Increment/Decrement Operators
Two unary operators:
1) ++ increments its operand by 1
2) -- decrements its operand by 1
The operand must be a numerical variable.
Each operation can appear in two versions:
• prefix version evaluates the value of the operand after performing the
increment/decrement operation
• postfix version evaluates the value of the operand before performing the
increment/decrement operation
Table: Increment/Decrement
Example: Increment/Decrement
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c, d;
c = ++b;
d = a++;
c++;
System.out.println(“a= “ + a);
System.out.println(“b= “ + b);
System.out.println(“c= “ + c);
}
}
Relational operators
Relational operators determine the relationship that one operand has to the
other operand, specifically equality and ordering.
The outcome is always a value of type boolean.
They are most often used in branching and loop control statements.
Table: Relational operators
Logical Operators
Logical operators act upon boolean operands only.
The outcome is always a value of type boolean.
In particular, “and” and “or” logical operators occur in two forms:
1) full op1 & op2 and op1 | op2 where both op1 and op2 are
evaluated
2) short-circuit - op1 && op2 and op1 || op2 where op2 is only
evaluated if the value of op1 is insufficient to determine the final
outcome
Logical Operators
Logical operators act upon boolean operands only.
The outcome is always a value of type boolean.
In particular, “and” and “or” logical operators occur in two forms:
1) full op1 & op2 and op1 | op2 where both op1 and op2 are
evaluated
2) short-circuit - op1 && op2 and op1 || op2 where op2 is only
evaluated if the value of op1 is insufficient to determine the final
outcome
Table: Logical Operators
Bitwise Operators
Bitwise operators apply to integer types only.
They act on individual bits of their operands.
There are three kinds of bitwise operators:
1. basic - bitwise AND, OR, NOT and XOR
2. shifts - left, right and right-zero-fill
3. assignments - bitwise assignment for all basic and shift operators
Bitwise Operators
Example: Bitwise Operators
class BitLogic {
public static void main(String args[]) {
String binary[] = { "0000","0001","0010", … };
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
System.out.print("a =" + binary[a]);
System.out.println("and b =" + binary[b]);
System.out.println("a|b = " + binary[c]);
System.out.println("a&b = " + binary[d]);
System.out.println("a^b = " + binary[e]);
}
}
Other Operators
Operator Description
?: shortcut if-else statement
instanceof determines if its first operand is an instance of the second
new creates a new object or a new array
(type) casts a value to the specified type
(params) delimits a comma-separated list of parameters
. used to form qualified names
used to declare arrays, create arrays, access array
[]
elements
Conditional Operators
General form:
expr1? expr2 : expr3
where:
1) expr1 is of type Boolean
2) expr2 and expr3 are of the same type
If expr1 is true, expr2 is evaluated, otherwise expr3 is evaluated.
Example: Conditional Operators
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i;
System.out.print("Abs value of “ + i + " is " + k);
i = -10;
k = i < 0 ? -i : i;
System.out.print("Abs value of “ + i + " is " + k);
}
}
Operator Precedence
Java operators are assigned precedence order.
Precedence determines that the expression
1 + 2 * 6 / 3 > 4 && 1 < 0
if equivalent to
(((1 + ((2 * 6) / 3)) > 4) && (1 < 0))
When operators have the same precedence, the earlier one binds stronger.
Table: Operator Precedence
Exercise: Operators
1) What operators do the code snippet below contain?
arrayOfInts[j] > arrayOfInts[j+1];
2) Consider the following code snippet:
int i = 10;
int n = i++%5;
a) What are the values of i and n after the code is executed?
b) What are the final values of i and n if instead of using the postfix increment operator
(i++), you use the prefix version (++i))?
3) What is the value of i after the following code snippet executes?
int i = 8;
i >>=2;
4) What’s the result of System.out.println(010|4); ?