Professional Documents
Culture Documents
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
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
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
+,- (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(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