Introduction to Java - PowerPoint PPT Presentation

introduction to java n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Java PowerPoint Presentation
Download Presentation
Introduction to Java

play fullscreen
1 / 54
Introduction to Java
116 Views
Download Presentation
keitha
Download Presentation

Introduction to Java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Introduction to Java How to use Eclipse How to display output

  2. Start Eclipse and select a workspace

  3. Select File->New->Project…

  4. Select Java Project, click Next >

  5. Name the project, click Finish

  6. Right click on the src folder

  7. Select new->Class

  8. Name the class, check the box if you want a main() method, click Finish

  9. Welcome class skeleton code

  10. Write your code inside main()

  11. The IDE will show choices at each step as you type to help you

  12. The IDE will indicate problems using various icons in the left margin

  13. Placing the mouse cursor over the icon will show a dialog with information

  14. The IDE compiles automagically, so if there are no errors, you can click the “run” icon and execute your code.

  15. Console Output • public class Welcome • { • public static void main(String[] args) • { • // Display message to the console • System.out.println("Welcome to Java!"); • System.out.println((10. 5 + 2 * 3) / (45 – 3. 5)); • } • } • Welcome to Java! • 0. 39759036144578314

  16. Dialog box output import javax.swing.JOptionPane; public class Welcome { public static void main(String[] args) { // Display message in a dialog box JOptionPane.showMessageDialog(null, "Welcome to Java!"); } }

  17. Another dialog box • import javax.swing.JOptionPane; • public class Welcome • { • public static void main(String[] args) • { • // Display message in a dialog box • JOptionPane.showConfirmDialog(null, "Give your user options", "Choice is good!", JOptionPane.OK_CANCEL_OPTION); • } • }

  18. Chapter 1 Notes • // makes the rest of the line a comment • /* comment */ is an another comment format • Java statements always end with a ‘;’ • Every class can have a main() method • Java source files have a .java extension • Java complied files have a .class extension • Import statements can use ‘*’ wildcard

  19. Chapter 2 More about using Eclipse How to read input and do basic operations

  20. Strings cannot break across linesUse the ‘+’ operator to concatenate public class Welcome { public static void main(String[] args) { System.out.println("If your string is too long " + "you need to concatenate it"); } } > If your string is too long you need to concatenate it

  21. Reading input from the console:Using the Scanner class(with a brief detour)

  22. When we begin typing the code to use the Scanner class, an error icon appears in the left margin.What’s the problem?

  23. Click the icon to import the class,double click Import ‘Scanner’

  24. The import statement is inserted at the top of the file

  25. The Scanner class can be used to read keyboard input

  26. In the IDE, console input and output occurs in the “Console” window at the bottom of the screen

  27. Scanner methods • nextByte() // reads an integer as a byte • nextShort() // reads an integer as a short • nextInt() // reads an integer as an int • nextLong() // reads an integer as a long • nextFloat() // reads a number as a float • nextDouble() // reads a number as a double • next() // reads up to whitespace • nextLine() // reads a line of text

  28. What happens if there is bad input?

  29. Java throws an exception!There are many different exceptions to indicate all kinds of program errorsUnhandled exceptions kill programs!

  30. Scanner.next() and Scanner.nextLine()

  31. nextLine() reads the whole line,next() reads the next “word”

  32. Identifiers • Identifiers are names used for variables, constants, methods, classes, and packages. • Must start with a letter, ‘_’, or ‘$’ character • Additional characters can be letters, ‘_’, ‘$’, and digits. • Cannot be a reserved word • Cannot be “true,” “false,” or “null” • Identifiers can be any length

  33. Examples of valid identifiers • x • firstName • tank_volume • $cursor • _main • $123 • While • USC_1

  34. Examples of invalid identifiers • 1forthemoney // begins with a digit • while // reserved word • false // true, false, null • yes#no // illegal character • Here^ // illegal character • const // reserved word • .options // ‘.’ is an operator • First-name // illegal character

  35. Variables • Declaration: datatype name; • Multiple declarations can be separated with commas on the same line: int a, b, c; • Can be initialized: int count = 0; • Combined form: int row = 0, column = 1; • Assignment chaining: a = b = c = d; assigns the value of d to a, b, and c.

  36. Constants • The value of constants do not change during program execution • The final keyword is used to declare constants: final datatype NAME = value; • Named constants are capitalized • Constants must be initialized in the declaration (since they cannot be changed…) • Using named constants is a very good idea

  37. Numeric Data Types • byte // -128 to 127 • short // -32768 to 32767 • int // -231 to 231 - 1 • long // -263 to 263 – 1 • float // +/- (1.4 x 10-45 to 3.4 x 1038) • double // +/- (4.9 x 10-324 to 1.8 x 10308)

  38. Numeric Operators • + Addition // 2 + 2 equals 4 • - Subtraction // 7 – 6 equals 1 • * Multiplication // 5 * 4 equals 20 • / Division // 5.0 / 2 equals 2.5 • / Integer division // 5 / 2 equals 2 • % Modulus // 5 % 2 equals 1

  39. Integer Literals • Assumed to be an int by default • Put an ‘L’ on the end to declare a long value • For example, 9876543210 is too big for an int, so it must be declared as 9876543210L. • A leading ‘0’ declares an octal value (0632) • A leading ‘0x’ declares a hexadecimal value (0x4A6F)

  40. Floating point literals • Floating point numbers are denoted with a decimal point (‘.’). • Floating point literals are double by default • Append an ‘F’ or ‘f’ to declare a float value: 5.47F or 5.47f • Append a ‘D’ or ‘d’ to declare a double value: 7.83D or 7.83d

  41. Scientific Notation • Scientific notation uses an ‘E’ or ‘e’ to denote the exponent. • For example, 5.72E4 is 57200 • By convention, numbers in scientific notation have one non-zero digit to the left of the decimal point: 5.72e4 • Not 57.2e3 or 0.572e5

  42. Shorthand Operators • += // j += 4; same as j = j + 4; • -= // z -= diff; same as z = z – diff; • *= // x *= 2; same as x = x * 2; • /= // wd /= 2; same as wd = wd / 2; • %= // m %= 3; same as m = m % 3;

  43. Increment and decrement operators • Prefix versions increment/decrement the value before it is evaluated in an expression • ++var; // incremented before • --var; // decremented before • Postfix version increment/decrement the value after it is evaluated in an expression • var++; // incremented after • var--; // decremented after

  44. How do they work?

  45. Numeric Type Conversions • Numeric values can be cast to different types • Format: (type)value; • Type can be byte, short, int, long, float, double • Type is surrounded by parentheses • Value can be a numeric literal or a variable • Type casting can be important to make mathematical operations work correctly

  46. Type conversion in action

  47. Characters and Strings • Single characters are surrounded by single quotes: char letter = ‘A’; • String literals are surrounded by double quotes: “Java is easy” • Java characters are 16-bit Unicode values • Characters can be specified using the ‘\u’ escape character followed by a hexadecimal value: ‘\u6B22’

  48. Example of a special Unicode character

  49. Escape sequences • \b backspace • \t tab • \n linefeed • \f formfeed • \r carriage return • \\ backslash • \’ single quote • \” double quote