\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{mjorod} \pdfinfo{ /Title (oca-java-se-programmer-i-java-basics.pdf) /Creator (Cheatography) /Author (mjorod) /Subject (OCA Java SE Programmer I . Java basics 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}{1D41A3} \definecolor{LightBackground}{HTML}{F0F3F9} \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{OCA Java SE Programmer I . Java basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{mjorod} via \textcolor{DarkBackground}{\uline{cheatography.com/17938/cs/14018/}}} \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}mjorod \\ \uline{cheatography.com/mjorod} \\ \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 18th December, 2017.\\ 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}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Objectives}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The structures and components of a Java class} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Understanding executable Java applications} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Understanding Java packages} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Importing Java packages into your code} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Applying access and non access modifiers} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Features and components of Java} \tn % Row Count 6 (+ 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}{Structure of a Java class}} \tn % Row 0 \SetRowColor{LightBackground} Package statement & -{}-{}-{}-- 1 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Import statements & -{}-{}-{}-- 2 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Comments & -{}-{}-{}-- 3a \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Class declaration & -{}-{}-{}-- 4 \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Variables & -{}-{}-{}-- 5 \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} Comments & -{}-{}-{}-- 3b \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} Constructors & -{}-{}-{}-- 6 \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} Methods & -{}-{}-{}-- 7 \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Nested classes, nested interfaces and Enum are not covered Enum} \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}{Packages}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{All Java {\emph{classes}} are part of a {\emph{package}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{If the class has not package definition, it is classified in the {\bf{default}} {\emph{package}} (which doesn't have a name)} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Must be the first in the class definition (though you can define comments a above its declaration)} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The {\emph{package}} statement can't appear within a class declaration or after the class declaration} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Must appear exactly once in a class} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Classes and interfaces in the same pacakge can use each other without prefixing theier names with the pacakge name} \tn % Row Count 12 (+ 3) % Row 6 \SetRowColor{LightBackground} The use a class or an interface of another package, you must use its fully qualified name & \seqsplit{`packageName.anySubpackageName.ClassName`} \tn % Row Count 17 (+ 5) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Use {\bf{import}} statement to use the simple name of a class or interface} \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A package is made of multiple sections that go from the more-generic(left) to the more specific(right)} \tn % Row Count 22 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Per java naming conventions, packages names should all be in lowercase} \tn % Row Count 24 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The package and subpackage names are separated using a dot (.)} \tn % Row Count 26 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Package names follow the rules defined for valid identifiers in Java} \tn % Row Count 28 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{For classes and interfaces defined in a package, the {\emph{package}} statement is the first statement in a Java source file (a .java file)} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Packages (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The can be a maximum of one {\emph{package}} statement per Java source code file (.java file)} \tn % Row Count 2 (+ 2) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{All the classes and interfaces defined in a Java source code file are defined in the same package. They can be defined in separate packages.} \tn % Row Count 5 (+ 3) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The hierachy of classes and interfaces defined in packages must match the hierarchy of the directories in which these classes and interfaces are defined in the code.} \tn % Row Count 9 (+ 4) % Row 16 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{To enable the Java Runtime Enviroment (JRE) to find your classes, add the base directory that contains your packaged Java code to the classpath} \tn % Row Count 12 (+ 3) \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}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Can appear before and after a {\emph{package}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Can appear before and after a {\emph{class}} definition} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Can appear before and after a {\emph{method}}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Multiline comments & /* */ \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} Multiline comments & Can contain special characters. The following is a coding practice (but not required): \{\{nl\}\} /*\{\{nl\}\}* comments that span \{\{nl\}\}* multiple lines\{\{nl\}\}*\{\{nl\}\}*/ \tn % Row Count 12 (+ 7) % Row 5 \SetRowColor{white} End-of-line comments & // \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} Is it valid? & `String name = "\textbackslash{}/* Juan */ Paul";` \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Javadoc comments are special comments that start with /** and end with */ (this is processed by Javadoc, a JDK tool to generate API documentation)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Class declaration}} \tn % Row 0 \SetRowColor{LightBackground} Acces modifiers & public/private/protected \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Nonacces modifiers & \seqsplit{static/final/abstract/synchronized} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Class name} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} Name of the base class & if the class is extending another class \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Class body & (class fields, methods, constructors), included \{\} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{More on classes}} \tn % Row 0 \SetRowColor{LightBackground} class definition & used to specify the attributes ({\emph{variables}}) and behavior ({\emph{methods}}) of an object. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} & A class name starts with the keyword {\emph{class}}. It is cAsE-sEnSiTiVe \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} & The state of a class is defined using {\emph{attributes}} or instance variables \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} & It isn't compulsory yo define all attributes of a class before defining its methods. But this is far from being optimal for readability \tn % Row Count 17 (+ 6) % Row 4 \SetRowColor{LightBackground} Methods & often used to manipulate the instance variables \tn % Row Count 20 (+ 3) % Row 5 \SetRowColor{white} & A {\emph{class method}} or {\emph{static method}} can be used to maniplate the {\emph{static}} variables \tn % Row Count 24 (+ 4) % Row 6 \SetRowColor{LightBackground} Instance \seqsplit{variabls/attributes} & Each object has its own copy of the instance variables \tn % Row Count 27 (+ 3) % Row 7 \SetRowColor{white} & The instance variables are defined within a class but outside all methods in a class \tn % Row Count 31 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Interfaces in a Java source code file}} \tn % Row 0 \SetRowColor{LightBackground} Interface in a Java source code file & Types?? \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & Specifies a contract for the classes to implement \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} & Grouping of related methods and constants \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} & Starting Java 8, methods in an interface can define a default implementation \tn % Row Count 11 (+ 4) % Row 4 \SetRowColor{LightBackground} & Interface can also define {\emph{static}} methods. \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} & You can define either a single class or an interface in a Java source code file or multiple such entites. \tn % Row Count 19 (+ 5) % Row 6 \SetRowColor{LightBackground} & -able- suffix for interfaces like Comparable? preffix for interfaces, like IComparable? \tn % Row Count 24 (+ 5) % Row 7 \SetRowColor{white} & The classes and interfaces can defined in any order of occurrence in a Java source code file \tn % Row Count 29 (+ 5) % Row 8 \SetRowColor{LightBackground} & Classes and interfaces defined in the same Java source code file can't be defined in separate packages. \tn % Row Count 34 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`interface Controls \{ \newline void changeChannel(int channelNumber); \newline void increaseVolume(); \newline void decreaseVolume(); \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}{Executable Java Applications}} \tn % Row 0 \SetRowColor{LightBackground} What is aexecutable Java class? & An executable Java class, when handed over to the JVM, starts its execution at a particular point in the class-{}- {\emph{main}} method. The JVM starts executing the code that's defined in the {\emph{main}} method. \tn % Row Count 10 (+ 10) % Row 1 \SetRowColor{white} & A Java application can define more than one executable class. We have to choose one when the times comes to start its execution by the JVM \tn % Row Count 17 (+ 7) % Row 2 \SetRowColor{LightBackground} main method & Must be marked {\bf{public}} \tn % Row Count 19 (+ 2) % Row 3 \SetRowColor{white} & Must be marked as a {\bf{static}} method \tn % Row Count 21 (+ 2) % Row 4 \SetRowColor{LightBackground} & The name of the method must me be {\bf{main}} \tn % Row Count 24 (+ 3) % Row 5 \SetRowColor{white} & The return type of this method must be {\bf{void}} \tn % Row Count 27 (+ 3) % Row 6 \SetRowColor{LightBackground} & The method must accept a method argument of a {\bf{String}} array or a variable argument (varargs) of type {\bf{String}} \tn % Row Count 33 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Executable Java Applications (cont)}} \tn % Row 7 \SetRowColor{LightBackground} `public static void main(String... args)` & It's valid \tn % Row Count 3 (+ 3) % Row 8 \SetRowColor{white} `public static void main(String args...)` & Won't compile \tn % Row Count 6 (+ 3) % Row 9 \SetRowColor{LightBackground} `public static void main(String{[}{]} hola)` & acceptable \tn % Row Count 8 (+ 2) % Row 10 \SetRowColor{white} `public static void main(String argumenmtos{[}{]})` & It's ok \tn % Row Count 11 (+ 3) % Row 11 \SetRowColor{LightBackground} `static public void main(String argumenmtos{[}{]})` & Yes, you can exchange modifiers (ok) \tn % Row Count 14 (+ 3) % Row 12 \SetRowColor{white} set up to compile or execute from command prompr & \seqsplit{http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html} \tn % Row Count 18 (+ 4) % Row 13 \SetRowColor{LightBackground} D:\textbackslash{}\textgreater{}java hello & Execute a Java app \tn % Row Count 19 (+ 1) % Row 14 \SetRowColor{white} D:\textbackslash{}\textgreater{}java hello 1 2 & D:\textbackslash{}\textgreater{}java hello with arguments \tn % Row Count 21 (+ 2) % Row 15 \SetRowColor{LightBackground} & Java doesn't pass the name of the class as an argument to the main method (like C does) \tn % Row Count 26 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}