540 likes | 681 Views
Introduction to Java. How to use Eclipse How to display output. Start Eclipse and select a workspace. Select File->New->Project…. Select Java Project, click Next >. Name the project, click Finish. Right click on the src folder. Select new->Class.
E N D
Introduction to Java How to use Eclipse How to display output
Name the class, check the box if you want a main() method, click Finish
The IDE will show choices at each step as you type to help you
The IDE will indicate problems using various icons in the left margin
Placing the mouse cursor over the icon will show a dialog with information
The IDE compiles automagically, so if there are no errors, you can click the “run” icon and execute your code.
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
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!"); } }
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); • } • }
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
Chapter 2 More about using Eclipse How to read input and do basic operations
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
Reading input from the console:Using the Scanner class(with a brief detour)
When we begin typing the code to use the Scanner class, an error icon appears in the left margin.What’s the problem?
Click the icon to import the class,double click Import ‘Scanner’
In the IDE, console input and output occurs in the “Console” window at the bottom of the screen
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
Java throws an exception!There are many different exceptions to indicate all kinds of program errorsUnhandled exceptions kill programs!
nextLine() reads the whole line,next() reads the next “word”
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
Examples of valid identifiers • x • firstName • tank_volume • $cursor • _main • $123 • While • USC_1
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
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.
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
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)
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
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)
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
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
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;
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
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
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’
Escape sequences • \b backspace • \t tab • \n linefeed • \f formfeed • \r carriage return • \\ backslash • \’ single quote • \” double quote