Title: Universidad Nacional de Colombia
1C
ertificación en
J
AVA
- Universidad Nacional de Colombia
- Facultad de Ingeniería
- Departamento de Sistemas
21. LANGUAGE FUNDAMENTALS
- Source Files
- Keywords and Identifiers
- Primitive Data Types
- Literals
- Arrays
- Class Fundamentals
- Argument Passing
- Garbage collection
3Source 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)
- Package declaration
- (package)
- import statements
- Class definitions
4Test.java
- // Package declaration
- package exam.prepguide
-
- // Imports
- import java.awt.Button // a class
- import java.util. // a package
-
- // Class Definition
- public class Test ...
5Keywords 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
6Primitive Data Types
- Type Representation Minimum Maximum
- (bits)
- boolean 1
- char 16 0 216-1
- byte 8 -27 27-1
- short 16 -215 215-1
- int 32 -231 231-1
- long 64 -263 263-1
- float 32
- double 64
- Boolean variables true or false
- 4 signed integral data types
- byte, short, int and long
7Char 16-bits encoding Floating-point
types float and double IEEE 754 specification
Float.Nan Float.NEGATIVE_INFINITY Float.POSITIVE_I
NFINITY Double.Nan Double.NEGATIVE_INFINITY Double
.POSITIVE_INFINITY
t
1. double d-10.0/0.0 2. if (dDouble.NEGATIVE_I
NFINITY) 3. System.out.println(d just
explodedd) 4.
t0
t1
t2
t3
t4
8Literals
A value that may be assigned to a primitive or
string variable or passed as an argument to a
method call
boolean boolean isBigtrue boolean
isLittletrue
char char cw char c \u4567
Unicode hexadecimal
t0
t1
t2
t3
t4
9SPECIAL CHARACTERS
\n new line \r return \t tab \b ba
ckspace \f formfeed \ single quote \
double quote \\ backslash
10Literals ...
- floating point
- Decimal point 1.414
- Letter E or e (scientific notation)
- 4.23E21
- Suffix f or F (float 32 bits)
- 1.828f
- Suffix D or d (double 64 bits)
- 1234d
- (default double 64 bits)
integral 28 034 (octal) 0x1c 0X1c 0x1C
String String s Characters in strings are 16
bits
11Arrays ...
An ordered collection of primitives, object
references, or other arrays
Homogeneous elements of the same type
- Declaration name and type of its elements
- Construction
- Initialization
t0
t1
t2
t3
t4
12Array 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()
13Arrays...
The declaration DOES NOT specify the size of an
array!! Array size is specified at runtime via
the new keyword
- int ints // Declaration to the compiler
- intsnew int25 // Run time construction
Size can be specified with a VARIABLE
- int size1152900
- int raster
- raternew intsize
14Declaration and construction in a single line
automatic initialization when an array is
constructed
15Automatic 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
16Initialization
- float diameters1.1f, 2.2f, 3.3f, 4.4f, 5.5f
- long squares
- squares new long6000
- for (int i0 ilt6000 i)
- squaresiii
17Class 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
args1France args2Belgium
18Variables 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
- class HasVariables
- int x20
- static int y 30
19public int wrong() int i return
i5
- Error variable i may not have been initialized
public double fourthRoot(double d) double
result if (dgt0)
resultMath.sqrt(Math.sqrt(d))
return result
Error variable result may not have been
initialized
20Correct Initialization!!
public double fourthRoot(double d) double
result0.0 // initialize if (dgt0)
resultMath.sqrt(Math.sqrt(d))
return result
21Argument passing
A copy of the argument that gets passed
double radians1.2345 System.out.println(Sine
ofradians Math.sin(radians))
Changes to the argument value DO NOT affect the
original data
22Argument passing ...
public void bumper(int bumpMe)
bumpMe15
int xx12345 bumper(xx) System.out.println(Now
xx isxx)
23Object Reference
When an object is created, a value (bit pattern)
that identifies the object is returned
Button btn Btnnew Button(Ok)
24Button btn btn new Button(Good) replacer(btn
) System.out.println(btn.getLabel()) public
void replacer(Button replaceMe)
replaceMenew Button(Evil)
The string printed out is Good
25TextField tf tf new TextField(Yin) changer(t
f) System.out.println(tf.getText()) public
void changer(TextField changeMe)
changeMe.setText(Yang)
The string printed out is Yang, not Yin
26How 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
myValue0) public static void
modifyIt(int value) value 0
27Garbage 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
28 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
29 public Object pop() return
storageindex--
public Object pop() Object
returnValuestorage index
storageindex-- null return
returnValue