DOC PREVIEW
UW CSE 142 - Study Guide

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

DZone, Inc. | www.dzone.com tech facts at your fingertipsCONTENTS INCLUDE:n Java Keywords n Standard Java Packagesn Character Escape Sequences n Collections and Common Algorithms n Regular Expressionsn JAR FilesThis refcard gives you an overview of key aspects of the Java language and cheat sheets on the core library (formatted output, collections, regular expressions, logging, properties) as well as the most commonly used tools (javac, java, jar).Java Keywords, continuedAbOUT CORE JAVAJAVA KEywORDSCore Java www.dzone.com Subscribe Now for FREE! refcardz.com n Authoritative contentn Designed for developersn Written by top expertsn Latest tools & technologiesn Hot tips & examplesn Bonus content onlinen New issue every 1-2 weeksSubscribe Now for FREE!Refcardz.comGet More Refcardz(They’re free!)Core JavaBy Cay S. Horstmann→Keyword Description Exampleabstractan abstract class or methodabstract class Writable { public abstract void write(Writer out); public void save(String filename) { ... }}assertwith assertions enabled, throws an error if condition not fulfilledassert param != null;Note: Run with -ea to enable assertionsbooleanthe Boolean type with values true and falseboolean more = false;breakbreaks out of a switch or loopwhile ((ch = in.next()) != -1) { if (ch == '\n') break; process(ch);}Note: Also see switchbytethe 8-bit integer typebyte b = -1; // Not the same as 0xFFNote: Be careful with bytes < 0casea case of a switch see switchcatchthe clause of a try block catching an exceptionsee trycharthe Unicode character typechar input = 'Q';classdefines a class typeclass Person { private String name; public Person(String aName) { name = aName; } public void print() { System.out.println(name); }}constnot usedcontinuecontinues at the end of a loopwhile ((ch = in.next()) != -1) { if (ch == ' ') continue; process(ch);}defaultthe default clause of a switchsee switchdothe top of a do/while loopdo { ch = in.next();} while (ch == ' ');doublethe double-precision floating-number typedouble oneHalf = 0.5;elsethe else clause of an if statementsee ifenuman enumerated typeenum Mood { SAD, HAPPY };extendsdefines the parent class of a classclass Student extends Person { private int id; public Student(String name, int anId) { ... } public void print() { ... }}finala constant, or a class or method that cannot be overriddenpublic static final int DEFAULT_ID = 0;Keyword Description Examplefinallythe part of a try block that is always executedsee tryfloatthe single-precision floating-point typefloat oneHalf = 0.5F;fora loop typefor (int i = 10; i >= 0; i--) System.out.println(i);for (String s : line.split("\\s+")) System.out.println(s);Note: In the “generalized” for loop, the expression after the : must be an array or an Iterablegotonot usedifa conditional statementif (input == 'Q') System.exit(0);else more = true;implementsdefines the interface(s) that a class implementsclass Student implements Printable { ...}importimports a packageimport java.util.ArrayList;import com.dzone.refcardz.*;instanceof tests if an object is an instance of a classif (fred instanceof Student) value = ((Student) fred).getId();Note: null instanceof T is always falseintthe 32-bit integer typeint value = 0;interfacean abstract type with methods that a class can implementinterface Printable { void print();}longthe 64-bit long integer typelong worldPopulation = 6710044745L;nativea method implemented by the host systemnewallocates a new object or arrayPerson fred = new Person("Fred");nulla null referencePerson optional = null; packagea package of classespackage com.dzone.refcardz;privatea feature that is accessible only by methods of this classsee classprotecteda feature that is accessible only by methods of this class, its children, and other classes in the same packageclass Student { protected int id; ...}Core Java2DZone, Inc. | www.dzone.com tech facts at your fingertipsJava Keywords, continuedKeyword Description Examplepublica feature that is accessible by methods of all classessee classreturnreturns from a methodint getId() { return id; }shortthe 16-bit integer typeshort skirtLength = 24;statica feature that is unique to its class, not to objects of its classpublic class WriteUtil { public static void write(Writable[] ws, String filename); public static final String DEFAULT_EXT = ".dat";}strictfpUse strict rules for floating-point computationssuperinvoke a superclass constructor or methodpublic Student(String name, int anId) { super(name); id = anId;}public void print() { super.print(); System.out.println(id);}switcha selection statementswitch (ch) { case 'Q': case 'q': more = false; break; case ' '; break; default: process(ch); break; }Note: If you omit a break, processing continues with the next case.synchronizeda method or code block that is atomic to a threadpublic synchronized void addGrade(String gr) { grades.add(gr);}thisthe implicit argument of a method, or a constructor of this classpublic Student(String id) {this.id = id;}public Student() { this(""); }throwthrows an exceptionif (param == null) throw new IllegalArgumentException();throwsthe exceptions that a method can throwpublic void print() throws PrinterException, IOExceptiontransientmarks data that should not be persistentclass Student { private transient Data cachedData; ...}trya block of code that traps exceptionstry { try { fred.print(out); } catch (PrinterException ex) { ex.printStackTrace(); }} finally { out.close();}voiddenotes a method that returns no valuepublic void print() { ... }volatileensures that a field is coherently accessed by multiple threadsclass Student {private volatile int nextId;...}whilea loopwhile (in.hasNext()) process(in.next());OpERATOR pRECEDENCEOperators with the same precedenceNotes [] . () (method call) Left to right! ~ ++ -- + (unary) – (unary) () (cast) newRight to left ~ flips each bit of a number* / %Left to right Be careful when using % with negative numbers. -a % b == -(a % b), but a % -b == a % b. For example, -7 % 4 == -3, 7 % -4 == 3.+ -Left to right<< >> >>>Left to right >> is arithmetic shift (n >> 1 == n / 2 for positive and negative numbers), >>> is logical shift (adding 0 to the highest bits). The right hand side is reduced modulo 32 if the left hand side is an int or modulo 64 if the left hand side is a long. For example, 1 << 35 == 1 << 3.< <= > >= instanceofLeft to right null instanceof T is


View Full Document

UW CSE 142 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?