1 / 29

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

C. ertificación en. J. 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.

Download Presentation

Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C ertificación en J AVA Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas

  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) • Package declaration • (package) • import statements • 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 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

  6. Primitive 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 • Booleanvariables: trueorfalse • 4 signed integral data types: byte, short, intandlong

  7. Char:16-bits encoding Floating-point types: floatanddouble IEEE 754 specification Nan:Not a number Float.Nan Float.NEGATIVE_INFINITY Float.POSITIVE_INFINITY Double.Nan Double.NEGATIVE_INFINITY Double.POSITIVE_INFINITY t 1. double d=-10.0/0.0; 2. if (d==Double.NEGATIVE_INFINITY) { 3. System.out.println(“d just exploded”+d); 4. } t[0] t[1] t[2] t[3] t[4]

  8. Literals A value that may be assigned to a primitive or string variable or passed as an argument to a method call boolean: boolean isBig=true; boolean isLittle=true; char: char c=´w´; char c= ´\u4567´; Unicode hexadecimal t[0] t[1] t[2] t[3] t[4]

  9. SPECIAL CHARACTERS ´\n’ new line ´\r’ return ´\t’ tab ´\b’ backspace ´\f’ formfeed ´\’ ’ single quote ´\” ’ double quote ´\\ ’ backslash

  10. Literals ... • 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) integral 28 034(octal) 0x1c 0X1c 0x1C String: String s =“Characters in strings are 16 bits”;

  11. Arrays ... 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 t[0] t[1] t[2] t[3] t[4]

  12. 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() doubleanotherMethod() []

  13. Arrays... The declarationDOES NOTspecify the size of an array!! Array size is specified atruntimevia thenewkeyword int ints[]; // Declaration to the compiler ints=new int[25]; // Run time construction Size can be specified with aVARIABLE int size=1152*900; int raster[]; rater=new int[size];

  14. Declaration and construction in a single line int ints[]=new int[25]; automaticinitialization when an array is constructed

  15. 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

  16. Initialization float diameters[]={1.1f, 2.2f, 3.3f, 4.4f, 5.5f}; long squares[]; squares =new long[6000]; for (int i=0; i<6000; i++) squares[i]=i*i;

  17. Class Fundamentals main() method • Entry point for JAVA applications • Application:class that includesmain() • 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”

  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 • class HasVariables { • int x=20; • static int y =30;

  19. public int wrong() { int i; return i+5; } Error:“variablei may not have been initialized” public double fourthRoot(double d) { double result; if (d>=0) { result=Math.sqrt(Math.sqrt(d)); } return result; } Error:“variableresultmay not have been initialized”

  20. Correct Initialization!! public double fourthRoot(double d) { double result=0.0; // initialize if (d>=0) { result=Math.sqrt(Math.sqrt(d)); } return result; }

  21. Argument passing Acopyof the argument that gets passed double radians=1.2345; System.out.println(“Sine of”+radians +”=“+Math.sin(radians)); Changes to the argument valueDO NOTaffect the original data

  22. Argument passing ... public void bumper(int bumpMe) { bumpMe+=15; } int xx=12345; bumper(xx); System.out.println(“Now xx is”+xx);

  23. Object Reference When an object is created, a value (bit pattern) that identifies the object is returned Button btn; Btn=new Button(“Ok”);

  24. 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”

  25. 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”

  26. 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]++; • } • }

  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 storagetoo soon • Memory Shortage:forgetto 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 storage[index--]; • } public Object pop() { Object returnValue=storage [index]; storage[index--] =null; return returnValue; }

More Related