22. Java Language Keywords abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while
23.
24.
25.
26.
27.
28.
29.
30. Ranges of the Integral Primitive Types Type Size Minimum Maximum byte 8 bits -2 7 2 7 – 1 short 16 bits -2 15 2 15 – 1 int 32 bits -2 31 2 31 – 1 long 64 bits -2 63 2 63 – 1
31.
32. Ranges of the Floating-Point Primitive Types Type Size Minimum Maximum float 32 bits +/- 1.40 -45 +/- 3.40 +38 double 64 bits +/- 4.94 -324 +/- 1.79 +308 char 16 bits 0 2 16 - 1
41. Variables, Declarations, and Assignments – Examples public class Assignments { public static void main(String args []) { int x, y; // declare int variables float z = 3.414f; // declare and assign float double w = 3.1415; // declare and assign double boolean b = true; // declare and assign boolean char ch; // declare character variable String str; // declare String variable String s = "bye"; // declare and assign String ch = 'A'; // assign a value to char variable str = "Hi out there!"; // assign value to String x = 6; // assign value to int variable y = 1000; // assign values to int variable ... } }
58. If Statement – Example public static void main(String[] args) { int radius = 5; double surface = Math.PI * radius * radius; if (surface > 100) { System.out.println("The circle is too big!"); } else if (surface > 50) { System.out.println( "The circle has acceptable size!"); } else { System.out.println( "The circle is too small!"); } }
59.
60. The switch Statement – Example int dayOfWeek = 3; switch (dayOfWeek) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; ... default: System.out.println("Invalid day!"); break; }
74. Scanner – Example import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner console = new Scanner(System.in); // Get the first input System.out.print("What is your name? "); String name = console.nextLine(); // Get the second input System.out.print("How old are you? "); int age = console.nextInt(); // Display output on the console System.out.println("Hello, " + name + ". " + "Next year, you'll be " + (age + 1)); } }
81. Elements Initialization Element Type Initial Value byte 0 int 0 float 0.0f char ‘ 0000’ object reference null short 0 long 0L double 0.0d boolean false
82.
83.
84. Arrays – Example // Finding the smallest and largest // elements in an array int[] values = {3,2,4,5,6,12,4,5,7}; int min = values[0]; int max = values[0]; for (int i=1; i<values.length; i++) { if (values[i] < min) { min = values[i]; } else if (values[i] > max) { max = values[i]; } } System.out.printf("MIN=%d", min); System.out.printf("MAX=%d", max);
85.
86.
87.
88.
89. M ulti-dimension al A rrays – Example // Finding the sum of all positive // cells from the matrix int[][] matrix = {{2,4,-3}, {8,-1,6}}; int sum = 0; for (int row=0; row<matrix.length; row++) { for (int col=0; col<matrix[row].length; col++) { if (matrix[row][col] > 0) { sum += matrix[row][col]; } } } System.out.println("Sum = " + sum);
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## The Java API is set of runtime libraries that give you a standard way to access the system resources of a host computer. When you write a Java program, you assume the class files of the Java API will be available at any Java virtual machine that may ever have the privilege of running your program (because the Java virtual machine and the class files for the Java API are the required components of any implementation of the Java Platform). When you run a Java program, the virtual machine loads the Java API class files that are referred to by your program's class files. The combination of all loaded class files (from your program and from the Java API) and any loaded dynamic libraries (containing native methods) constitute the full program executed by the Java virtual machine. The class files of the Java API are inherently specific to the host platform. To access the native resources of the host, the Java API calls native methods. The class files of the Java API invoke native methods so your Java program doesn't have to. In this manner, the Java API's class files provide a Java program with a standard, platform-independent interface to the underlying host. Creating platform- independent API is inherently difficult , given that system functionality varies greatly from one platform to another. In addition to facilitating platform independence, the Java API contributes to Java's security model. The methods of the Java API, before they perform any action that could potentially be harmful (such as writing to the local disk), check for permission. In Java releases prior to 1.2, the methods of the Java API checked permission by querying the security manager . The security manager is a special object that defines a custom security policy for the application . A security manager could, for example, forbid access to the local disk.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## As a whole, Java technology leans heavily in the direction of networks, but the Java programming language is quite general-purpose . Java is, first and foremost, an object-oriented language. One promise of object-orientation is that it promotes the re-use of code, resulting in better productivity for developers. In Java, there is no way to directly access memory by arbitrarily casting pointers to a different type or by using pointer arithmetic, as there is in C++. Java requires that you strictly obey rules of type when working with objects. Because Java enforces strict type rules at run- time, you are not able to directly manipulate memory in ways that can accidentally corrupt it. As a result, you can't ever create certain kinds of bugs in Java programs that regularly harass C++ programmers and hamper their productivity. Another way Java prevents you from inadvertently corrupting memory is through automatic garbage collection . Java has a new operator, just like C++, that you use to allocate memory on the heap for a new object. But unlike C++, Java has no corresponding delete operator, which C++ programmers use to free the memory for an object that is no longer needed by the program. In Java, you merely stop referencing an object, and at some later time, the garbage collector will reclaim the memory occupied by the object. You can be more productive in Java primarily because you don't have to chase down memory corruption bugs. But also, you can be more productive because when you no longer have to worry about explicitly freeing memory, program design becomes easier. A third way Java protects the integrity of memory at run-time is array bounds checking . In Java, arrays are full-fledged objects, and array bounds are checked each time an array is used. If you create an array of ten items in Java and try to write to the eleventh, Java will throw an exception. Java won't let you corrupt memory by writing beyond the end of an array. One final example of how Java ensures program robustness is by checking object references , each time they are used, to make sure they are not null. In C++, using a null pointer usually results in a program crash. In Java, using a null reference results in an exception being thrown. The productivity boost you can get just by using the Java language results in quicker development cycles and lower development costs . You can realize further cost savings if you take advantage of the potential platform independence of Java programs. Even if you are not concerned about a network, you may still want to deliver a program on multiple platforms. Java can make support for multiple platforms easier, and therefore, cheaper.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## The executables “javac” and “java” are platform-specific and are part of the Java Development Kit (JDK), which must be installed on the target host machine. Each of the executables take large number of arguments that customize the compilation and execution process (set custom classpath etc.)
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 To create an application, you write a class definition that includes a main() method. To execute an application, type java at the command line, followed by the name of the class containing the main() method to be executed. The main() method must be public so that the JVM can call it. It is static so that it can be executed without the necessity of constructing an instance of the application class. The return type must be void . The argument to main() is a single-dimension array of Strings, containing any arguments that the user might have entered on the command line. For example, consider the following command line: # java Mapper France Belgium With this command line, the args[] array has two elements: France in args[0], and Belgium in args[1].
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Unused keywords There are two keywords that are reserved in Java but which are not used. If you try to use one of these reserved keywords, the Java compiler will produce the following: KeywordTest.java:4: ‘goto’ not supported. goto MyLabel; 1 error const Do not use to declare a constant; use public static final . goto Not implemented in the Java language.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 According to the Java Language Specification these are technically literal values and not keywords. A literal is much the same as a number or any other value. If we try to create an identifier with one of these literal values we will receive errors. class LiteralTest { public static void main (String [] args) { int true = 100; // this will cause error } } Compiling this code gives us the following error: c:Java ProjectsLiteralTest>javac LiteralTest.java LiteralTest.java:3: Invalid expression statement. int true = 100; .. In other words, trying to assign a value to true is much like saying: int 200 = 100;
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 All numeric primitive types are signed. The char type is integral but unsigned. The range of a variable of type char is from 0 through 216 − 1. Java characters are in Unicode, which is a 16-bit encoding capable of representing a wide range of international characters. If the most significant 9 bits of a char are all 0, then the encoding is the same as 7-bit ASCII.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 These types conform to the IEEE 754 specification. Many mathematical operations can yield results that have no expression in numbers (infinity, for example). To describe such non-numeric situations, both double and float can take on values that are bit patterns that do not represent numbers. Rather, these patterns represent non-numeric values. The patterns are defined in the Float and Double classes and may be referenced as shown in the next slide.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 NaN stands for Not a Number The following code fragment shows the use of these constants: double d = -10.0 / 0.0; if (d == Double.NEGATIVE_INFINITY) { System.out.println(&quot;d just exploded: &quot; + d); } In this code fragment, the test on line 2 passes, so line 3 is executed. Non-numeric values cannot be compared – the following is TRUE: ( Float.NaN != Float.NaN )
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 The Java operators are listed in precedence order, with the highest precedence at the top of the table. Each group has been given a name for reference purposes; that name is shown in the left column of the table. Arithmetic and comparison operators are each split further into two sub groupings because they have different levels of precedence. We’ll discuss these groupings later.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 These are applicable to all numeric types and to char and produce a boolean result.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Notice that arithmetic promotions are applied when these operators are used. This is entirely according to the normal rules discussed in Module 4. For example, although it would be an error to attempt to assign, say, the float value 9.0F to the char variable c , it is perfectly acceptable to compare the two. To achieve the result, Java promotes the smaller type to the larger type; hence the char value ‘A’ (represented by the Unicode value 65) is promoted to a float 65.0F. The comparison is then performed on the resulting float values. Although the ordinal comparisons operate satisfactorily on dissimilar numeric types, including char , they are not applicable to any non-numeric types. They cannot take boolean or any classtype operands.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 If the cast, which is represented by the (int) part, were not present, the compiler would reject the assignment; a double value, such as is returned by the arithmetic here, cannot be represented accurately by an int variable. Casts can also be applied to object references. This often happens when you use containers, such as the Vector object. If you put, for example, String objects into a Vector, then when you extract them, the return type of the elementAt() method is simply Object. Module 4, “Converting and Casting,” covers casting, the rules governing which casts are legal and which are not, and the nature of the runtime checks that are performed on cast operations.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 An evaluation from left to right requires that the leftmost expression, a[b], be evaluated first, so it is a reference to the element a[1]. Next, b is evaluated, which is simply a reference to the variable called b. The constant expression 0 is evaluated next, which clearly does not involve any work. Now that the operands have been evaluated, the operations take place. This is done in the order specified by precedence and associativity. For assignments, associativity is right to left, so the value 0 is first assigned to the variable called b; then the value 0 is assigned into the last element of the array a.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 A Java array is an ordered collection of primitives, object references, or other arrays. Java arrays are homogeneous: except as allowed by polymorphism, all elements of an array must be of the same type. That is, when you create an array, you specify the element type, and the resulting array can contain only elements that are instances of that class or subclasses of that class.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 The first example declares an array of a primitive type. Example 2 declares an array of object references (Dimension is a class in the java.awt package). Example 3 declares a two-dimensional array—that is, an array of arrays of floats. The square brackets can come before or after the array variable name - This is also true, and perhaps most useful, in method declarations. A method that takes an array of doubles could be declared as myMethod(double dubs[]) or as myMethod(double[] dubs); a method that returns an array of doubles may be declared as either double[] anotherMethod() or as double anotherMethod()[]. In this last case, the first form is probably more readable.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Since array size is not used until runtime, it is legal to specify size with a variable rather than a literal: int size = 1152 * 900; int [] raster; raster = new int[size];
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Arrays are actually objects, even to the extent that you can execute methods on them (mostly the methods of the Object class), although you cannot subclass the array class. So this initialization is exactly the same as for other objects, and as a consequence you will see the initialization table again in the next section.
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Of course, an array can also be initialized by explicitly assigning a value to each element, starting at array index 0: long [] squares; squares = new long[6000]; for ( int i = 0; i < squares.length; i++) { squares[i] = i * i; } Keep in mind, that the next code is also legal, although it will show only 3 elements: float [] diameters = {1.1f, 2.2f, 3.3f, }; for ( int i = 0; i < diameters.length; i++) { System.out.println(&quot;Element [&quot; + i + &quot;] = &quot; + diameters[i]); }
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Actually, the f igure is misleading. myInts is actually an array with three elements. Each element is a reference to an array containing 4 ints, as shown in the next slide .
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 Actually, the f igure is misleading. myInts is actually an array with three elements. Each element is a reference to an array containing 4 ints, as shown in the next slide .
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## ## (c) 2006 National Academy for Software Development - http://academy.devbg.org* * 07/16/96 When you realize that the outermost array is a single-dimension array containing references, you understand that you can replace any of the references with a reference to a different subordinate array, provided the new subordinate array is of the right type. For example, you can do the following: int [][] myInts = { {1, 2, 3}, {91, 92, 93, 94}, {2001, 2002} }; int [] replacement = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; myInts[1] = replacement;
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 10/09/1007/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##