You are on page 1of 14

Types and Variables

 In Java, every value has a type . “This is a simple Java program!” has the
type String, the object System.out has the type PrintStream, and the number
13 has the type int.
 To store values so that you can use them at a later time. To remember an
object, you need to hold it in a variable .
 A variable is a storage location in the computer's memory that has a type, a
name, and a contents.
 You use variables to store values that you want to use at a later time.
Ex:
String message = “This is a simple Java program!";
PrintStream printer = System.out;
int luckyNumber = 13;
 Variables can be used in place of the objects that they store:
printer.println(message); // System.out.println("This is a simple Java
program!")
printer.println(luckyNumber); // Same as System.out.println(13)
 Identifiers for variables, methods, and classes are composed of letters,
digits, and underscore characters.
 By convention, variable names should start with a lowercase letter.
 Class names should start with an uppercase letter.
1
Data Types in Java
Primitive data types
int 4 bytes -2.147.483.648  2.147.438.647
short 2 bytes -32.768  32.767
long 8 bytes -9.223.372.036.854.775.808L-9.223.372.036.854.775.807L

float 4 bytes ±3,40282347E+38F


double 8 bytes ± 1,79769313486231570E+308

char 2 bytes -32.768  32.767

Reference data types ( Their values are complex


pointers to objects and arrays. They are managed by
the JVM)
• array
• class
• interface
• null 2
Tokens in Java
 Identifier::= letter (letter+digit)*
letter::=(‘A’-’Z’)+(‘a’-’z’)+’_’+’$’
digit::=‘0’-’9’
 Literals
• integers: 57 -456 0x5A9F 0745 6894576123L
• reals: 3.402F -45.89
• boolean: true false
• characters: ‘f’ ‘T’ ‘9’ ‘\u0A48’ \110 ‘\\’ ‘\n’ ‘\f’ ‘\t’ ‘\”’ ‘\’’
• strings: “This is a string” “” “0123456789\n” “+” “ “
 Operators: + - * / % & | ^ ~ && || ! < > <= >=
<< >> >>> = ? ++ -- == += -= *= /= %= &= |=
^= != <<= >>= >>>= . [ ] ( )
 Separators: { } ; , :
 Comments: /*…………*/ //…………. /**………..*/

3
Expressions
 Expression = description of a computation which
produces an unique value of a well-defined type.
 Operators
 Operands : variables, constants, method calls,
expressions
 Expressions which result in a primitive value:
• Numerical expressions
• Boolean expressions,
• Logical expressions;
 Expressions which result in an object.
Precedence and Associativity Rules for
Operators
Operator type Operators Associativity

Postfix [] . (parameters) expr++ expr-- right


Unary ++expr --expr +expr -expr ~ ! right
Creation or cast new (type)expr right
Multiplicative * / % left
Additive +- left
Shift << >> >>> left
Relational < > <= >= instanceof left
Equality == != left
Bitwise AND & left
Bitwise XOR ^ left
Bitwise OR | left
Conditional AND && left
Conditional OR || left
Conditional ?: left
Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= right
Sequence , left
Examples
int a=5;
System.out.print((a=2) + a); //4

System.out.print(2+3*2); //8

int a,b;
a=b=1;

int[] ar={1,2,3};
int index=2;
ar[index]=index=1;
System.out.print(ar[index]); //{1,2,1}
Numerical expressions
 Operands are fully evaluated from left to right
before an operator is applied.
a+b*c; We first evaluate a,b,c

 Range of Numeric values


int overFlow=Integer.MAX_VALUE +1 // MIN_VALUE
1.0/0.0 Infinity
0.0/0.0 NaN

 +,- (unary)
 *,/,%,+,-
 *=,/=,%=,+=,-=
 ++,--
Numerical expressions
 + Addition operator 5+3 is evaluated to 8
 - Subtraction operator 5-3 is evaluated to 2
 * Multiplication operator 5*3 is 15
 / Division operator 5/2 is 2 but 5.0/2.0=2.5
 % Modulus operator 5%2 is the remainder of
5/2 so 1
 = Assignment operator a=2 , assigns the value 2
to variable a
 += Assign and add operator
a+=2 is a=a+2 (the right-hand side is evaluated
first)
Numerical expressions
 ++ Increment operator, adds 1
a++ is a=a+1
 -- Decrement operator, subtracts 1
a - - is a=a-1
a++ returns a then adds 1
++a adds 1 then returns a+1
int a=3;
System.out.print(a++); //3
System.out.print(a); //4
int b=5;
System.out.print(++b); //6
System.out.print(b); //6
Widening and Narrowing of Primitive Types

• Implicit conversions
int a=10;
long l=a;
double d=a+l;
• The type of an arithmetic expression between
two operands is the widest type AND at least int.
Implicit Primitive Narrowing Conversions
 source is constant expression of : byte, short,
char ,int
 target is byte, short, char
 the value of the source is in range of the target
type

 short s=5;
 final int i=20;
 byte b=i;
Widening and Narrowing of Primitive Types
 Explicit conversions
 Cast operator (<type>)
short x = (short)75;
int pixels = (int)(width/scale);
double result=((double)10/3);
double result2=1/(2/3.0)

(d*i) + (c /-s ) – (f * b)
Boolean expressions
 <,<=,>,>=
 nonassociative
 ==,!=
• primitive data equality
• object reference equality
• object value equality
 !,&,|
 &&,||
 ?:
Control Flow Structures

if-else Decision Switch While loop


if(Condition) switch(Expression) { while(Expression)
Instruction1 case Constant1: Instruction
InstructionList1
case Constant2: do-while loop
InstructionList2
if(Condition) ….... do
Instruction1 default: Instruction
else DefInstructionList while(Expression)
Instruction2 }

if(Condition1)
Instruction1
For Loop
else if(Condition2) for(InitExpress; Condition; IncremExpress)
Instruction2 Instruction
else if(Condition3)
Instruction3 Loop interruption Current Iteration
else break; interruption
ElseInstruction break label; continue;

14

You might also like