\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{Jianmin Feng (taotao)} \pdfinfo{ /Title (java-methods-and-encapsulations.pdf) /Creator (Cheatography) /Author (Jianmin Feng (taotao)) /Subject (Java Methods and encapsulations 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}{136310} \definecolor{LightBackground}{HTML}{F7FAF7} \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{Java Methods and encapsulations Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jianmin Feng (taotao)} via \textcolor{DarkBackground}{\uline{cheatography.com/79308/cs/19357/}}} \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}Jianmin Feng (taotao) \\ \uline{cheatography.com/taotao} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 8th May, 2019.\\ 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.34379 cm} x{3.63321 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Accessors}} \tn % Row 0 \SetRowColor{LightBackground} private & class private \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} (default) & package private \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} protected & package private+subclass \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} public & any class \tn % Row Count 4 (+ 1) \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}{Optional specifier}} \tn % Row 0 \SetRowColor{LightBackground} static & class method,accessed by class name or obj reference \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} abstract & w/o body, even empty \{\} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} final & no overridden by subclass \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{synchronized} & OCP, thread safe \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} native & interact with other language \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} strictfp & float point calc portable \tn % Row Count 8 (+ 1) \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}{Return type}} \tn % Row 0 \SetRowColor{LightBackground} Required & void =return; or no return statement \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} void method()\{return;\} & void method()\{\} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} String method()\{return "";\} & String method()\{return null;\} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} String{[}{]} method()\{return null;\} & Integer method()\{return null;\} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} void method()\{return null;\} & This not compiled \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} String method()\{if ... return null; else return;\} & Not compile \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Method name}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{follow variable naming rule} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} start with & letters, \&, \_ \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} can contains & number \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Can not start with number} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Parameter list}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Must have a parameter list} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{if no parameter, use empty ()} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{parameter separated by ;} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Optional Exception List}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{throws excaption\_name1,exception\_name2,...} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Method body}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{MUST for all non abstract method, Could by empty \{\}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{abstract method could not have a body, even empty \{\} one} \tn % Row Count 4 (+ 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}{Order must be followed}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{accessor -{}-optional-modifier -{}- return type name() optional-throws .... \{\}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{accessor could be omitted for default access} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{accessor,return type, name, (), \{\} are required} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{signature: return type + name + ()} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{overloading: same name, but differs in parameter list, overloading won't care about return type} \tn % Row Count 7 (+ 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}{varargs}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{//varargs=variable arguments, \newline //array but variable in length \newline //only last one parameter could be varargs \newline //accessed like array \newline public void walk (int start, int...steps) \newline \{ \seqsplit{System.out.print(steps.length} +"," +steps{[}0{]});\} \newline walk(1);//ArrayIndexOutOfBoundsException \newline walk(1,2) //1,2 \newline walk(1,2,3);//2,2 \newline walk(1,null);//NullPointException \newline walk(1,new int{[}{]} \{1,2,3\}); //3,1 \newline walk(1,new int{[}3{]}); //3,0} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.87717 cm} x{0.87717 cm} x{1.33664 cm} x{1.08602 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{5.377cm}}{\bf\textcolor{white}{protected members}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{pacakage} & \seqsplit{subclass} & direct access & by obj ref \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} same & Yes & yes & yes \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} same & No & yes & Yes \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} diff & Yes & yes & No * \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} diff & diff & No & No \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{5.377cm}}{could access protected member from parent by obj ref of subclass itself} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.23965 cm} x{2.73735 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Design static methods and fields}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{2 use cases} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} shared component among instances & util/helper which not require any obj state \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{2 calling approaches} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} by class name & by obj ref ( null obj can too) \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} Static variables & Counter, Constant \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} Static initializer- run once & static \{....\}//bad practice \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import static java.util.Arrays.asList} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{import static java.util.Arrays.*;} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} static import \seqsplit{java.util.Arrays.*;} & syntax error \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} import static java.util.Arrays; & Can not static import class \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} Name conflicts: not compiled & Solution: regular import,ref by classname \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Object has its status, but shared the code, and static data} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.16956 cm} x{0.87717 cm} p{0.71009 cm} x{1.42018 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{5.377cm}}{\bf\textcolor{white}{Static vs instance}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{type}} & {\bf{calling}} & {\bf{legal}} & {\bf{how}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} static method & static & Yes & Class name \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} static method & \seqsplit{instance} & No & create obj \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} instancd method & static & Yes & class name,obj ref \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} instance method & \seqsplit{instance} & Yes & obj ref \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{5.377cm}}{Object has its status, but shared the code, and static data} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.39356 cm} x{3.58344 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Passing data among method}} \tn % Row 0 \SetRowColor{LightBackground} pass by value & change in callee not affect caller \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} pass by ref & change in callee do affect caller \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Return value, if not used, will be discarded.} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.44333 cm} x{3.53367 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Overloading a method}} \tn % Row 0 \SetRowColor{LightBackground} Method signature & name+parameter list \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Overloading:} & same name+different parameters \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} varargs & 2nd test can't compile, varargs and array = same parameter list \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} & void test(int... a)\{\}; void test(int{[}{]} a)\{\} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} & test(new \{1,2,3\}) call either \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} & test(1,2,3) call test(int... a) only \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} autoboxing & void fly(int a)\{\}; void fly(Integer a)\{\}; \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} & fly(3) call fly(int a); if not exit, call fly(Integer a) \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} Ref type & void fly(Object o)\{\}; \tn % Row Count 18 (+ 1) % Row 9 \SetRowColor{white} & fly(3):match fly(int) \textgreater{}fly(Integer) \textgreater{}fly(Object o) \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} Primitive & autocast(wider), explicitly cast(narrower) \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{overloading matching order} \tn % Row Count 23 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{exact match -\textgreater{} unwrapping -\textgreater{} promotion -\textgreater{} wrapping -\textgreater{}varargs(exact match, promotion, wrapper)} \tn % Row Count 25 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{promotion is an independent check, it retains no influence over wrapping check} \tn % Row Count 27 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{void play(Long l)\{\}} \tn % Row Count 28 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{play(4); compile error, 2 step conversion, only 1 conversion allowed, play(4L) will work.} \tn % Row Count 30 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{2.01388 cm} x{1.00694 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Create constructors}} \tn % Row 0 \SetRowColor{LightBackground} Same name as class & no return type & \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} default no-argument constructor & auto generated if no constructor provided & \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{overloading of constructor}} & Constructor chain & by \seqsplit{this(...)} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{Calling Constructo}}r & This(...): not in static method & first line of method \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} & new Constructor(...) & Create new object \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} {\bf{final field}} & must be initialized by the time of constructor completion & \tn % Row Count 18 (+ 4) % Row 6 \SetRowColor{LightBackground} {\bf{super()}} & call automatically in any constructor implicitly & \tn % Row Count 21 (+ 3) % Row 7 \SetRowColor{white} & all class are subclass of Object & \tn % Row Count 23 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{order of initialization}} & super() is always call in every constructor implicitly; super(...) must be called explicitly & \tn % Row Count 29 (+ 6) % Row 9 \SetRowColor{white} \mymulticolumn{3}{x{5.377cm}}{super() -\textgreater{}static declaration and static \{\} as its order -\textgreater{}instance declaration and \{\} as its order -\textgreater{}constructor} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.55618 cm} x{2.01388 cm} x{1.00694 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Create constructors (cont)}} \tn % Row 10 \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{example}}} \tn % Row Count 1 (+ 1) % Row 11 \SetRowColor{white} class Bunny\{ \} & public bunny()\{\} & wrong \tn % Row Count 3 (+ 2) % Row 12 \SetRowColor{LightBackground} & public Bunny()\{\} & OK \tn % Row Count 4 (+ 1) % Row 13 \SetRowColor{white} & public void bunny()\{\} & OK \tn % Row Count 6 (+ 2) % Row 14 \SetRowColor{LightBackground} & public void Bunny()\{\} & wrong \tn % Row Count 8 (+ 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}{Encapsulating data}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Encapsulation: binding fields with method; data hiding(hiding \seqsplit{properties+implementation} details): private fields+public setter and getter; purpose: data validation and integrity + flexibility code for upgrade and maintenance} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} private attributes & public getter(), setter(), isA() \tn % Row Count 7 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Immutable class}} & Math, String \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} Class w/o public setter & no status change of object once created \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} safe to passing around and easy to maintain & bette performance by limiting the number of copies ( string pool) \tn % Row Count 14 (+ 4) % Row 5 \SetRowColor{white} {\bf{String and StringBuffer}} & Mutated by StringBuffer, return a String \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{JavaNean}}} \tn % Row Count 17 (+ 1) % Row 7 \SetRowColor{white} a class with no-argument constructor & naming convention:instance variable, getA,setA,isA, \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} reusable & easy to code \tn % Row Count 21 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.78503 cm} x{2.19696 cm} p{0.59501 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Lambdas expression}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{Why lambdas?}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} less code & delayed implementation & \seqsplit{declarative} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{what's lambdas?}} & a declarative block of code & \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{3}{x{5.377cm}}{passed to the associated 1 method interface, as delayed dynamic implement the method} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{Syntax}}} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} parameter & -\textgreater{} & body \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} ()-\textgreater{}true & simplest format & \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} a-\textgreater{}a.canHop() & type is optonal & \tn % Row Count 12 (+ 1) % Row 8 \SetRowColor{LightBackground} (Animal a) -\textgreater{}\{return a.canHop();\} & can't miss ; or return if use\{\}; need () if has type & \tn % Row Count 15 (+ 3) % Row 9 \SetRowColor{white} (a,b)-\textgreater{}a.canHop() & () required if \textgreater{}1args; if \textgreater{}1 statement, need \{\} & \tn % Row Count 18 (+ 3) % Row 10 \SetRowColor{LightBackground} {\bf{Context}} & functional method to match the only method of an interface & \tn % Row Count 22 (+ 4) % Row 11 \SetRowColor{white} & replace the implementing class of the interface & \tn % Row Count 25 (+ 3) % Row 12 \SetRowColor{LightBackground} & parameter type will be get from the associated interface API & \tn % Row Count 29 (+ 4) % Row 13 \SetRowColor{white} \mymulticolumn{3}{x{5.377cm}}{{\bf{Notes}}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.78503 cm} x{2.19696 cm} p{0.59501 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Lambdas expression (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{lambdas expr could access static/instance variable. method parameter and local variable also fine if not assigned a new value} \tn % Row Count 3 (+ 3) % Row 15 \SetRowColor{white} \mymulticolumn{3}{x{5.377cm}}{can not redeclare a variable same name as local variable} \tn % Row Count 5 (+ 2) % Row 16 \SetRowColor{LightBackground} (a,b)-\textgreater{}\{int a=0;return 5;\} & won't compile & \tn % Row Count 7 (+ 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}{Lambdas example - Predicate}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{//Predicate interface \newline public interface Predicate\textless{}T\textgreater{}\{ \newline boolean test(T t); \} \newline \newline // \newline class Animal\{ \newline private String canHop; \newline public void setCanHop(boolean b) \newline \{ canHop =b; \} \newline public boolean canHop() \newline \{ return canHop; \} \newline \} \newline public interface CheckTrait \newline \{ boolean test(Animal a); \} \newline public class CehckIfHopper implements CheckTrait\{ \newline public boolean test(Animal a) \newline \{ return a.canHop();\} \newline \} \newline \newline //compare \newline import \seqsplit{java.util.function.Predicate;} \newline public class TestLambdas \{ \newline public static void main(String{[}{]} args)\{ \newline Animal a1 = new Animal(); \newline a1.setCanHop(true); \newline print (a1,a-\textgreater{}a.canHop()); \newline // print2 (new checkIfHopper()); \newline \} \newline void print(Animal a, Predicate\textless{}Animal\textgreater{} p) \{ \newline if (p.test(a) \{\} \} \newline \newline void print2(Animal a, CheckTrait c) \{ \newline if (c.test(a) \{\} \newline \} \newline //for Predicate version, no interface and implementing class needed \newline // \{return a.canHop();\} will be delayed dynamic implementation of test method in Predicate interface.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Type must match in lambda expr, or just do put type} \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}{Lambdas - ArrayList.removeIf()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{//Java 8 intergrated Predicate interface into ArrayList \newline default boolean removeIf(Predicate\textless{}? super E\textgreater{} filter) \newline \newline arrayList.removeIf(s-\textgreater{}s.charAt(0)!='h');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{ArrayList\textless{}? super E\textgreater{} : super class of E, upper bound: "This can be cast to E". \newline ArrayList\textless{}? extends E\textgreater{}: hold type= subclass of E, lower bound: "E can be cast to this." \newline ArrayList\textless{} E\textgreater{}: holder type E} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{What happens after new Constructor()}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{1. allocate memory space on the heap} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{2. create the object and instance variables are initialized with default value} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{3. explicit initialize the instance variabls} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{4. constructor code are executed} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Notes:static variable are initialized once for all objects to be created, so it is before step 2} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{for no argument constructor, super() is called first for initialization} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}