The Java team identified key considerations for Java including:
- Simple, secure, portable, object-oriented, robust, multithreaded, architecture-neutral, interpreted, high performance, distributed, and dynamic.
Java was designed to be easy to learn and use, support objects, find mistakes early, manage memory automatically, support multithreading, provide platform independence, allow interpretation and native code execution for efficiency, handle network protocols, and support runtime type checking.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1K views
Java Buzzwords
The Java team identified key considerations for Java including:
- Simple, secure, portable, object-oriented, robust, multithreaded, architecture-neutral, interpreted, high performance, distributed, and dynamic.
Java was designed to be easy to learn and use, support objects, find mistakes early, manage memory automatically, support multithreading, provide platform independence, allow interpretation and native code execution for efficiency, handle network protocols, and support runtime type checking.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58
The Java Buzzwords
• The key considerations were summed up by the Java
team in the following list of buzzwords: Simple Secure Portable Object-oriented Robust Multithreaded Architecture-neutral Interpreted High performance Distributed Dynamic • simple – Java is designed to be easy for the professional programmer to learn and use. • object-oriented: a clean, usable, pragmatic approach to objects, not restricted by the need for compatibility with other languages. • Robust: restricts the programmer to find the mistakes early, performs compile-time (strong typing) and run-time (exception-handling) checks, manages memory automatically. • Multithreaded: supports multi-threaded programming for writing program that perform concurrent computations • Architecture-neutral: Java Virtual Machine provides a platform independent environment for the execution of Java byte code • Interpreted and high-performance: Java programs are compiled into an intermediate representation – byte code: a) can be later interpreted by any JVM b) can be also translated into the native machine code for efficiency. • Distributed: Java handles TCP/IP protocols, accessing a resource through its URL much like accessing a local file. • Dynamic: substantial amounts of run-time type information to verify and resolve access to objects at run-time. • Secure: programs are confined to the Java execution environment and cannot access other parts of the computer. • Portability: Many types of computers and operating systems are in use throughout the world—and many are connected to the Internet. • For programs to be dynamically downloaded to all the various types of platforms connected to the Internet, some means of generating portable executable code is needed. The same mechanism that helps ensure security also helps create portability. Indeed, Java's solution to these two problems is both elegant and efficient. The command line argument is the argument passed to a program at the time when you run it. To access the command-line argument inside a java program is quite easy, they are stored as string in String array passed to the args parameter of main() method class Cmd { public static void main(String[] args) { for(int i=0;i< args.length;i++) { System.out.println(args[i]); } } } Execute this program as java cmd 10 20 30 10 20 30 Data Types • Java defines eight simple types: 1)byte – 8-bit integer type 2)short – 16-bit integer type 3)int – 32-bit integer type 4)long – 64-bit integer type 5)float – 32-bit floating-point type 6)double – 64-bit floating-point type 7)char – symbols in a character set 8)boolean – logical values true and false byte: 8-bit integer type. Range: -128 to 127. Example: byte b = -15; Usage: particularly when working with data streams.
short: 16-bit integer type.
Range: -32768 to 32767. Example: short c = 1000; Usage: probably the least used simple type. int: 32-bit integer type. Range: -2147483648 to 2147483647. Example: int b = -50000; Usage: 1) Most common integer type. 2) Typically used to control loops and to index arrays. 3) Expressions involving the byte, short and int values are promoted to int before calculation.
long: 64-bit integer type.
Range: -9223372036854775808 to 9223372036854775807. Example: long l = 10000000000000000; Usage: 1) useful when int type is not large enough to hold the desired value class Light { public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; // approximate speed of light in miles per second lightspeed = 186000; days = 1000; // specify number of days here seconds = days * 24 * 60 * 60; // convert to seconds distance = lightspeed * seconds; // compute distance System.out.print("In " + days); System.out.print(" days light will travel about "); System.out.println(distance + " miles."); } } This program generates the following output: In 1000 days light will travel about 16070400000000 miles. float: 32-bit floating-point number. Range: 1.4e-045 to 3.4e+038. Example: float f = 1.5; Usage: 1) fractional part is needed 2) large degree of precision is not required
• double: 64-bit floating-point number.
Range: 4.9e-324 to 1.8e+308. Example: double pi = 3.1416; Usage: 1) accuracy over many iterative calculations 2) manipulation of large-valued numbers char: 16-bit data type used to store characters. Range: 0 to 65536. Example: char c = ‘a’; Usage: 1) Represents both ASCII and Unicode character sets; Unicode defines a character set with characters found in (almost) all human languages. 2) Not the same as in C/C++ where char is 8-bit and represents ASCII only. // Demonstrate char data type. class CharDemo { public static void main(String args[]) { char ch1, ch2; ch1 = 88; // code for X ch2 = 'Y'; System.out.print("ch1 and ch2: "); System.out.println(ch1 + " " + ch2); //X Y } } // char variables behave like integers. class CharDemo2 { public static void main(String args[]) { char ch1; ch1 = 'X'; System.out.println("ch1 contains " + ch1); ch1++; // increment ch1 System.out.println("ch1 is now " + ch1); } } The output generated by this program is shown here: ch1 contains X ch1 is now Y • boolean: Two-valued type of logical values. Range: values true and false. Example: boolean b = (1<2); Usage: 1) returned by relational operators, such as 1<2 2) required by branching expressions such as if or for. // Demonstrate boolean values. class BoolTest { public static void main(String args[]) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); if(b) System.out.println("This is executed."); b = false; if(b) System.out.println("This is not executed."); // outcome of a relational operator is a boolean value System.out.println("10 > 9 is " + (10 > 9)); } } The output generated by this program is shown here: b is false b is true This is executed. 10 > 9 is true Integer Literals Integers are probably the most commonly used type in the typical program. Any whole number value is an integer literal. Examples are 1, 2, 3, and 42. These are all decimal values, meaning they are describing a base 10 number. There are two other bases which can be used in integer literals, octal (base eight) and hexadecimal (base 16). Octal values are denoted in Java by a leading zero. • Floating-point numbers represent decimal values with a fractional component. They can be expressed in either standard or scientific notation. Examples include 6.022E23, 314159E–05,and 2e+100. • Boolean literals are simple. There are only two logical values that a boolean value can have,true and false. The values of true and false do not convert into any numerical representation. • A literal character is represented inside a pair of single quotes. All of the visible ASCII characters can be directly entered inside the quotes, such as ‘a’, ‘z’, and ‘@’. Variables
• declaration – how to assign a type to a variable
• initialization – how to give an initial value to a variable • scope – how the variable is visible to other parts of the program • lifetime – how the variable is created, used and destroyed • type conversion – how Java handles automatic type conversion • type casting – how the type of a variable can be narrowed down • type promotion – how the type of a variable can be expanded Variables
• Java uses variables to store data.
• To allocate memory space for a variable JVM requires: 1) to specify the data type of the variable 2) to associate an identifier with the variable 3) optionally, the variable may be assigned an initial value • All done as part of variable declaration. // Demonstrate block scope. class Scope { public static void main(String args[]) { int x; // known to all code within main x = 10; if(x == 10) { // start new scope int y = 20; // known only to this block x and y both known here. System.out.println("x and y: " + x + " " + y); x = y * 2; } // y = 100; // Error! y not known here // x is still known here. System.out.println("x is " + x); } } Type Conversion and Casting: Java’s Automatic Conversions When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met: • The two types are compatible. • The destination type is larger than the source type. When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required. Casting Incompatible Types Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign an int value to a byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing conversion Type Conversion
• Widening Type Conversion
– Implicit conversion by compiler automatically
byte -> short, int, long, float, double
short -> int, long, float, double char -> int, long, float, double int -> long, float, double long -> float, double float -> double L 3.6 Type Conversion
• Narrowing Type Conversion
– Programmer should describe the conversion explicitly byte -> char short -> byte, char char -> byte, short int -> byte, short, char long -> byte, short, char, int float -> byte, short, char, int, long double -> byte, short, char, int, long, float L 3.7 General form: (targetType) value Examples: 1) integer value will be reduced module bytes range: int i; byte b = (byte) i; 2) floating-point value will be truncated to integer value: float f; int i = (int) f; class Conversion { public static void main(String args[]) { byte b; int i = 257; double d = 323.142; System.out.println("\nConversion of int to byte."); b = (byte) i; System.out.println("i and b " + i + " " + b); System.out.println("\nConversion of double to int."); i = (int) d; System.out.println("d and i " + d + " " + i); System.out.println("\nConversion of double to byte."); b = (byte) d; System.out.println("d and b " + d + " " + b); } } This program generates the following output: Conversion of int to byte. i and b 257 1 Conversion of double to int. d and i 323.142 323 Conversion of double to byte. d and b 323.142 67 The following program demonstrates how each value in the expression gets promoted to match the second argument to each binary operator: class Promote { public static void main(String args[]) { byte b = 42; char c = 'a'; short s = 1024; int i = 50000; float f = 5.67f; double d = .1234; double result = (f * b) + (i / c) - (d * s); System.out.println((f * b) + " + " + (i / c) + " - " + (d * s)); System.out.println("result = " + result); } } Arrays An array is a group of like-typed variables that are referred to by a common name. // Average an array of values. class Average { public static void main(String args[]) { double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5}; double result = 0; int i; for(i=0; i<5; i++) result = result + nums[i]; System.out.println("Average is " + result / 5); } } Multidimensional Arrays : In Java, multidimensional arrays are actually arrays of arrays. It Demonstrate a two-dimensional array. class TwoDArray { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } } This program generates the following output: 01234 56789 10 11 12 13 14 15 16 17 18 19 // Manually allocate differing size second dimensions. class Twod { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } } This program generates the following output: 0 12 345 6789 Creating Multi dimensional Array in Java In order to create a multi dimensional array we have to use the New operator as we shown below: Data_Type[][][] Array_Name = new int[Tables][Row_Size][Column_Size]; If you observe the above code snippet, Tables: It will decide the number of tables an array can accept. Two Dimensional Array is always single table with rows and columns whereas Multi Dimensional array is more than 1 table with rows and columns. Row_Size: Please specify the number of Row elements an array can store. For example, Row_Size =5 then array will have 5 rows. Column_Size: Number of Column elements an array can store. For example, Column_Size = 6 then array will have 6 Columns. // Demonstrate a three-dimensional array. class Threed { public static void main(String args[]) { int threeD[][][] = new int[3][4][5]; int i, j, k; for(i=0; i<3; i++) for(j=0; j<4; j++) for(k=0; k<5; k++) threeD[i][j][k] = i * j * k; for(i=0; i<3; i++) { for(j=0; j<4; j++) { for(k=0; k<5; k++) System.out.print(threeD[i][j][k] + " "); System.out.println(); } System.out.println(); } } } This program generates the following output: 00000 00000 00000 00000 00000 01234 02468 0 3 6 9 12 00000 02468 0 4 8 12 16 0 6 12 18 24 Operators: operators can be divided into four groups: arithmetic, bitwise, relational, and logical. class BasicMath { public static void main(String args[]) { // arithmetic using integers System.out.println("Integer Arithmetic"); int a = 1 + 1; int b = a * 3; int c = b / 4; int d = c - a; int e = -d; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println("\nFloating Point Arithmetic"); double da = 1 + 1; double db = da * 3; double dc = db / 4; double dd = dc - a; double de = -dd; System.out.println("da = " + da); System.out.println("db = " + db); System.out.println("dc = " + dc); System.out.println("dd = " + dd); System.out.println("de = " + de); }} Integer Arithmetic a=2 b=6 c = 1 d = -1 e = 1 Floating Point Arithmetic da = 2.0 db = 6.0 dc = 1.5 dd = -0.5 de = 0.5 Increment and Decrement operators: The ++ and the – – are Java’s increment and decrement operators. y = x++; is the equivalent of these two y = x; x = x + 1; // Demonstrate ++. class IncDec { public static void main(String args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } } Output: a=2 b=3 c=4 d=1 The Bitwise Logical Operators: The bitwise logical operators are &, |, ^, and ~. // Demonstrate the bitwise logical operators. class BitLogic { public static void main(String args[]) { String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; int a = 3; // 0 + 2 + 1 or 0011 in binary int b = 6; // 4 + 2 + 0 or 0110 in binary int c = a | b; int d = a & b; int e = a ^ b; int f = (~a & b) | (a & ~b); int g = ~a & 0x0f; System.out.println(" a = " + binary[a]); System.out.println(" b = " + binary[b]); System.out.println(" a|b = " + binary[c]); System.out.println(" a&b = " + binary[d]); System.out.println(" a^b = " + binary[e]); System.out.println("~a&b|a&~b = " + binary[f]); System.out.println(" ~a = " + binary[g]); } } a = 0011 b = 0110 a|b = 0111 a&b = 0010 a^b = 0101 ~a&b|a&~b = 0101 ~a = 1100 // Left shifting a byte value. class ByteShift { public static void main(String args[]) { byte a = 64, b; int i; i = a << 2; b = (byte) (a << 2); System.out.println("Original value of a: " + a); System.out.println("i and b: " + i + " " + b); } } The output generated by this program is shown here: Original value of a: 64 i and b: 256 0 The Right Shift: The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. Its general form is shown here: value >> num int a = 32; a = a >> 2; // a now contains 8 When a value has bits that are “shifted off,” those bits are lost. // Demonstrate the boolean logical operators. class Bool1 { public static void main(String args[]) { boolean a = true; boolean b = false; boolean c = a | b; boolean d = a & b; boolean e = a ^ b; boolean f = (!a & b) | (a & !b); boolean g = !a; System.out.println(" a = " + a); System.out.println(" b = " + b); System.out.println(" a|b = " + c); System.out.println(" a&b = " + d); System.out.println(" a^b = " + e); System.out.println("!a&b|a&!b = " + f); System.out.println(" !a = " + g); } } The ? Operator: class Ternary { public static void main(String args[]) { int i, k; i = 10; k = i < 0 ? -i : i; // get absolute value of i System.out.print("Absolute value of "); System.out.println(i + " is " + k); i = -10; k = i < 0 ? -i : i; // get absolute value of i System.out.print("Absolute value of "); System.out.println(i + " is " + k); } } output: Absolute value of 10 is 10 Absolute value of -10 is 10 Control statements control statements can be put into the following categories: selection, iteration, and jump. Java supports two selection statements: if and switch Nested ifs: A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. Here is an example: if(i == 10) { if(j < 20) a = b; if(k > 100) c = d; // this if is else a = c; // associated with this else } else a = d; // this else refers to if(i == 10) The if-else-if Ladder A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder. It looks like this: if(condition) statement; else if(condition) statement; else if(condition) statement; ... else statement; // In a switch, break statements are case 7: optional. case 8: class MissingBreak { case 9: public static void main(String args[]) { System.out.println("i is less than 10"); for(int i=0; i<12; i++) break; switch(i) { default: case 0: System.out.println("i is 10 or more"); case 1: } case 2: } case 3: } case 4: System.out.println("i is less than 5"); break; case 5: case 6: Output: i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is 10 or more i is 10 or more Iteration Statements: Java’s iteration statements are for, while, and do-while. These statements create what we commonly call loops. // Demonstrate the while loop. class While { public static void main(String args[]) { int n = 10; while(n > 0) { System.out.println("tick " + n); n--; }}} tick 10 tick 9 tick 8 tick 7 tick 6 tick 5 tick 4 tick 3 tick 2 tick 1 do-while // Demonstrate the do-while loop. class DoWhile { public static void main(String args[]) { int n = 10; do { System.out.println("tick " + n); n--; } while(n > 0); } } Here is one more for loop variation. You can intentionally create an infinite loop (a loop that never terminates) if you leave all three parts of the for empty. For example: for( ; ; ) { // ... } The general form of the for-each version of the for is shown here: for(type itr-var : collection) statement-block // Use a for-each style for loop. class ForEach { public static void main(String args[]) { int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; // use for-each style for to display and sum the values for(int x : nums) { System.out.println("Value is: " + x); sum += x; } System.out.println("Summation: " + sum); }} Value is: 1 Value is: 2 Value is: 3 Value is: 4 Value is: 5 Value is: 6 Value is: 7 Value is: 8 Value is: 9 Value is: 10 Summation: 55 Nested Loops: // Loops may be nested. class Nested { public static void main(String args[]) { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) System.out.print("."); System.out.println(); } } } Jump Statements Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program. // Using break to exit a loop. class BreakLoop { public static void main(String args[]) { for(int i=0; i<100; i++) { if(i == 10) break; // terminate loop if i is 10 System.out.println("i: " + i); } System.out.println("Loop complete."); } } // Using break with nested loops. class BreakLoop3 { public static void main(String args[]) { for(int i=0; i<3; i++) { System.out.print("Pass " + i + ": "); for(int j=0; j<100; j++) { if(j == 10) break; // terminate loop if j is 10 System.out.print(j + " "); } System.out.println(); } System.out.println("Loops complete."); }} Pass 0: 0 1 2 3 4 5 6 7 8 9 Pass 1: 0 1 2 3 4 5 6 7 8 9 Pass 2: 0 1 2 3 4 5 6 7 8 9 Loops complete. The break statement in the inner loop only causes termination of that loop.The outer loop is unaffected. // Using break as a civilized form of goto. class Break { public static void main(String args[]) { boolean t = true; first: { second: { third: { System.out.println("Before the break."); if(t) break second; // break out of second block System.out.println("This won't execute"); } System.out.println("This won't execute"); } System.out.println("This is after second block."); } } } Running this program generates the following output: Before the break. This is after second block. // Demonstrate continue. class Continue { public static void main(String args[]) { for(int i=0; i<10; i++) { System.out.print(i + " "); if (i%2 == 0) continue; System.out.println(""); } } } 01 23 45 67 89 // Demonstrate return. class Return { public static void main(String args[]) { boolean t = true; System.out.println("Before the return."); if(t) return; // return to caller System.out.println("This won't execute."); } } The output from this program is shown here: Before the return. // Using continue with a label. class ContinueLabel { public static void main(String args[]) { outer: for (int i=0; i<10; i++) { for(int j=0; j<10; j++) { if(j > i) { System.out.println(); continue outer; } System.out.print(" " + (i * j)); } } System.out.println(); } } 0 01 024 0369 0 4 8 12 16 0 5 10 15 20 25 0 6 12 18 24 30 36 0 7 14 21 28 35 42 49 0 8 16 24 32 40 48 56 64 0 9 18 27 36 45 54 63 72 81
(Ebook) The Art of Linux Kernel Design: Illustrating the Operating System Design Principle and Implementation by Lixiang Yang ISBN 9781466518032, 9781466518049, 1466518030, 1466518049 - Get instant access to the full ebook content