Sin título de diapositiva

Download Report

Transcript Sin título de diapositiva

ertificación en
AVA
Universidad Nacional de Colombia
Facultad de Ingeniería
Departamento de Sistemas
1. LANGUAGE FUNDAMENTALS
Source Files
Keywords and Identifiers
Primitive Data Types
Literals
Arrays
Class Fundamentals
Argument Passing
Garbage collection
Source Files




“.java” extension
At most one top-level public class definition
(unlimited
number
of
non-public
class
definitions)
Class name= unextended filename
Three top-level elements (None of them
required)
1. Package declaration
(package)
1. import statements
2. Class definitions
Test.java
// Package declaration
package exam.prepguide;
// Imports
import java.awt.Button; // a class
import java.util.*; // a package
// Class Definition
public class Test { ... }
Keywords and Identifiers
Identifier :
word used by a programmer to name a
variable, method, class, or label
 Keywords and reserved words may not be used

as identifiers!!
An identifier must begin with a letter, a dollar
sign (‘$’), or an underscore (‘_’); subsequent
characters may be letters, ‘$’, ‘_’ , or digits
Primitive Data Types
Type
• boolean
• char
• byte
• short
• int
• long
• float
• double


Representation
(bits)
1
16
8
16
32
64
32
64
Minimum Maximum
0
-27
-215
-231
-263
Boolean variables : true or false
4 signed integral data types:
byte, short, int and long
216-1
27-1
215-1
231-1
263-1
Char: 16-bits encoding
Floating-point types:
float and double
IEEE 754 specification
Nan: Not a number
Float.Nan
Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Double.Nan
Double.NEGATIVE_INFINITY
t
Double.POSITIVE_INFINITY
1. double d=-10.0/0.0;
2. if (d==Double.NEGATIVE_INFINITY) {
t[0]
t[1]
t[2]t[3] t[4]
3.
System.out.println(“d
just
exploded”+d);
4. }
Literals
A value that may be assigned to a
primitive or string variable or passed
as an argument to a method call
boolean:
char:
boolean isBig=true;
char c=´w´;
boolean isLittle=true;
char c= ´\u4567´;
Unicode hexadecimal
t[0] t[1] t[2]t[3] t[4]
SPECIAL CHARACTERS
´\n’
´\r’
´\t’
´\b’
´\f’
´\’ ’
´\” ’
´\\ ’
new line
return
tab
backspace
formfeed
single quote
double quote
backslash
Literals ...
integral
28
034 (octal)
0x1c
0X1c
0x1C
floating point
• Decimal point : 1.414
• Letter E or e (scientific notation):
4.23E+21
• Suffix f or F (float 32 bits)
1.828f
• Suffix D or d (double 64 bits):
1234d
(default: double 64 bits)
String:
String s =“Characters in strings are 16 bits”;
Arrays ...
An ordered collection of primitives,
object references, or other arrays
Homogeneous: elements of the same type
1. Declaration: name and type of its elements
2. Construction
3. Initialization
t[0] t[1] t[2]t[3] t[4]
Array declaration
int ints[];
double dubs[];
Dimension dims[];
float toDee[] [];
[] can come before or after the array name
myMethod(double dubs [])
myMethod(double[] dubs)
double[] anotherMethod()
double anotherMethod() []
Arrays...
The declaration DOES NOT specify the size of an
array!! Array size is specified at runtime via the
new keyword
1. int ints[]; // Declaration to the compiler
2. ints=new int[25]; // Run time construction
Size can be specified with a VARIABLE
1. int size=1152*900;
2. int raster[];
3. rater=new int[size];
Declaration and construction in a single line
1. int ints[]=new int[25];
automatic initialization when an array is constructed
Automatic Initialization
Element
Initial
Type
Value
boolean
false
char
‘\u0000’
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0d
object reference null
Initialization
1. float diameters[]={1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
1. long squares[];
2. squares =new long[6000];
3. for (int i=0; i<6000; i++)
4.
squares[i]=i*i;
Class Fundamentals
main() method
 Entry point for JAVA applications
 Application: class that includes main()
 Execution: java at the command line,
followed by the name of the class
public static void main(String args[])
Command line
% java Mapper France Belgium
args[1]=“France”
args[2]=“Belgium”
Variables and initialization


Member Variable:
 When an instance is created
 Accessible from any method in the class
Automatic (Local) Variable:
 On entry to the method
 Exists only during execution of the
method
 Only accessible within the method
1. class HasVariables {
2.
int
x=20;
3.
static int y =30;
public int wrong() {
int i;
return i+5;
}
Error: “variable i may not have been initialized”
public double fourthRoot(double d) {
double result;
if (d>=0) {
result=Math.sqrt(Math.sqrt(d));
}
return result;
}
Error: “variable result may not have been initialized”
Correct Initialization!!
public double fourthRoot(double d) {
double result=0.0; // initialize
if (d>=0) {
result=Math.sqrt(Math.sqrt(d));
}
return result;
}
Argument passing
A copy of the argument that gets passed
double radians=1.2345;
System.out.println(“Sine of”+radians
+”=“+Math.sin(radians));
Changes to the argument value DO NOT affect
the original data
Argument passing ...
public void bumper(int bumpMe) {
bumpMe+=15;
}
int xx=12345;
bumper(xx);
System.out.println(“Now xx is”+xx);
Object Reference
When an object is created, a value (bit pattern)
that identifies the object is returned
Button btn;
Btn=new Button(“Ok”);
Button btn;
btn = new Button(“Good”);
replacer(btn);
System.out.println(btn.getLabel());
public void replacer(Button replaceMe) {
replaceMe=new Button(“Evil”);
}
The string printed out is ”Good”
TextField tf;
tf = new TextField(“Yin”);
changer(tf);
System.out.println(tf.getText());
public void changer(TextField changeMe) {
changeMe.setText(“Yang”);
}
The string printed out is ”Yang”, not ”Yin”
How to create a Reference to a primitive
public class PrimitiveReference {
public static void main( String args [])
int [] myValue ={ 1 };
modifyIt(myValue);
System.out.println(“myValue contains “+
myValue[0]);
}
public static void modifyIt(int [] value) {
value [0]++;
}
}
Garbage Collection
Storage can remain allocated longer than the
lifetime of any method call
releasing the storage when you have finished
with it:
 Corrupted data: releasing storage too soon
 Memory Shortage: forget to release it
Automatic Garbage Collection:
The runtime system keeps track of the memory
that is allocated and determines whether it is still
usable
Garbage collector: Low-priority thread
public Object pop() {
return storage[index--];
}
public Object pop() {
Object returnValue=storage [index];
storage[index--] =null;
return returnValue;
}