fifteen years (15 Yrs) experience in various phase of System Development Life Cycle, specialized using COBOL either in mainframe or midrange platform, POWERBUILDER And Visual Basic/VBA/Net for object oriented programming. Working knowledge in Web development using ASP.NET, C#,JAVA, JAVASCRIPT, XML, HTML, and SHAREPOINT. Hands on IT experience in the following Industries (Banking, Insurance, Manufacturing, Government, Retailing Business, and Telecommunication).
the Java language, as well as a full coverage of the Java Syntax You will be exposed to examples of building programs that incorporates Java as it’s primary building blocks.
Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc in 1991. It took 18 months to develop the first working version. Then language was initially called “Oak but renamed “Java” in 1995. Somewhat surprisingly, the original impetus for Java was not the internet! Instead, the primary motivation was the need for a platform-independent (that is architecture- neutral) language that could be used to create software
microwave ovens and remote control. About the time that the details of java were being worked out, a second factor was emerging that would play a crucial role in the future of Java. This was, of course, the World Wide Web. Had the Web not taken shape at about the same time that Java was being implemented. Java might have remained a useful but obscure language for programming consumer electronics. However, with the emergence of the World Wide Web, Java was propelled to the forefront of
programs. The Java Programming Language? Java is a general-purpose, concurrent, class-based, object-oriented language. It is designed to be simple enough that many programmers can achieve fluency in the language. Java is related to C and C++ but is organized rather differently, with a number of aspects of C and C++ omitted and a few ideas from other languages included. Java is intended to be a production language, not a research language, and so, as C. A. R. Hoare suggested in his classic paper on language design, the design of Java has avoided including new and untested features. Java is strongly typed. This specification clearly distinguishes between the compile-time errors that can and must be detected at compile time, and those that occur at run time. Compile time normally consists of translating Java programs into a machine-independent byte-code representation. Run-time activities include loading and linking of the classes needed to execute a program, optional machine code generation and dynamic optimization of the program, and actual program execution.
the machine representation are not available through the language. It includes automatic storage management, typically using a garbage collector, to avoid the safety problems of explicit deallocation (as in C's free or C++'s delete). High-performance garbage- collected implementations of Java can have bounded pauses to support systems programming and real-time applications. Java does not include any unsafe constructs, such as array accesses without index checking, since such unsafe constructs would cause a program to behave in an unspecified way. Java is normally compiled to a bytecoded instruction set and binary format defined in The Java Virtual Machine Specification
output Escape sequence Description \ n Newline. Position the screen cursor at the beginning of the next line. \ t Horizontal tab. Move the screen cursor to the next tab stop. \ r Carriage return. Position the screen cursor at the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. \ \ Backslash. Used to print a backslash character. \ " Double quote. Used to print a double quote charcter Fig. 2.5 Some common escape sequences.
variable's data type determines the values that the variable can contain and the operations that can be performed on it. For example, in the MaxVariablesDemo program, the declaration int largestInteger declares that largestInteger has an integer data type (int). Integers can contain only integral values (both positive and negative). You can perform arithmetic operations, such as addition, on integer variables. The Java programming language has two categories of data types: primitive and reference. A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean value. For example, an integer value is 32 bits of data in a format known as two's complement, the value of a char is 16 bits of data formatted as a Unicode character, and so on.
lu e s St a n d a rd boo le a n tru e or fa ls e [N ote: T he representation of a boolean is specific to the Java V irtual M achine on each com puter platform .] cha r 16 '\u 00 0 0' to '\ uFFF F' (0 to 65535) (ISO U nicode character set) byt e 8 –128 to +127 (–27 to 27 – 1) sho rt 16 –32,768 to +32,767 (–215 to 215 – 1) int 32 –2,147,483,648 to +2,147,483,647 (–231 to 231 – 1) lon g 64 –9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (–263 to 263 – 1) flo at 32 N egative range: –3.4028234663852886E +38 to –1.40129846432481707e–45 Positive range: 1.40129846432481707e–45 to 3.4028234663852886E +38 (IE EE 754 floating point) dou bl e 64 N egative range: –1.7976931348623157E +308 to –4.94065645841246544e–324 Positive range: 4.94065645841246544e–324 to 1.7976931348623157E +308 (IE EE 754 floating point) Fig . 4.16 Th e Ja v a p rim it iv e ty p e s.
under J2SE 5.0. It requires no other files. public class MaxVariablesDemo { public static void main(String args[]) { // integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; // other primitive types char aChar = 'S'; boolean aBoolean = true;
+ largestByte); System.out.println("The largest short value is " + largestShort); System.out.println("The largest integer value is " + largestInteger); System.out.println("The largest long value is " + largestLong); System.out.println("The largest float value is " + largestFloat); System.out.println("The largest double value is " + largestDouble); if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); } System.out.println("The value of aBoolean is " + aBoolean); } }
value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable (see the following figure). A reference is called a pointer, or a memory address in other languages. The Java programming language does not support the explicit use of addresses like other languages do. You use the variable's name instead.
or three operands. An operator that requires one operand is called a unary operator. For example, ++ is a unary operator that increments the value of its operand by 1. An operator that requires two operands is a binary operator . For example, = is a binary operator that assigns the value from its right-hand operand to its left-hand operand. And finally, a ternary operator is one that requires three operands. The Java programming language has one ternary operator, ?:, which is a short-hand if-else statement. • Arithmetic Operators The Java programming language supports various arithmetic operators for all floating-point and integer numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The following table summarizes the binary arithmetic operations in the Java programming language.
) O p e r a t io n ( s ) O r d e r o f e v a lu a t io n ( p r e c e d e n c e ) * / M u l t i p l i c a t i o n D i v i s i o n R e m a i n d e r E v a l u a t e d f i r s t . I f t h e r e a r e s e v e r a l o f t h i s t y p e o f o p e r a t o r , t h e y a r e e v a l u a t e d f r o m l e f t t o r i g h t . + - A d d i t i o n S u b t r a c t i o n E v a l u a t e d n e x t . I f t h e r e a r e s e v e r a l o f t h i s t y p e o f o p e r a t o r , t h e y a r e e v a l u a t e d f r o m l e f t t o r i g h t . F ig . 2 . 1 7 P r e c e d e n c e o f a r it h m e t ic o p e r a t o r s . Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: ( a + b + c ) / 3 Follows PEMDAS Parentheses, Exponents, Multiplication, Division, Addition, Subtraction
and two double-precision floating-point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses + to concatenate strings. The arithmetic operations are shown in boldface: public class ArithmeticDemo { public static void main(String[] args) { //a few numbers int i = 37; int j = 42; double x = 27.475; double y = 7.22; System.out.println("Variable values..."); System.out.println(" i = " + i); System.out.println(" j = " + j); System.out.println(" x = " + x); System.out.println(" y = " + y);
System.out.println(" i % j = " + (i % j)); System.out.println(" x % y = " + (x % y)); //mixing types System.out.println("Mixing types..."); System.out.println(" j + y = " + (j + y)); System.out.println(" i * x = " + (i * x)); } } The output from this program is: Variable values... i = 37 j = 42 x = 27.475 y = 7.22 Adding... i + j = 79 x + y = 34.695 Subtracting... i - j = -5 x - y = 20.255 Multiplying... i * j = 1554 x * y = 198.37
3.8054 Computing the remainder... i % j = 37 x % y = 5.815 Mixing types... j + y = 49.22 i * x = 1016.58 Note that when an integer and a floating-point number are used as operands to a single arithmetic operation, the result is floating point. The integer is implicitly converted to a floating-point number before the operation takes place. The following table summarizes the data type returned by the arithmetic operators, based on the data type of the operands. The necessary conversions take place before the operation is performed.
by 1; evaluates to the value of op before it was incremented ++ ++op Increments op by 1; evaluates to the value of op after it was incremented -- op-- Decrements op by 1; evaluates to the value of op before it was decremented -- --op Decrements op by 1; evaluates to the value of op after it was decremented
-- once. public class SortDemo { public static void main(String[] args) { int[ ] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = arrayOfInts.length; --i >= 0; ) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } } This program puts ten integer values into an array — a fixed-length structure that can hold multiple values of the same type — then sorts them. The boldface line of code declares an array referred to by arrayOfInts, creates the array, and puts ten integer values into it. The program uses arrayOfInts.length to get the number of elements in the array. Individual elements are accessed with this notation: arrayOfInts[index], where index is an integer indicating the position of the element within the array. Note that indices begin at 0. You’ll get more details and examples for arrays in the section Arrays . The output from this program is a list of numbers sorted from lowest to highest: 3 8 12 32 87 127 589 622 1076 2000
and determines the relationship between them. For example, != returns true if its two operands are unequal. The next table summarizes the relational operators: Standard algebraic equality or relational operator Java equality or relational operator Example of Java c ondition Meaning of Java c ondition Equality operators = == x == y x is equal to y != x != y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y ≥ >= x >= y x is greater than or equal to y ≤ <= x <= y x is less than or equal to y Fig. 2.19 Equality and relational operators.
numbers and uses the relational operators to compare them. The relational operations are shown in boldface: public class RelationalDemo { public static void main(String[] args) { //a few numbers int i = 37; int j = 42; int k = 42; System.out.println("Variable values..."); System.out.println(" i = " + i); System.out.println(" j = " + j); System.out.println(" k = " + k); //greater than System.out.println("Greater than..."); System.out.println(" i > j = " + (i > j)); //false System.out.println(" j > i = " + (j > i)); //true System.out.println(" k > j = " + (k > j)); //false; //they are equal //greater than or equal to System.out.println("Greater than or equal to..."); System.out.println(" i >= j = " + (i >= j)); //false System.out.println(" j >= i = " + (j >= i)); //true System.out.println(" k >= j = " + (k >= j)); //true
37 j = 42 k = 42 Greater than... i > j = false j > i = true k > j = false Greater than or equal to... i >= j = false j >= i = true k >= j = true Less than... i < j = true j < i = false k < j = false Less than or equal to... i <= j = true j <= i = false k <= j = true Equal to... i == j = false k == j = true Not equal to... i != j = true k != j = false
action only when condition is true Action/decision programming model You use if along with a condition to test, as in the following statement: if (account < 0.01) System.out.println("Hear that bouncing noise? It's your checks");
System.out.println("This elephant is too fat for your tightrope act."); if (elephantTotal > 12) cleaningExpense = cleaningExpense + 150; if (account <= 0) System.out.println("Hear that bouncing noise? It's your checks"); Equal and Not Equal Comparisons if (answer == rightAnswer) studentGrade = studentGrade + 10; if (studentGrade == 100) System.out.println("Congratulations -- a perfect score!"); if (team != "New York Jets") chanceToWin = 50; if (answer != rightAnswer) score = score - 5;
{ public static void main(String[ ] arguments) { int total = 0; int score = 7; if (score == 7) System.out.println("You score a touchdown!"); if (score == 3) System.out.println("You kick a field goal!"); total = total + score; System out.println("Total score: " + total); } }
Selection Statement Perform action only when condition is true Perform different specified action when condition is false Conditional operator (?:) Nested if…else selection structures
a condition is true and do something else if the condition is false. You can do this by using the else statement in addition to the if statement, as in the following example: if (answer == correctAnswer) { score += 10; System.out.println("That's right. You get 10 points."); } else { score -= 5; System.out.println("Sorry, that's wrong. You lose 5 points."); } The else statement does not have a condition listed alongside it, unlike the if statement. Generally, the else statement is matched with the if statement that immediately comes before it in a Java program. You also can use else to chain several if statements together, as in the following example: if (grade == "A") System.out.println("You got an A. Great job!"); else if (grade == "B") System.out.println("You got a B. Good work!"); else if (grade == "C") System.out.println("You got a C. You'll never get into a good college!"); else System.out.println("You got an F. You'll do well in Congress!");
the switch statement. You can use it in a Java program to test for a variety of different conditions and respond accordingly. In the following example, the grade example has been rewritten with the switch statement to handle a complicated range of choices: switch (grade) { case ‘A’: System.out.println("You got an A. Great job!"); break; case ‘B’: System.out.println("You got a B. Good work!"); break; case ‘C’: System.out.println("You got a C. You'll never get into a good college!"); break; default: System.out.println("You got an F. You'll do well in Congress!"); }
that you might not find reasons to use in your programs, the ternary operator. If you find it too confusing to implement in your own programs, take heart: You can use other conditionals to accomplish the same thing. You can use the ternary operator when you want to assign a value or display a value based on a conditional test. For example, in a video game, you might need to set the numberOfEnemies variable based on whether the variable is greater than 5. One way to do this is with an if-else statement: if (skillLevel > 5) numberOfEnemies = 10; else numberOfEnemies = 5; A shorter way to do this is to use the ternary operator, which is ?. A ternary operator has the following parts: The condition to test, surrounded by parentheses, as in (skillLevel > 5) A question mark (?) The value to use if the condition is true A colon (:) The value to use if the condition is false To use the ternary operator to set the value of numberOfEnemies based on skillLevel, you could use the following statement: numberOfEnemies = ( skillLevel > 5) ? 10 : 5;
functionality would be to evaluate age value and print the appropriate information listed below. If Age < 18 print "You're young - enjoy it! If not print "You're not under 18" If Age >= 18 and Age < 50 print "You're in the prime of your life“ if not print "You're not in the prime of your life\n"; If Age >= 50 print " print "You can retire soon - hurrah! “ if not print "print "You cannot retire soon :( “.
module functionality would be to evaluate age value and print the appropriate information listed below. Age < 10 print " print "You're under 10“ Age < 20 print "You're under 20“ Age < 30 print "You're under 30“ Age < 40 print "You're under 40“ Age more than 40 print "You're over 40"
module functionality would be to evaluate name value and print the appropriate information listed below. Name is Jim print " print "You're name is Jim“ Name is Bob print "You're name is Bob“ Name is Sally print "You're name is Sally“ Name is Linda print "You're name is Linda“ Else print “I don’t know you’re name"
functionality would be to evaluate a numeric value and print the appropriate information listed below. iValue = 1 set value = “Ace“ iValue = 10 set value = “Ten“ iValue = 11 set value = “Jack“ iValue = 12 set value = “Queen“ iValue = 13 set value = “King“ After evaluation print “ iValue = “ value variable.
functionality would be to evaluate grade value and print the appropriate information listed below. Grade = ‘A’ print “You got an A. Great job” Grade = ‘B’ print “You got a B. Good work!” Grade = ‘C’ print “You got a C. You'll never get into a good college!” Grade = ‘F’ print "You got an F. You'll do well in Congress!"
{ /** * Creates a new instance of <code>ClockTalk</code>. */ public ClockTalk() { } /** * @param args the command line arguments */ public static void main(String[] args) { // get current time and date GregorianCalendar now = new GregorianCalendar(); int hour = now.get(Calendar.HOUR); int minute = now.get(Calendar.MINUTE); int month = now.get(Calendar.MONTH) + 1; int day = now.get(Calendar.DATE); int year = now.get(Calendar.YEAR);
case (1): System.out.print("January"); break; case (2): System.out.print("February"); break; case (3): System.out.print("March"); break; case (4): System.out.print("April"); break; case (5): System.out.print("May"); break; case (6): System.out.print("June"); break; case (7): System.out.print("July"); break; case (8): System.out.print("August"); break;
punishments for schoolchildren is to make them write something over and over again on paper or, for a capital offense, on the chalkboard. In one of his frequent trips to the board, cartoon problem child Bart Simpson had to write "I will not trade pants with others" dozens of times. This kind of punishment might work on children, but it definitely would fail on a computer. They can repeat a task with ease. for Loops The most complex of the loop statements is for. The for loop is often used in cases where you want to repeat a section of a program for a fixed amount of times. It also can be used if the number of times the loop should be repeated is variable. The following is an example of a for loop: for (int number = 0; number < 1000; number++) { if (number % 12 == 0) System.out.println("#: " + number); }
loop condition that controls the execution of the loop statement. Unlike the for loop, the while loop has no initialization or step expressions. The syntax for the while statement follows: while (LoopCondition) Statement If the Boolean LoopCondition evaluates to true, the Statement is executed and the process starts over. It is important to understand that there is no step expression, as in a for loop. This means that the LoopCondition must somehow be affected by code in the Statement or the loop will infinitely repeat, which is a bad thing. This is bad because an infinite loop causes a program to never exit, which hogs processor time and can ultimately hang the system. Another important thing to notice about the while loop is that its LoopCondition occurs before the body of the loop Statement. This means that if the LoopCondition initially evaluates to false, the Statement never will be executed. This might seem trivial, but it is in fact the only thing that differentiates the while loop from the do- while loop.
look at Listing 13.15, which shows how a counting program works using a while loop. The WhileCount class. class WhileCount { public static void main (String args[ ]) { String strLine = ""; int numToCount; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number to count to between 0 and 10:"); try { strLine =in.readLine(); } catch (Exception e) { System.out.println("Error: " + e.toString()); } numToCount = Integer.parseInt(strLine); if ((numToCount > 0) && (numToCount < 11)) { int i = 1; while (i <= numToCount) { System.out.println(i); i++; } } else System.out.println("That number was not between 0 and 10!"); } }
loop, as you can see in the following syntax: do Statement while (LoopCondition); The major difference between the do-while loop and the while loop is that the LoopCondition is evaluated after the Statement is executed. This difference is important because there might be times when you want the Statement code to be executed at least once, regardless of the LoopCondition. The syntax for the do- while loop follows: The Statement is executed initially, and from then on it is executed as long as the LoopCondition evaluates to true. Like the while loop, you must be careful with the do-while loop to avoid creating an infinite loop. An infinite loop occurs when the LoopCondition remains true indefinitely.
do System.out.println(“I’m stuck!”); while (true); Because the LoopCondition is always true, the message I’m Stuck! is printed forever, or at least until you hit Ctrl+C and break out of the program. break and continue You’ve already seen how the break statement works in regard to the switch branch. The break statement is also useful when dealing with loops. You can use the break statement to jump out of a loop and effectively bypass the loop condition. Listing 13.16 shows how the break statement can help you out of the infinite loop problem shown earlier.