Overview of the Java Programming Language 2011 edition




![Hello World in Java class Hello { public static void main(String[] args) { System. Hello World in Java class Hello { public static void main(String[] args) { System.](https://slidetodoc.com/presentation_image_h2/6a444c2ffd2efd849621a4fcc72f1e31/image-5.jpg)














![Arrays int [ ] x = new int[20]; x[5] = 7; n=40; String [ Arrays int [ ] x = new int[20]; x[5] = 7; n=40; String [](https://slidetodoc.com/presentation_image_h2/6a444c2ffd2efd849621a4fcc72f1e31/image-20.jpg)


![Expressions: Operator Precedence Priority Operator Description 1 () []. 2 ++ -+ ! ~ Expressions: Operator Precedence Priority Operator Description 1 () []. 2 ++ -+ ! ~](https://slidetodoc.com/presentation_image_h2/6a444c2ffd2efd849621a4fcc72f1e31/image-23.jpg)























- Slides: 46

Overview of the Java Programming Language (2011 edition)

If you know C you already know the basics of Java

Common Programming Language Features • • • Comments Data Types Variable Declarations Expressions Flow of Control Statements Functions and Subroutines Macros and Preprocessor Commands I/O Statements Libraries Compiler Directives External Tools (Compiler, debugger etc)

Comments in Java • The ability to comment is the most important feature in any programming language!!! • Comments should precede any block of code or any code that might be difficult to understand. A comment should describe the intent of what you are trying to do. • Write your comments BEFORE you write your code. Do not rely on the code itself to document what you are doing as the code may be incorrect • Comments may also be used to temporarily remove a block of code from your program //* Javadoc comments */ • Special comments used to generate help files //TODO • Special comments used in Net. Beans to keep track of unfinished tasks and problems /* This is a comment it can span many lines */ //Single line comments
![Hello World in Java class Hello public static void mainString args System Hello World in Java class Hello { public static void main(String[] args) { System.](https://slidetodoc.com/presentation_image_h2/6a444c2ffd2efd849621a4fcc72f1e31/image-5.jpg)
Hello World in Java class Hello { public static void main(String[] args) { System. out. println(“Welcome to Java”); } }

Why Java? Java enables users to develop and deploy applications on the Internet on multiple platforms: servers, desktop computers, hand-held and embedded devices such as dvd and blue-ray players, cell phones, RFID devices etc, regardless of the underlying processor or operating system. FJava is a general purpose programming language. FJava is the Internet programming language. FWrite once, deploy everywhere 6

Characteristics of Java • • • Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic www. cs. armstrong. edu/liang/intro 8 e/Java. Characteristics. pdf 7

JDK Versions • • JDK 1. 02 (1995) JDK 1. 1 (1996) JDK 1. 2 (1998) JDK 1. 3 (2000) JDK 1. 4 (2002) JDK 1. 5 (2004) a. k. a. JDK 5 or Java 5 JDK 1. 6 (2006) a. k. a. JDK 6 or Java 6 JDK 1. 7 (possibly 2010) a. k. a. JDK 7 or Java 7 8

JDK Editions • Java Standard Edition (J 2 SE) – J 2 SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J 2 EE) – J 2 EE can be used to develop server-side applications such as Java servlets and Java Server. Pages. • Java Micro Edition (J 2 ME). – J 2 ME can be used to develop applications for mobile devices such as cell phones. The Course Text uses J 2 SE to introduce Java programming. 9

Compiling Java Source Code Unlike compiled programs written in languages such as C, C++ or Assembler, Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine, as shown below. Java Virtual Machine is a software that interprets Java bytecode. 10

Looking at Java as a Language Remember – the syntax is like

Primitive Data Types • boolean, char, byte, int, short, long, float and double.

Ordinal Constants in Java • char : ‘A’ • int: -200, – – – 1000000 L (unsigned) 127 0 x 80 FA (hex), 007 (octal) ‘u 0811’ (unicode) • range of values depends on word size of the machine. Usually 4 bytes for an int. “Ordinal” means “countable” – there is usually a next and previous value.

boolean data types boolean found=false; String password=“Swordfish”; found=(my. Input. equals(“password”); if(found==true) System. out. println(“Yes we found it!”) else System. out. println(“No we did not”);

Special Character Constants • • • ‘n’ - newline ‘r’ - carriage return ‘t’ - tab ‘\’ - backslash ‘’’ - quoted apostrophe ‘”’ –quoted quote ‘ ’ – null character ‘a’ – audible alert ‘b’ - backspace

Non-Ordinal Constants • float : 12. 5 E-12. -0. 5 • double: 12. 5 E+200 always use doubles

Numerical Data Types 17

Non-Primitive Data Types contain multiple values • arrays • classes (start with the idea of C’s structs) • Collections

Declaring Scalar Variables • • short cake; int i; unsigned int value; long face; float icecream; double mint; boolean result;
![Arrays int x new int20 x5 7 n40 String Arrays int [ ] x = new int[20]; x[5] = 7; n=40; String [](https://slidetodoc.com/presentation_image_h2/6a444c2ffd2efd849621a4fcc72f1e31/image-20.jpg)
Arrays int [ ] x = new int[20]; x[5] = 7; n=40; String [ ] names; names=new String[n]; //2 Dimensional Arrays Vehicle[ ][ ] parking. Lot=new Vehicle[40][25];

Operators in Java • Assignment: = • Arithmetic : * / + - ++ -- % • Logical: && • Relational: > < • Bitwise: & | ^ << >> ~ • String: + • Grouping: () [] , • Triadic: (cond) ? value 1 : value 2 || ! == <>

Operators in C • Operators can be binary or unary. • ? : is a special triadic operator. • Any binary operator can be combined with the assignment operator, ie: a*=3; is the same as a=a*3; • Assignment isn’t special – its an operator like any other a=b+(c=7);
![Expressions Operator Precedence Priority Operator Description 1 2 Expressions: Operator Precedence Priority Operator Description 1 () []. 2 ++ -+ ! ~](https://slidetodoc.com/presentation_image_h2/6a444c2ffd2efd849621a4fcc72f1e31/image-23.jpg)
Expressions: Operator Precedence Priority Operator Description 1 () []. 2 ++ -+ ! ~ (type) Unary pre/post increment/ decrement Unary plus/minus Unary logical negation/bitwise complement Unary cast (change type) Dereference Address of size in bytes 3 * / % times/divide/mod 4 + - 5 << >> bitwise left shift, bitwise right shift 6 < <= > >= less than, less than or equal to greater than, greater than or equal 2 7 Parentheses (grouping) Brackets (array subscript) Member selection via pointer plus, minus != equal to, not equal to 8 & bitwise and 9 ^ bitwise xor 10 | bitwise or 11 && || 12 = += -= *= /= %= &= |= ^= <<== >>= 13 , == logical and, logical or (short circuit operators Arithmetic Assignment Bitwise assignment comma: sequence operator

Expressions: Operator Precedence • When in doubt use brackets () • If you know the correct operator precedence, but aren’t so sure others will know it as well – use brackets () • use brackets to make your meaning clear ( : - ) • did I mention you should use () ?

Expressions: Type Precedence • float + double • float + int float • short * long • char + int • address + int address When 2 operands are of different types, the result is the larger or more complicated type When in doubt of the result, use the cast operator result = (float) (my. Double + my. Int);

Expressions: Type Precedence Remember: x = 1/4; /* x 0 */ x = 1. 0/7; /* x . 25*/ C does very little type checking!!

Flow of Control: IF stmt if(condition) stmt; else stmt; if(x>10) System. out. print(“Too Big”); else System. out. print(“Value OK”);

Flow of Control: if stmt if(x>10 && x<20) { /* block of code */ } else { /* another block of code */ }

Secret Slide The triadic operator is a shorter form of if/then/else examples: if(x>10) y=2; else y=0; y= (x>10) ? 2 : 0 if(x>10) System. out. println(”X is big”); else System. out. println(”X is small”); System. out. println(x>10 ? “X is big” : “x is small”); if(a>b) return a; else return b; return (a>b) ? a : b;

Flow of Control: switch/case switch(ordinal. Value) { case ‘A’: case ‘a’: puts(“A chosen”); break; case 2: puts(“# 2 choice”); break; default: puts(“None of the above”); }

Flow of Control: while(condition) { int local. Value; do. Stuff(); } Variables can be declared at the start of any block of code {}

Forever loop: while(true) { int local. Value; do. Stuff(); }

Flow of Control: do while do { do. Stuff(); } while(condition); This kind of loop is always executed at least once. The test is at the end of the loop

Flow of Control: for loops for(initialization; condition; increment) { do. Stuff(); . . . } initialization do. Stuff Test increment

The break and continue statements for(int i=0; i<10; i+=2) { for(init; cond; incr) {. . some code. . . if(condition 1) break; . . . if(condition 2) continue; } } break: exits the inner loop continue: jump to the end of the inner loop and loops around again

Flow of Control: For Loops (cont’d) for(int i=0; i<10; i++) {} for(; i; i--) {} for(; condition; ) {} for(; ; ) {}

Flow of Control: Labels and the dreaded Goto • You probably were not taught about the goto statement • If you were, you were told that it was bad • use break, continue, exit(n) instead • Use it only in emergencies. .

Flow of Control: An example of goto for (. . . ) for(. . . ) { if(error. Cond) goto error. Label; } error. Label: fprintf(stderr, ”Bad mojo – quitting program”);

I/O Java has no I/O commands. All I/O is performed using library functions

Output to the Console //Unformatted Output System. out. println(value 1+val 2+val 3); //Formatted I/O String name="John Smith"; int age=20; System. out. printf(“Name: %-20 s: Age: %4 dn” , name, age);

Frequently-Used Specifiers Specifier Output Example %b true or false %c a boolean value a character 'a' %d a decimal integer 200 %f a floating-point number 45. 460000 %e a number in standard scientific notation %s a string 4. 556000 e+01 "Java is cool" 41

Input from the Console String name; int age=20; //Wrap a Scanner around the input console Scanner my. Scanner=new Scanner(System. in); //Input a string System. out. print("Name: "); //Prompt name=my. Scanner. next. Line(); //Input an integer System. out. print("Age: "); //Prompt age=my. Scanner. next. Int(); System. out. printf("Name: %-20 s: Age: %4 dn", name, age);

Input from a file String name; int age=20; //Wrap a Scanner around the input console Scanner my. Scanner=new Scanner(new File(“c: \my. Data. txt”)); //Input a whole line as a String name=my. Scanner. next. Line(); //Input an integer age=my. Scanner. next. Int(); System. out. printf("Name: %-20 s: Age: %4 dn", name, age);

Input from a web site on the Internet String name; int age; //This looks complicated – but only at first. All it is doing is creating a connection //to a remote file! Scanner my. Scanner=new Scanner( (new URL("http: //munro. humber. ca/~king/abc. txt")). open. Connection(). get. Input. Stream()); //The rest is exactly the same name=my. Scanner. next. Line(); age=my. Scanner. next. Int(); System. out. printf("Name: %-20 s: Age: %4 dn", name, age);

Getting Input from Input Dialog Boxes String input = JOption. Pane. show. Input. Dialog( "Enter an input");

(GUI) Confirmation Dialogs int option = JOption. Pane. show. Confirm. Dialog (null, "Continue"); 46