Professional Documents
Culture Documents
Java Libraries
The expanse of tools included with Java They are all documented online:
http://java.sun.com/j2se/1.4.1/docs/api/index.html
Some examples
String Date JOptionPane
Java Package
A group of classes that are related in a logical manner that have been packaged (bundled) together Named in lowercase and imply a folder structure (java.awt.font = java/awt/font) Done to avoid conflicts when you have two classes with the same name
import statement
Used to import packages into your code java.lang is automatically imported, allowing you to use
objects like System and String wrapper classes like Float and Double which include methods (unlike their primitive types)
Wildcarding imports
Imports
Technically, you dont need imports. Option 1:
Option 2:
java.text.DecimalFormat formatter = new java.text.DecimalFormat(0.00);
Java Methods
Use the class name to call the method directly instead of creating an object and calling the method by object name JOptionPanes showMessageDialog() and showInputDialog() are good examples JOptionPane also has static fields for an enumeration of icons used (JOptionPane.ERROR_MESSAGE shows a stop sign on the dialog box) Another example is Math.PI Note, static fields use caps separated by underscore
Java objects
To use non-static methods, you must first instantiate an object Instantiation allocates memory and associates the properties & methods with the object Example: doLecture() can only be performed by a real ITTInstructor, not an ITTInstructor class, but SCHOOL_NAME can be static, since it is always ITT.
Constructor Methods
To instantiate an object you must call the classs constructor method using new Instructor rDobda = new Instructor(); The constructor method is a public method in the class with the same name as the class and it has no return value (but it does not use void). You can have many constructor methods for a single class (overloading), but the default constructor has no inputs. Take a look at the String class!!!
**DecimalFormat class**
Part of the java.text package Allows you to format decimal numbers like currency, percentages, etc DecimalFormat formatter = new DecimalFormat(String pattern) The pattern may include 0 if you want to show a zero or # if you want to leave it out Examples:
$#,###,###.00 for currency to include commas 0.0 will show at least 1 digit to the left and exactly one digit to the right.
**Random class**
In
//use the current time as a seed Random generator = new Random(); //generate random number from 0 to 4 int randomIndex = generator.nextInt( 5 );
Flow Control
if statements unary & binary operators switch statements
If-then conditional
if (boolean_value) { //do something } If (dealId == null) { log.info(deal cannot be null); return ERROR; }
Logical operators
&& and binary operator (2 arguments) || or binary operator (2 arguments) ! not unary operator (1 argument)
Note that a.getValue() would throw a null pointer exception if not for short-circuiting
Relational Operators
> greater-than >= greater-than or equal to (NOT =>) <= less-than or equal to (NOT =<) < less-than == equal to (not =) != not equal to
Logic questions
1.
2.
3. 4. 5. 6. 7. 8. 9.
((7 > 3)||(4 != 4)) && ((3 > 6)||(3 > 1)) ((3 == 6) && ((12 > 1) || 7 <= 42)) ((4 <= 4) || (2>3) )) || (3==3)) ((7 <= 8) && (3 ==3)) ((4==4) || (7==3) || (3==7)) (true && false || true && true) ((3+1*2==8) || (12-6*2==12)) (((3+1)*2==8) || ((12-6)*2==12)) (true || false && false)
if-then-else
//using else if
if (hairCondition.equals(Hair.WET)) { useBlowdryer(); } else if (hairCondition.equals(Hair.OILY) { washHair(); } else if (hairCondition.equals(Hair.LONG) { getHaircut; } else { thinkAboutSomethingElse(); }
If executing only a single line of code after an if statement, you dont need braces But they are still reocommended.
if (hairCondition.equals(Hair.WET)) useBlowdryer(); else if (hairCondition.equals(Hair.OILY) washHair(); else if (hairCondition.equals(Hair.LONG) getHaircut; else thinkAboutSomethingElse();
Ternary operator
One-liner for an if-then-else statement condition ? trueAction : falseAction;
(age > 99) ? youAreOld() : youAreAKid(); boolean ofAge = (age >= 21) ? letIntoClub() : slamDoor();
switch
switch (value) { case 1: doSomething(); break; case 2: doSomethingElse(); break; default: doWhatever(); }
Dont forget to break, or the code will continue to run!