You are on page 1of 22

Java Libraries

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

You can either import a single class


import javax.swing.JOptionPane

Or, you can import all classes in a package


import javax.swing.*

But, you cant import sub folders


import javax.* wrong!!!

The wildcard can only be used for classes, not packages!

Imports
Technically, you dont need imports. Option 1:

import java.text.*; DecimalFormat formatter = new DecimalFormat(0.00);

Option 2:
java.text.DecimalFormat formatter = new java.text.DecimalFormat(0.00);

Obviously, 1 saves you some typing

Java Methods

Every method has a signature that consists of the following


Method type (public, private) static (or not) Return type Method name Input parameters

Static Methods and Fields


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

the java.util package

An instance of this class is used to generate a stream of pseudorandom numbers

//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)

Binary logical operators are short-circuited


if (true || a==b) //a==b not evaluted if (false && a==b) //a==b not evaluted if (a != null && a.getValue().equals(b))

!true == false !false == true

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

What is the result and which are not evaluated?

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

If with an else statement

If (user.groupName.equals(Group.ADMIN) { authorizeUser(user.userName); } else { role == null; }

//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(); }

No braces needed for single line

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

A case statement that only works for int or char values

switch (value) { case 1: doSomething(); break; case 2: doSomethingElse(); break; default: doWhatever(); }
Dont forget to break, or the code will continue to run!

You might also like