04 JavaBase PDF
04 JavaBase PDF
Comments
C-style comments (multi-lines)
/* this comment is so long
that it needs two lines */
Java (basic concepts)
Comments on a single line
// comment on one line
Politecnico di Torino 2
1
26/03/2009
Class
Defined by developer (eg, Exam) or by the Java
environment (eg, String)
the following declaration
Exam e; e null
Primitive types
…allocates memory space for the reference
(„pointer‟)
…and sometimes it initializes it with null by default
Allocation and initialization of the object value are
made later by its constructor
Object
e 0Xffe1
e = new Exam(); Exam
Politecnico di Torino 11
2
26/03/2009
Class
Object descriptor
It consists of attributes and methods
Classes
Politecnico di Torino 18
3
26/03/2009
Overloading Overloading
In a Class there may be public class Foo{
different methods with class Car { public void doIt(int x, long c){
the same name String color; System.out.println("a");
But they have a void paint(){
}
different signature color = “white”;
public void doIt(long x, int c){
A signature is made System.out.println("b");
} }
by: void paint(int i){} public static void main(String args[]){
Method name
void paint(String Foo f = new Foo();
Ordered list of
parameters types newCol){ f.doIt( 5 ,(long)7 ); // “a”
color = newCol; f.doIt( (long)5 , 7 ); // “b”
the method whose }
parameters types list }
matches, is then } }
executed
Politecnico di Torino 21 Politecnico di Torino 22
Objects Objects
class Car {
An object is identified by: String color;
Its class, which defines its structure void paint(){
(attributes and methods) color = “white”;
}
Its state (attributes values) void paint(String newCol) {
An internal unique identifier color = newCol;
}
Zero, one or more reference can point }
to the same object Car a1, a2;
a1 = new Car();
a1.paint(“green”);
a2 = new Car();
4
26/03/2009
5
26/03/2009
6
26/03/2009
Operations on references
Only the relational operators == and != are
defined
Note well: the equality condition is evaluated on
the values of the references and NOT on the
values of the objects ! Strings
The relational operators tell you whether the
references points to the same object in memory
Dotted notation is applicable to object
references
There is NO pointer arithmetic
Politecnico di Torino 39
String Operator +
No primitive type to represent string It is used to concatenate 2 strings
String literal is a quoted text “This string” + ” is made by two strings”
C
char s[] = “literal” Works also with other types
Equivalence between string and char (automatically converted to string)
arrays
System.out.println(”pi = ” + 3.14);
Java System.out.println(”x = ” + x);
char[] != String
String class in java.lang library
7
26/03/2009
String String
String valueOf(int)
int length() Converts int in a String – available for all primitive
returns string length types
boolean equals(String s) String toUpperCase()
compares the values of 2 strings String toLowerCase()
String s1, s2;
s1 = new String(“First string”);
String concat(String str) - like ‘+’
s2 = new String(“First string”); int compareTo(String str)
System.out.println(s1);
Compare w.r.t alphabetical order
System.out.println(“Length of s1 = ” +
s1.length()); <0 if this < str
if (s1.equals(s2)) // true ==0 if this == str
if (s1 == s2) // false
>0 if this > str
Politecnico di Torino 44 Politecnico di Torino 45
String StringBuffer
String subString(int startIndex)
String s = “Human”;
insert
s.subString(2) returns “man” append
String subString(int start, int end) delete
Char „start‟ included, „end‟ excluded
reverse
String s = “Greatest”;
s.subString(0,5) returns “Great”
int indexOf(String str)
Returns the index of the first occurrence of str
int lastIndexOf(String str)
The same as before but search starts from the end
Politecnico di Torino 46 Politecnico di Torino 47
Character
Utility methods on the kind of char
isLetter(), isDigit(),
isSpaceChar()
Utility methods for conversions Scope and encapsulation
toUpper(), toLower()
Politecnico di Torino 48
8
26/03/2009
9
26/03/2009
10
26/03/2009
Politecnico di Torino 62
11
26/03/2009
s heap
s[1] = new null
String(“abcd”); null “abcd”
Array length (number of elements) is
null
Person[] p =
{new Person(“John”) , p heap for (int i=0; i < a.length; i++)
new Person(“Susan”)}; John a[i] = i;
Susan
12
26/03/2009
Motivation
Class is a better element of
modularization than a procedure
But it is still little
Package
For the sake of organization, Java
provides the package feature
Politecnico di Torino 79
13
26/03/2009
14
26/03/2009
no no yes no
Package Q Package Q
class C { class C {
public void f2(){} public void f2(){}
} }
Access rules
Method in the Method of other Method of other
same class class in the same class in other
Politecnico di Torino 90
15
26/03/2009
Enum Enum
Defines an enumerative type Enum can be declared outside or inside a class,
public enum Suits { but NOT within a method
SPADES, HEARTS, DIAMONDS, CLUBS Enums are not Strings or ints, but more like a
} kind of class but constructor can‟t be invoked
Variables of enum types can assume directly, conceptually like this:
only one of the enumerated values
class Suits {
Suits card = Suits.HEARTS;
public static final Suits HEARTS= new Suits (“HEARTS”,0);
They allow much more strict static public static final Suits DIAMONDS= new Suits (“DIAMONDS”,1);
checking compared to integer
constants (used e.g. in C) public static final Suits CLUBS= new Suits (“CLUBS”, 2);
public static final Suits SPADES= new Suits (“SPADES”, 3);
public Suits (String enumName, int index) {…}
}
Politecnico di Torino 94 Politecnico di Torino 95
Motivation
In an ideal OO world, there are only
classes and objects
Wrapper classes for For the sake of efficiency, Java use
built-in types primitive types (int, float, etc.)
Politecnico di Torino 97
16
26/03/2009
Example Autoboxing
Integer obj = new Integer(88); In Java 5 an automatic conversion
String s = obj.toString(); between primitive types and wrapper
int i = obj.intValue(); classes (autoboxing) is performed.
Integer i= new Integer(2); int j;
j = i + 5;
int j = Integer.parseInt(“99”); //instead of:
int k = (new Integer(99)).intValue(); j = i.intValue()+5;
i = j + 2;
//instead of:
i = new Integer(j+2);
Politecnico di Torino 100 Politecnico di Torino 101
17
26/03/2009
Memory types
Depending on the kind of elements they
include:
Static memory
Memory management elements living for all the execution of a
program (class definitions, static variables)
Heap (dynamic memory)
elements created at run-time (with „new‟)
Stack
elements created in a code block (local
variables and method parameters)
Class Window {
There is a different copy in each instance of …
the Class void resize () {
int i;
Create/initialized whenever a new instance for (i=0; i<5; i++) { … }
of a Class is created } // here i is destroyed
18
26/03/2009
19