\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 (operator-an-statement.pdf) /Creator (Cheatography) /Author (Jianmin Feng (taotao)) /Subject (Operator an statement 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}{216926} \definecolor{LightBackground}{HTML}{F8FAF8} \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{Operator an statement Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jianmin Feng (taotao)} via \textcolor{DarkBackground}{\uline{cheatography.com/79308/cs/19373/}}} \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.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{[}{]} ()} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} x++ x-{}- & post unary \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} ++x -{}-x & pre unary \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textasciitilde{} + - ! & ! only for logic \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{* / \%} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{+ -} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \textless{}\textless{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{} & shift \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \textless{}\textgreater{} \textless{}= \textgreater{}= instanceof & compare \tn % Row Count 9 (+ 2) % Row 8 \SetRowColor{LightBackground} == != & for primitives, obj type compare addr \tn % Row Count 11 (+ 2) % Row 9 \SetRowColor{white} \&- \textasciicircum{} -\textgreater{}| & logic \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} \&\& -\textgreater{} || & shortcut logic \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} (boolean)?x :y & tenary (can be nested) \tn % Row Count 15 (+ 2) % Row 12 \SetRowColor{LightBackground} = & assignment \tn % Row Count 16 (+ 1) % Row 13 \SetRowColor{white} += -=... & compound assignment, auto casting \tn % Row Count 18 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{casting (type)} \tn % Row Count 19 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{== and != are logic; = is assignment \newline . operator precedent of casting, so be careful with (B a).m() vs (B)a.m()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Numberic promotion}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{promote the larger operand type} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{byte,short,char -\textgreater{}int-\textgreater{}float-\textgreater{}double} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{assign result to promoted type} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{1)unary exclusive short ++ -\textgreater{} still short} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{2) by default literal is int or double} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} float f=1; & ok,1 is a definite number \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} float f=0.1; & X 0.1 is not definite number \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} long l=power(2,32) & X \seqsplit{Integer.MAX\_VALUE=power(2},32)-1 \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 11 (+ 0) % Row 9 \SetRowColor{white} short a=2,b=3,z;z=a*b; & a*b -\textgreater{} int \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{x++ and ++x}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{int x=3,y;} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} y=x++; & assign x to y(1), then increment x(2) \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} y=++x; & increment x(2), then assign x to y(2) \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} y=++x*5/x-{}- + -{}-x; & x=2,y=7 \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{No effect in stand for loop} \tn % Row Count 8 (+ 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}{Using enum}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1. dot notation, Days.FRIDAY \newline 2. name(), toString() -\textgreater{}capital label \newline 3. ordinal() -\textgreater{} index from 0 \newline 4. valueOf(String) -\textgreater{}parse string to enum \newline 5. compare: == or equals() \newline 6.switch (enum\_var) \{ \newline case enum\_value: \newline .. \newline default: \newline .... \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.9862 cm} x{1.9908 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{enum}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Why enum?} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} more readable constant declaration & allow compiler time check \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} avoid unexpected behavior & document upfront \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} What's enum & start from java5 \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{extends java.lang.enum} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{enum constant internally implemented as public static final variables} \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{overriden equals(), toString, hasCode()} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{may define variable,constructor, methods just like in a class} \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} name(),toString() return string value & ordinal() return index \tn % Row Count 14 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{could has constuctor and methods, just like a class} \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Assignments}} \tn % Row 0 \SetRowColor{LightBackground} assignment overflow ( too high to hold) & int i=1.0;byte b=128;float f=0.1; \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{explicit cast required when overflow} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{underflow (float number too small to represent)} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Compound assignment & auto cast back to left operand \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} assignment chaining allowed & int i,j,k; i=j=k=9; \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} if (a=9) \{\} //exception & if (a=true)\{\} //ok \tn % Row Count 10 (+ 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}{enum example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public enum Days \{ \newline SUNDAY, MONDAY, TUESDAY, WEDNESDAY, \newline THURSDAY, FRIDAY, SATURDAY; \newline \} \newline \newline // syntax like class, but with enum keyword \newline // name=capital label;ordinal=index value from 0 \newline // could have \seqsplit{fields/constructor/methods} inside \newline // extends java.lang.enum \newline // implemented internally as class, \newline // enum value are public static final object constant \newline \newline public enum MusicType\{ \newline CLASSIC(1),JAZZ(4),ROCK(6),METAL(11); \newline private int earDamageFacor; \newline \newline private MusicType(int earDamageFactor) \newline \{ this \seqsplit{earDamageFactor=earDamageFactor;} \} \newline \newline public int calcHearingLoss(int huors) \newline \{ return huors*earDamageFactor;\} \newline \newline public String toString () \newline \{ return \seqsplit{this.name()+"-"+this.ordinal()+'\_"} \newline + this.earDamageFactor; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Java Statement}} \tn % Row 0 \SetRowColor{LightBackground} if ({\bf{boolean}}) \{\} & multiple statement need \{\} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} else if (...) \{\} & one statement may miss \{\} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{else \{\}} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} ( ...) ? x :y & x,y: same data type \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} ... ? ...?x :y:z & nested \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{if (x=5) \{\} //won't compile} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Switch}} \tn % Row 0 \SetRowColor{LightBackground} switch \seqsplit{(literal-value)} \{ & Byte,byte,Short,short,Integer,int \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} case 1: ....;break; & Character,char,enum,String \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} case 2: ....;break; & 8 types, after case is literal \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} case 0: ....;break; & default: position not matters \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} default: & breaks; avoid follow through, need in every case, exclude last on \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\}} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{the case value must be compiling time literal} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.78712 cm} x{2.18988 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{While}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{while (...) \{ .. increate conunter ...\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} do \{....\} wjile (....) & execute \textgreater{}=1 times \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} infinity loop & while (true) \{\} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.38436 cm} x{1.59264 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{for loop}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{for ( int i;i\textless{}arr.length-1;i++)}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{standard for loop: (4 steps init -\textgreater{}eval -\textgreater{}body exec -\textgreater{}increament, ++i,i++ no different)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{multiple declaration initializer allowed, separated by ',', should be same type} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{initializer scope: inside for loop} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{for ( Class instance:collection)\{\}}}} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{for each: iterate only, unable to access i} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{java.iterable \textless{}-java.lamg.collection} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Array, ArrayList, List} \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} {\bf{for(;;)\{\}}} & infinite loop \tn % Row Count 12 (+ 2) % Row 9 \SetRowColor{white} for( int i=0;i\textless{}10;) \{i=i++;\} & infinite loop \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{for ( int value, values) \{\} -\textgreater{} for ( \seqsplit{java.uitl.iterator(Integer)} i=value.iterator(); i.hasNext())\{\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Advance Flow Control}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Nested loops}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Optional label}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} OUTER\_LOOP:.... INNER\_LOOP & all flow control allowed, but bad practice \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} {\bf{breaks}} optional\_label; & not for if..then..else \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} breakout current block & breakout current block and go to optional label \tn % Row Count 9 (+ 3) % Row 5 \SetRowColor{white} {\bf{Continue}} optional\_label; & not for if..then..else and switch \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} stop and Continue next iteration & stop and go to optional label \tn % Row Count 13 (+ 2) \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}{Scope}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{if, while,for loop define local scope} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{vaiable declared inside control block could not used out of \{\}} \tn % Row Count 3 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}