Download presentation
Presentation is loading. Please wait.
Published byElijah Small Modified over 9 years ago
1
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA
2
1. LANGUAGE FUNDAMENTALS Source Files Keywords and Identifiers Primitive Data Types Literals Arrays Class Fundamentals Argument Passing Garbage collection
3
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
4
Test.java // Package declaration package exam.prepguide; // Imports import java.awt.Button; // a class import java.util.*; // a package // Class Definition public class Test {... }
5
Keywords and Identifiers 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 Identifier : word used by a programmer to name a word used by a programmer to name a variable, method, class, or label
6
Primitive Data Types Boolean variables : true or false 4 signed integral data types: byte, short, int and long TypeRepresentation Minimum Maximum TypeRepresentation Minimum Maximum (bits) (bits) boolean1 boolean1 char16 0 2 16 -1 char16 0 2 16 -1 byte8 -2 7 2 7 -1 byte8 -2 7 2 7 -1 short16 -2 15 2 15 -1 short16 -2 15 2 15 -1 int32 -2 31 2 31 -1 int32 -2 31 2 31 -1 long64 -2 63 2 63 -1 long64 -2 63 2 63 -1 float32 float32 double64 double64
7
Char: Char: 16-bits encoding Floating-point types: floatdouble float and double IEEE 754 specification t t[0]t[1]t[2]t[3]t[4] Float.NanFloat.NEGATIVE_INFINITYFloat.POSITIVE_INFINITYDouble.NanDouble.NEGATIVE_INFINITYDouble.POSITIVE_INFINITY Nan: Not a number 1. double d=-10.0/0.0; 2. if (d==Double.NEGATIVE_INFINITY) { 3.System.out.println(“d just exploded”+d); 4. }
8
Literals boolean: boolean isBig=true; boolean isLittle=true; A value that may be assigned to a primitive or string variable or passed as an argument to a method call t[0]t[1]t[2]t[3]t[4] char: char c=´w´; char c= ´\u4567´; Unicode hexadecimal
9
´\n’new line ´\r’return ´\t’tab ´\b’backspace ´\f’formfeed ´\’ ’single quote ´\” ’double quote ´\\ ’backslash SPECIAL CHARACTERS
10
Literals... integral28 034 (octal) 0x1c0X1c 0x1C floating point Decimal point : 1.414 Decimal point : 1.414 Letter E or e (scientific notation): Letter E or e (scientific notation): 4.23E+21 4.23E+21 Suffix f or F (float 32 bits) Suffix f or F (float 32 bits)1.828f Suffix D or d (double 64 bits): Suffix D or d (double 64 bits):1234d (default: double 64 bits) String: String s =“Characters in strings are 16 bits”;
11
Arrays... t[0]t[1]t[2]t[3]t[4] An ordered collection of primitives, object references, or other arrays Homogeneous: elements of the same type Homogeneous: elements of the same type 1.Declaration: name and type of its elements 2.Construction 3.Initialization
12
Array declaration int ints[]; double dubs[]; Dimension dims[]; float toDee[] []; [] can come before or after the array name [] can come before or after the array name myMethod(double dubs []) myMethod(double[] dubs) double[] anotherMethod() double anotherMethod() []
13
Arrays... The declaration DOES NOT specify the size of an array!! Array size is specified at runtime via the new keyword 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 1.int size=1152*900; 2.int raster[]; 3.rater=new int[size]; Size can be specified with a VARIABLE
14
1.int ints[]=new int[25]; Declaration and construction in a single line automatic initialization when an array is constructed
15
ElementInitial TypeValue booleanfalse booleanfalse char‘ \ u0000’ char‘ \ u0000’ byte0 byte0 short0 short0 int0 int0 long0L long0L float0.0f float0.0f double0.0d double0.0d object reference null object reference null Automatic Initialization
16
1.float diameters[]={1.1f, 2.2f, 3.3f, 4.4f, 5.5f}; Initialization 1.long squares[]; 2.squares =new long[6000]; 3.for (int i=0; i<6000; i++) 4. squares[i]=i*i;
17
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[]) % java Mapper France Belgium Command line args[1]=“France”args[2]=“Belgium”
18
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;
19
public int wrong() { int i; int i; return i+5; return i+5;} public double fourthRoot(double d) { double result; double result; if (d>=0) { if (d>=0) { result=Math.sqrt(Math.sqrt(d)); result=Math.sqrt(Math.sqrt(d)); } return result; return result;} Error: “variable i may not have been initialized” Error: “variable result may not have been initialized”
20
public double fourthRoot(double d) { double result=0.0; // initialize double result=0.0; // initialize if (d>=0) { if (d>=0) { result=Math.sqrt(Math.sqrt(d)); result=Math.sqrt(Math.sqrt(d)); } return result; return result;} Correct Initialization!!
21
Argument passing A copy of the argument that gets passed double radians=1.2345; System.out.println(“Sine of”+radians +”=“+Math.sin(radians)); +”=“+Math.sin(radians)); Changes to the argument value DO NOT affect the original data
22
Argument passing... public void bumper(int bumpMe) { bumpMe+=15; bumpMe+=15;} int xx=12345; bumper(xx); System.out.println(“Now xx is”+xx);
23
Object Reference Button btn; Btn=new Button(“Ok”); When an object is created, a value (bit pattern) that identifies the object is returned
24
Button btn; btn = new Button(“Good”); replacer(btn);System.out.println(btn.getLabel()); public void replacer(Button replaceMe) { replaceMe=new Button(“Evil”); replaceMe=new Button(“Evil”);} The string printed out is ”Good”
25
TextField tf; tf = new TextField(“Yin”); changer(tf);System.out.println(tf.getText()); public void changer(TextField changeMe) { changeMe.setText(“Yang”); changeMe.setText(“Yang”);} The string printed out is ”Yang”, not ”Yin”
26
How to create a Reference to a primitive public class PrimitiveReference { public class PrimitiveReference { public static void main( String args []) public static void main( String args []) int [] myValue ={ 1 }; int [] myValue ={ 1 }; modifyIt(myValue); modifyIt(myValue); System.out.println(“myValue contains “+ System.out.println(“myValue contains “+ myValue[0]); myValue[0]); } public static void modifyIt(int [] value) { public static void modifyIt(int [] value) { value [0]++; value [0]++; } }
27
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
28
Automatic Garbage Collection: 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() { public Object pop() { return storage[index--]; return storage[index--];} public Object pop() { public Object pop() { Object returnValue=storage [index]; Object returnValue=storage [index]; storage[index--] =null; storage[index--] =null; return returnValue; return returnValue;}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.