Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
in low-level or machine
language (called object code)
Compilers translate the whole program. The program is then executed (run).
Interpreters translate each line of the program (or a block of lines) and execute
it, then translate the next one and execute it, etc.
Java compiler translates a Java program into an intermediate language, called
bytecode. Bytecode is a low-level language. Bytecode is the machine language
for a hypothetical computer called a virtual machine. The bytecode is then
translated into machine code by an interpreter called the Java Virtual Machine
(JVM). JVM translates the byte-code into machine language instructions for a
particular computer and then runs these instructions.
Type Name (Primitive
types)
byte
Kind of Value
short
Int
Long
Float
Double
Char
Integer
Integer
Integer
Floating-point
Floating-point
Single
character
Integer
Boolean
Memory
Used
1 byte
2
4
8
4
8
2
bytes
bytes
bytes
bytes
bytes
bytes
1 but
Range of
Values
-2^8 to 2^8
1
All Unicode
values
True or false
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int bottles = scanner.nextInt(); next(); nextLine(); nextInt(); nextDouble();
nextFloat();
Format String
%d
%5d
Sample Output
24
24
%f
1.21997
%.2f
%s
1.22
Hello
Comments
Use d with integer
Spaces are added so
that the field with is 5
Use f with a floatingpoint number
Two digits after decimal
Use s with a string
Statement
team = 49 + ers
team.charAt(1)
team.subString(1,4)
Result
team = 49ers
Comment
49 is converted to string
9er
Substring at positon 1,
ending before position 4
System.out.println(How\s this?); System.out.println(yes\\no);
System.out.println("\"Java\" is a programming language.");
In classes variables are private or protected and methods are public.
Objects are similar to arrays they always have a reference variable that stores
the memory address of the object or the array
Modifier
Accessible
from
Class
Package
Subclass
Other
Classes
Y
N
N
N
public
Y
Y
Y
protected
Y
Y
Y
private
Y
N
N
default (no
Y
Y
N
modifier)
Overloading a method name is giving a name to more than on method in a class.
Based on the methods name, number and type of parameters. Note you cannot
overload by chanign the return type.
If a withdraw method was bothn in ChequeAccount and Account class, the
withdraw method in the ChequeAccout will be used, and the withdraw method in
the ChequeAccount overrides the withdraw method in the Account class, which is
overridden by the ChequeAccount class. Note that when different parameters are
used, then the subclass has overloaded its superclasss method. Overloading
takes precedence to overriding.
If we declare instance or static variables with the same name in a subclass and
superclass, the variable in the subclass will shadow the variable in the
superclass, just like method overriding.
If we want to use overriden methods, we need to use the super keyword.
Polymorphism is made possible via a mechanism called dynamic binding (or late
binding) of a method definition. Dynamic binding means that in case of method
overriding Java will determine which method to use at run time, not at compile
time. This will depend on the type of the object at run time, i.e. on its subclass.
The dynamic type of an object is the specific subclass type of object that we
have at run time.
Account a = new ChequeAccount(Cindy Cheque); //The static type is Account,
The dynamic type is ChequeAccount
The class Object (part of Java) is the superclass of every class (that is, everything
is an Object). Even classes that we write ourselves, without using inheritance,
are derived classes of Object. The class Object allows us to write methods that
take as a parameter Object, i.e. that represent an object of any class.
import java.util.ArrayList;
ArrayList <String> names = new ArrayList <String> ();
names.add ( Emily ); //size 1 and element Emily
names.add ( Bob ); //size 2 and elements Emily, Bob
names.add ( Cindy ); //size 3
names.add(1, names.add(1, Ann ); // Inserts a new element at position 1
names.remove(0);
names.set(0,Bill);
String last = names.get(name.size() - 1);
ArrayList <String> friends = new ArrayList <String>(names); // Creates a copy of
names