Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
0Activity
×
0 of .
Results for:
No results containing your search query
P. 1
Java

Java

Ratings: (0)|Views: 37|Likes:
Java Basic
Java Basic

More info:

Published by: Niyamathulla Khaleel on Jan 11, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See More
See less

01/11/2013

pdf

text

original

 
26/03/20091
 Java (basic concepts)
 Version 3 -March 2009
Politecnico di Torino
2
Comments
C-style comments (multi-lines)
/* this comment is so longthat it needs two lines */
Comments on a single line
// comment on one line
Politecnico di Torino
3
Code blocks and Scope
 Java code blocks are the same as in Clanguage
Each block is enclosed bybraces{ } andstarts a newscopefor the variables
Variables can be declared both at thebeginning and in the middle of block code
for (int i=0; i<10; i++){int x = 12;...int y;...}
Politecnico di Torino
4
Control statements
Same as C
if-else,switch,while,do-while,for,break,continue
Politecnico di Torino
5
Boolean
 Java has an explicit type (boolean) torepresent logic values (
true
,
false
)
Conditional constructs evaluateboolean conditions
Note well-
It‟s not possible to evaluate
this condition
int x = 7;if(x)
{…}
//NO
Use relational operators
if (x != 0)
Politecnico di Torino
6
Passing parameters
Parameters are always passedby value
...they can be primitive types or objectreferences
Note well: only the object reference iscopied not the value of the object
 
26/03/20092
Politecnico di Torino
7
Constants
Thefinalmodifier
final float PI = 3.14;PI = 16.0; // ERROR, no changesfinal int SIZE; // ERROR, init missing
Use uppercases (coding conventions)
Politecnico di Torino
8
Elements in a OO program
Structural elements(types)(compile time)
Class
Primitive typeDynamic elements(data)(run time)
Reference
Variable
Politecnico di Torino
9
Classes and primitive types
Class
class Exam {}
Variable of typereference
Exam e;e = new Exam();
type primitive
int, char,float
Variable of typeprimitive
int i;
descriptorinstance
 
Politecnico di Torino
10
Defined in the language:
int, double, boolean
Instance declaration:
Declares instance nameDeclares the typeAllocates memory space for the reference
Primitive type
0
int i;
Politecnico di Torino
11
Class
Defined by developer (eg, Exam) or by the Javaenvironment (eg, String)
the following declaration
…allocates memory space for the
reference 
(„pointer‟)…and
sometimes 
it initializes it withnullby default
Allocation and initialization of the
object 
value aremade later by its constructor
Exam e;
enullObjectExame
e = new Exam();
0Xffe1
Primitive types
 
26/03/20093
Politecnico di Torino
13
Primitive types
Have a unique dimension and encoding
Representation is platform-independent
typeDimensionEncoding
 boolean
1 bit-
char
16 bits
Unicode
 byte
8 bitsSigned integer 2C
short
16 bitsSigned integer 2C
int
32 bitsSigned integer 2C
long
64 bitsSigned integer 2C
float
32 bitsIEEE 754 sp
double
64 bitsIEEE 754 dp
void 
--
Politecnico di Torino
14
Constants
Constants of type int, float, char,strings follow C syntax
123 256789L 0xff34 123.750.12375e+3
‟a‟ ‟%‟ ‟
\
n‟ ”prova” ”prova
\
n”
Boolean constants (do not exist in C)are
true
,
false
Politecnico di Torino
15
Operators (integer and floating-point)
Operators follow C syntax:
arithmetical
+ -* / %
relational
== != > < >= <=
bitwise (int)& | ^ << >> ~Assignment
= += -= *= /=%= &= |= ^=
Increment
++ --
Chars are considered like integers(e.g. switch)
Politecnico di Torino
16
Logical operators
Logical operators follows C syntax:
&& || ! ^
Note well: Logical operators workONLY on booleans
Type int is NOT considered a booleanvalue like in CRelational operators work with booleanvalues
Classes
Politecnico di Torino
18
Class
Object descriptor
It consists of attributes and methods

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->