\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Philip Schmid (Higarigh)} \pdfinfo{ /Title (oov1.pdf) /Creator (Cheatography) /Author (Philip Schmid (Higarigh)) /Subject (OOv1 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{3363AB} \definecolor{LightBackground}{HTML}{F2F5F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{OOv1 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Philip Schmid (Higarigh)} via \textcolor{DarkBackground}{\uline{cheatography.com/20304/cs/3111/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Philip Schmid (Higarigh) \\ \uline{cheatography.com/higarigh} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 25th January, 2015.\\ Updated 12th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java basics}} \tn % Row 0 \SetRowColor{LightBackground} `x = a ? b : c` & a true? x = b, a false? x = c \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `0.1 + 0.1 == 0.3` & False, workaround: `Math.abs(0.1 + 0.1) \textless{} 0.2e6` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `a \&\& b` & Only check b if a is true \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `a || b` & Only check b if a is false \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} `0b11001` & binary, leading "0b" \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `0x1e` & hexadecimal, leading "0x" \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} `010 != 10` & Leading 0 means octal \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `'A' == 'a'` & False (case sensitive) \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} `'A' \textless{} 'B'` & True \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java primitive data types}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{boolean} & true, false \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} char & 16 bit, UTF-16 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} byte & 8 bit, -128...127 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} short & 16 bit, -32.768 ... 32.767 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} int & 32 bit, -2\textasciicircum{}31\textasciicircum{} to +2\textasciicircum{}31\textasciicircum{}-1 \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} long & 64 bit, -2\textasciicircum{}63\textasciicircum{} to +2\textasciicircum{}63\textasciicircum{}-1,`long x = 100l;` \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} float & 32 bit, `float x = 100f;` \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \seqsplit{double} & 64 bit, `double x = 100d;` \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java type casting}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/higarigh_1419424860_Cast_overview.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{red arrows =implizit (probably information loss due inaccurate dataformat) \newline black arrows = explizit cast (heavy information loss possible -{}-\textgreater{} developer)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java interfaces}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Methods in intefaces are implicitly public and abstract and can't be private.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Only constant variables are allowed: `public static final int HIGHWAY\_MIN\_SPEED = 60;` "public static final" is optional.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Same named methods in basic interface and super interface must have the same return type.} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Same named variables in basic interface and super interface can have different return types.} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Default methods: Basic interface doesn't have to implement default methods. If one method is not overriden, it just takes the default method.} \tn % Row Count 12 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java reference types}} \tn % Row 0 \SetRowColor{LightBackground} `int{[}{]} x = new int{[}10{]};` & normal Array (`public int{[}{]} x(int{[}{]} y)\{return z\}`) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `int{[}{]}{[}{]} m = new int{[}2{]}{[}3{]};` & 2 dimensional array \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} `Arrays.equals(a, b)` & Compare array content \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`Arrays.deepEquals(a}, b)` & Compare x dimensional array \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} `public enum Weekday\{ MONDAY, ..., SUNDAY \}` & Enum \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} `String a = "Prog1";` & new reference to String-Object "Prog1" (if already exists, otherwise create it) \tn % Row Count 16 (+ 4) % Row 6 \SetRowColor{LightBackground} `String c = new String("Prog1");` & new String-Object \tn % Row Count 18 (+ 2) % Row 7 \SetRowColor{white} `a.equals(b)` & Compare Strings \tn % Row Count 19 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`StringBuilderbuilder = new StringBuilder("Hi"); builder.append(" you!"); builder.insert(0, "XY"); String text= builder.toString();`} \tn % Row Count 22 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java equals() example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{@Override \newline public boolean equals(Object obj) \{ \newline ~ if (obj == null) \{ \newline ~~~~return false; \newline ~ \} else if (getClass() != obj.getClass()) \{ \newline ~~~~return false; \newline ~ \} else if (!super.equals(obj)) \{ \newline ~~~~return false; \newline ~ \} else \{ \newline ~~~~Student other = (Student)obj; return regNumber == other.regNumber; \newline ~ \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{If equals() is changed, hashCode() has also be changed! x.equals(y) -\textgreater{} x.hashCode() == y.hashCode()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.14471 cm} x{3.83229 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Keywords}} \tn % Row 0 \SetRowColor{LightBackground} public & can be seen by all that imports this package \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{protected} & can be seen by all classes in this package and all subclasses of this \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} package & can be seen by all classes in this package \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} private & can be seen only by this class \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} static & only once for all instances of this class \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} final & can only be defined once and not changed later. Class: no subclasses, Method: no overriding \tn % Row Count 14 (+ 4) % Row 6 \SetRowColor{LightBackground} static final & Means this is a constant \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.19696 cm} x{1.32733 cm} x{1.05271 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Javadoc}} \tn % Row 0 \SetRowColor{LightBackground} Start with /** & end with */ & each line * \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} @author name & author & class / \seqsplit{interface} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} @version number & version & class / \seqsplit{interface} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} @param name description & parameter & method \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} @return description & \seqsplit{returnvalue} & method \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} @throws/@exception type description & potential exception & method \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} @deprecated description & deprecated (outdated) & method \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java hashcode() example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public int hashCode() \{ \newline ~~~~return firstName.hashCode() + 31 * surName.hashCode(); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java compareTo example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Person implements Comparable\textless{}Person\textgreater{} \{ \newline ~ private String firstName, lastName; \newline ~ // Constructor… \newline ~ @Override \newline ~ public int compareTo(Person other) \{ \newline ~~~~ int c = compareStrings(lastName, other.lastName); \newline ~~~~ if (c != 0) \{ return c; \} \newline ~~~~ else \{ return compareStrings(firstName, other.firstName); \} \newline ~\} \newline private int compareStrings(String a, String b) \{ \newline ~ if (a == null) \{ return b == null ? 0 : 1; \} \newline ~ else \{ return a.compareTo(b); \} \newline ~~\} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java collections}} \tn % Row 0 \SetRowColor{LightBackground} `ArrayList\textless{}Object\textgreater{} a1 = new ArrayList\textless{}\textgreater{}();` & `get(int), set(int, "OO"), add("CN2"), remove(int), remove("CN1"), contains("CN1"), size()` \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} `LinkedList\textless{}Object\textgreater{} l1 = new LinkedList\textless{}\textgreater{}();` & `add("ICTh"), add(int, "Bsys1"), remove(int), remove("ICTh"), contains("ICTh")` \tn % Row Count 9 (+ 4) % Row 2 \SetRowColor{LightBackground} `Set\textless{}String\textgreater{} s1= new TreeSet\textless{}\textgreater{}();` or `Set\textless{}String\textgreater{} s2= new HashSet\textless{}\textgreater{}();` & `add("Test"), remove("Test"), size()` checks if already added -\textgreater{} if so, return false. TreeSet = sorted, always efficient. HashSet = unsorted, usually very efficient \tn % Row Count 18 (+ 9) % Row 3 \SetRowColor{white} `Map\textless{}Integer, Object\textgreater{} m1= new HashMap\textless{}\textgreater{}();` or `Map\textless{}Integer, Object\textgreater{} m2= new TreeMap\textless{}\textgreater{}();` & `get(key), put(key, "Test"), remove(key), containsKey(key), size(),` TreeMap = sorted by key, always efficient. HashMap = unsorted, usually very efficient \tn % Row Count 26 (+ 8) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`Iterator\textless{}String\textgreater{} it= m1.iterator(); while(it.hasNext()) \{..\}`} \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java inheritance}} \tn % Row 0 \SetRowColor{LightBackground} `Vehicle v1 = new Car();` & {\bf{Vehicle = static type, Car = dynamic type}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `Object o = new Vehicle(); Vehicle v = (Vehicle)o;` & Vaild -\textgreater{} Down-Cast: Static type can be down casted \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} `Vehicle v = new Vehicle; Car c = (Car)v;` & Error -\textgreater{} Dynamic type can't be down casted \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} `if(v instanceof Car) \{ Car c = (Car)v;\}` & Test if dynamic type matches static type \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} `super.variable` & Hiding: variable from super class \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{`((SuperSuperClass)this)}.variable` & Hiding: variable from "super.super" class \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Dynamic dispatch}}: Methods: from dynamic typ and variables from static type.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java Lambdas / Stream API}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`Collections.sort(people}, (p1, p2) -\textgreater{} p1.getAge() –p2.getAge());` & sort ascending by age \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \seqsplit{`Collections.sort(people}, (p1, p2) -\textgreater{} \seqsplit{p1.getLastName().compareTo(p2.getLastName()));`} & sort by lastname \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} \seqsplit{`people.stream().filter(p} -\textgreater{} p.getAge() \textgreater{}= 18) .map(p -\textgreater{} p.getLastName()) .sorted() \seqsplit{.forEach(System.out::println);`} & stream API example \tn % Row Count 15 (+ 6) % Row 3 \SetRowColor{white} `people.stream() .filter(p -\textgreater{} \seqsplit{p.getLastName().contains(pattern))} .forEach(System.out::println);` & `pattern` has to be final! \tn % Row Count 20 (+ 5) % Row 4 \SetRowColor{LightBackground} `Random random= newRandom(4711); \seqsplit{Stream.generate(random::nextInt)} .forEach(System.out::println);` & generate random stream \tn % Row Count 25 (+ 5) % Row 5 \SetRowColor{white} `List\textless{}Person\textgreater{} list= \seqsplit{peopleStream.collect(Collectors.toList());`} & stream to collection (List/Set/Map) \tn % Row Count 28 (+ 3) % Row 6 \SetRowColor{LightBackground} `Person{[}{]} array= peopleStream.toArray(length-\textgreater{} newPerson{[}length{]});` & stream to array \tn % Row Count 32 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Possible Stream API operations: `filter(Predicate), map(Function), mapToInt(Function), mapToDouble(Function), sorted(), distinct(), limit(long n), skip(long n), count(), min(), max(), average(), sum()`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Comparator \& Methodreference}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Inferface used to compare 2 Objects(before you used lamdas).} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Contains the method `public int compare(T o1, T o2)` which you need to override. Returns positiv number if o1 is bigger then o2 and negative if oppisite 0 means that they are equal} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Instead of a comparator use methodrefernce class:methodName eg \seqsplit{`PersonComp::compareName`}} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Nested class}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Use this if a class is only used in another class} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{No seperate classfile} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The inner class can use all members of the outer class (this include private members)} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Instantiation from outside eg `Polygon.Pointpoint= myPolygon.newPoint();`} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Can be declared in a method -\textgreater{} All variables from outside are getting final eg `Car getSuperCar() \{ class SuperCar extends Car \{ @Override public int getMaxSpeed() \{return 300; \} \} return newSuperCar();\}`} \tn % Row Count 11 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java own exception class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public class MyException extends Exception \{ \newline ~private static final long serialVersionUID = 1L; \newline ~public MyException(String msg) \{ \newline ~~ super(msg); \newline ~\} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java package import conflict order}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{1. own class (inc. nested class) \newline % Row Count 1 (+ 1) 2. single type imports -\textgreater{} `import p2.A;` \newline % Row Count 2 (+ 1) 3. type in own package -\textgreater{} `package p1; class X` \newline % Row Count 3 (+ 1) 4. Import on demand -\textgreater{} `import p2.*;`% Row Count 4 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java regex}} \tn % Row 0 \SetRowColor{LightBackground} `1*` & 0 to * \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `1+` & 1 to * \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `1\{2,5\}` & min 2 max 5 (11..11111) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `1\{2,\}` & min 2 max * (11, 111, etc.) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} `1\{3\}` & exactly 111 \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} `-?1` & -1 or 1, "-" is optional \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} `Mo|Di` & Or \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} `{[}a-z{]}` & any letter from a to z \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} `{[}a-zA-Z{]}` & any letter from a to z or A to Z \tn % Row Count 10 (+ 2) % Row 9 \SetRowColor{white} `\textbackslash{}s` & whitespace \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} `.` & anything except newline \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} `{[}\textasciicircum{}abc{]}` & anything except a, b or c \tn % Row Count 13 (+ 1) % Row 12 \SetRowColor{LightBackground} `\$` & end of string \tn % Row Count 14 (+ 1) % Row 13 \SetRowColor{white} `\textbackslash{}d` & any digit \tn % Row Count 15 (+ 1) % Row 14 \SetRowColor{LightBackground} `\textbackslash{}D` & not digit \tn % Row Count 16 (+ 1) % Row 15 \SetRowColor{white} `(?\textless{}Group1\textgreater{}REGEX)` & name capture group -\textgreater{} `String Part1 = \seqsplit{matcher.group("Group1");`} \tn % Row Count 19 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example: Check daytime: `({[}0-1{]}?{[}0-9{]}|2{[}0-3{]}):{[}0-5{]}{[}0-9{]}`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java regex code example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{String input = scanner.nextLine(); \newline Pattern pattern = Pattern.compile("({[}0-2{]}?{[}0-9{]}):({[}0-5{]}{[}0-9{]})"); \newline Matcher matcher = pattern.matcher(input); \newline if (matcher.matches()) \{ \newline ~~String hoursPart = matcher.group(1); \newline ~~String minutesPart = matcher.group(2); \newline ~~System.out.println(..); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java JUnit}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`assertEquals(expected}, actual)` & actual «equals» expected \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`assertSame(expected},actual)` & actual== expected (only reference comparation) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{`assertNotSame(expected}, actual)` & expected != actual (only reference comparation) \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{`assertTrue(condition)`} & condition \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{`assertFalse(condition)`} & !condition \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} `assertNull(value)` & value== null \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{`assertNotNull(value)`} & value!= null \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} `fail()` & everytime false \tn % Row Count 16 (+ 1) % Row 8 \SetRowColor{LightBackground} `@Test(timeout= 5000)` & set test timeout \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} `@Test(expected= \seqsplit{IllegalArgumentException}.class)` & expect exception, if exception is thrown, test passes \tn % Row Count 21 (+ 3) % Row 10 \SetRowColor{LightBackground} `@Before public void setUp() \{ … \}` & run this before each test \tn % Row Count 23 (+ 2) % Row 11 \SetRowColor{white} `@After public void tearDown() \{ … \}` & run this after each test \tn % Row Count 25 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java JUnit examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{@Test \newline publicvoidtestPrime\_2() \{ \newline ~~assertTrue("2 isprime", utils.isPrime(2)); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java generics}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Example: `class Node\textless{}T extends Number \& Serializable\textgreater{}\{ … \} Node\textless{}Integer\textgreater{} n1; // OK Node\textless{}Number\textgreater{} n1; // OK Node\textless{}String\textgreater{} n2; // ERROR` You can add different Interfaces with \& to ensure other functionality like serializable} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Wildcard type: `Node\textless{}?\textgreater{} undefinedNode; undefinedNode = new Node\textless{}Integer\textgreater{}(4); undefinedNode= new Node\textless{}String\textgreater{}("Hi!");` No read (`.getValue()`) and write (`.setValue(X)`) is allowed} \tn % Row Count 9 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{static variables with generics NOT allowed eg static T maxSpeed;} \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Generic Method: `public \textless{}T\textgreater{} T majority(T x, Ty, T z)\{ if(x.equals(y)) \{ returnx; \} if(x.equals(z)) \{ returnx; \} if(y.equals(z)) \{ return y; \} return null; \}` Call: `Double d = test.\textless{}Double\textgreater{}majority(1.0, 3.141, 1.0);` but also: `int i = majority(1,1,3);` called type inference(also possible to mix types of argument)} \tn % Row Count 18 (+ 7) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Rawtype: like you would insert Object -\textgreater{} you need to down cast the elements. e.g. `Node n; //without class n = new Node("Hi"); String s = (String)n.getValue();`} \tn % Row Count 22 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Serializable}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Is a marker interface (is empty, just says that this class supports it)} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Use it to say the developer that he can serialize objects of this class, which means he can write then in a bitecode and export them. Always serialize all the objects contained in the mainobject} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Use serialVersionUID to identify your class (normally generated number) `private static final long serialVersionUID= -6583929648459736324L;`} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Example:`OutputStream fos= new \seqsplit{FileOutputStream("serial.bin")} try (ObjectOutputStream stream = new ObjectOutputStream(fos)) \{ \seqsplit{stream.writeObject(person);} \}`} \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Example:`InputStream fis= new \seqsplit{FileInputStream("serial.bin")} try (ObjectInputStream stream = new ObjectInputStream(fis)) \{ Person p = \seqsplit{(Person)stream.readObject();} … \}`} \tn % Row Count 17 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java clone() method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public Department clone() throws Exception \{ \newline ~Department d = new Department(); \newline ~d.name = name; \newline ~d.people = people; \newline ~d.subDepartments = new ArrayList\textless{}\textgreater{}(); \newline ~for (Department subD : subDepartments) \{ \newline ~~d.subDepartments.add(subD.clone()); \newline \} \newline ~return d; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}