Lec2 - Basics and Overview
Lec2 - Basics and Overview
Identifiers
Variables
Operators
Statements’ type
08/27/2022 3
Identifiers
• Identifiers are used for class names, method names, and variable names.
• An identifier may be any sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters.
• Identifiers must not begin with a number.
• Java Identifiers are case-sensitive.
• Some valid identifiers are ATEST, count, i1, $Atest, and this_is_a_test
• Some invalid identifiers are 2count, h-l, and a/b
Operators
08/27/2022 5
Operators
Java operators can be grouped into the following four groups:
Arithmetic,
Bitwise,
Relational, and
Logical.
08/27/2022 6
Arithmetic Operators
Th
e
Operator Result nu ope
m
bo eric rand
+ Addition o le s
an type. of th
bu ann me
* Multiplication ty
ou ot us tic op
/ Division c an e a er a
use rithm tors
% Modulus the etic mu
m s
on oper t be o
++ Increment ch a t o
r fa
ar
+= Addition assignment typ s on
es
.
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
08/27/2022 7
Bitwise Operators
Jav
Operator Result lon a bit
~ Bitwise unary NOT the g, int wise
i nd , s h o pe
& Bitwise AND ivid ort, rato
ua
| Bitwise OR l bi char, rs ca
ts n
^ Bitwise exclusive OR of byte. be a
the
ir o Bitwi pplie
>> Shift right pe s e d
ran Op to th
>>> Shift right zero fill ds. era e i
tor nte
<< Shift left s a ge
ct r
up t y p e
&= Bitwise AND assignment on s:
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
08/27/2022 8
Relational Operators
Th
e
rel rela
Operator Result ati t
on iona
sh lo
== Equal to ip p
be erat
tw ors
!= Not equal to ee
nt d
wo eterm
> Greater than op i
era ne th
< Less than nd e
s.
>= Greater than or equal to
<= Less than or equal to
08/27/2022 9
Operators
• Other operators:
Operator Description
• Other operators:
The special null data type
{that is we may write if (obj!= null)}
Primitive Data Types in Java
Type Kind Memory Range
to 9223372036854775808-
long integer bytes 8
-9223372036854775807
x 1038 to ±3.40282347
float floating point bytes 4
±3.40282347 x 10-45
x 10308 to ±1.76769313486231570
double floating point bytes 8
±4.94065645841246544 x 10-324
08/27/2022 14
Try this….
/** This program demonstrates how Java
* adds two integers. */
public class BigInt
{
public static void main(String args[])
{
int a = 2000000000; //(9 zeros)
int b = 2000000000;
System.out.println ( “This is how Java adds integers”);
System.out.println ( a + “+” + b + “ = ” + (a+b) );
} // end of main
Output:
This is how Java adds integers
}// end of class 2000000000 + 2000000000 = -294967296
08/27/2022 15
Try this….
public class Significant
{
public static void main (String args[])
{
final float PI = 3.141519265359f;
float radius = 1.0f;
float area;
area = PI * radius * radius;
System.out.println (“The area of the circle = ” + area);
}// end of main
}// end of class
Output:
area of the circle = 3.1415193
08/27/2022 16
Variable
• The value of a final variable cannot change after it has been initialized. Such
variables are similar to constants in other programming languages.
• To declare a final variable, use the final keyword in the variable declaration
before the type:
final int aFinalVar = 0;
• It is possible declare the local variable and initialize it later (but only once):
final int blankfinal;
...
blankfinal = 0;
Declaration of variable
A variable is defined by an identifier, a type, and an optional initializer.
The variables also have a scope(visibility / lifetime).
It must be unique within its scope.
It must not be a keyword, a boolean literal (true or false), or the reserved word null.
In Java, all variables must be declared before they can be used. The basic form of a variable declaration is :
type identifier [ = value][, identifier [= value] ...] ;
Java allows variables to be initialized dynamically. For example: double c = 2 * 2;
08/27/2022 18
:Scope and life of a variable
Variables declared inside a scope are not accessible to code outside.
Scopes can be nested. The outer scope encloses the inner scope.
Variables declared in the outer scope are visible to the inner scope.
Variables declared in the inner scope are not visible to the outside scope.
•
08/27/2022 19
Try this….
public class Main
{ public static void main(String args[])
{ int x; // known within main
x = 10;
if (x == 10)
{ int y = 20;
System.out.println("x and y: " + x + " " + y);
x = y + 2; }
System.out.println("x is " + x);
}// end of main Output:
x and y: 10 20
}// end of class x is 22
08/27/2022 20
Try this….
public class Main2
{ public static void main(String args[])
{ if (true)
{ int y = 20;
System.out.println("y: " + y);
} // end of if Output
D:\>jav
:
ac Main
locatio .java M
n: clas
s Main ain.java:9: ca
nnot fin
y = 100; y = 1 00
d symb
ol s y m
; // Erro bol : va
^ r! y not riable y
known
1 error here
}// end of main
}// end of class
08/27/2022 21
Try this….
public class Main3
{ public static void main(String args[])
{ int i = 1;
{int i = 2;
}
}
Output
} Results
:
in compil
ation e
rror.
‘i‘ is alr
eady d
efined…
…
08/27/2022 22
Control Flow Statements
} statement
}
do { for ( ; ; ) {
try {
statement(s)
}
catch (exceptiontype name) {
statement(s)
}
finally {
statement(s)
}
Try catch Example
try
{
Scanner in = new Scanner(new File("input.txt"));
String input = in.next();
process(input);
}
catch (IOException exception)
{
System.out.println("Could not open input file");
}
Break, continue and return
Break and continue:
1) break; || continue; Return:
...
search:
for ( ; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length; j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}}}
Break Example
Control Flow Statements
Question:
What's wrong with the following code :
if (i = 1) {
/* do something */
}
Answer:
Condition is ALWAYS true. Typical mistake of programmers:
= is an assignment == is a comparison
Ussually compiler warns about it.
Control Flow Statements(3)
Question:
What is an output if aNumber = 2?
if (aNumber >= 0)
if (aNumber == 0) System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");
Answer:
second string
third string
The end
08/27/2022 32