\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{COOKIE1234} \pdfinfo{ /Title (oop-java.pdf) /Creator (Cheatography) /Author (COOKIE1234) /Subject (oop java 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{oop java Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{COOKIE1234} via \textcolor{DarkBackground}{\uline{cheatography.com/148236/cs/32318/}}} \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}COOKIE1234 \\ \uline{cheatography.com/cookie1234} \\ \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 4th June, 2022.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Most used}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Print with new line -\textgreater{}}} \seqsplit{System.out.println("Hello} World"); \newline {\bf{Scanner object -\textgreater{}}} Scanner s = new Scanner(System.in); \newline {\bf{Find the length (int) -\textgreater{}}} msg.length() \newline {\bf{To lower/uppercase -\textgreater{}}} msg.toLower/UpperCase() \newline {\bf{Replace a string -\textgreater{}}} msg.replaceAll(String a, String b) \newline {\bf{charAt(int index) -\textgreater{}}} char value at the specified index \newline {\bf{equals(String/Object) -\textgreater{}}} if strings values are the same \newline {\bf{toCharArray() -\textgreater{}}} Convert string to character array} \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}{Switch}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{switch (num) \{ \newline case 1: doSomething(); \newline break; \newline default: doThis(); \newline break; \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}{Q1}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/cookie1234_1654377614_ שם.png}}} \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}{bubble sort}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/cookie1234_1654378007_ שם.png}}} \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}{binary search}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/cookie1234_1654378139_ שם.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Exception Handling in Java}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{ArithmeticException }} & Errors because of an arithmetic expression. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{ZeroDivisionException}} & Dividing a value with 0. \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{ArrayIndexOutOfBoundException}} & No such index number. \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\bf{FileNotFoundException}} & Accessing a file that does not exist. \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{IOException }} & Input or output error. \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} {\bf{NullPointerException}} & Referencing a null object. \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{NumberFormatException}} & When typecasting does not work. \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} {\bf{StringIndexOutOfBoundException}} & When there is no index for a particular string. \tn % Row Count 18 (+ 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}{try…..catch Statement in Java}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Exception\_Handaling \newline \{ \newline public static void main(String args{[}{]}) \newline \{ \newline try \newline \{ \newline int x = 10/0; \newline \} \newline catch(ArithmeticException e) \newline \{ \newline System.out.println("Division by zero"); \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}{File Reading and Writing Process}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Create a file \newline File Obj = new File("file.txt"); \newline // Write in the file \newline import java.io.FileWriter; \newline FileWriter write\_file = new FileWriter("file.txt"); \newline write\_file.write(" ") // write in the file \newline write\_file.close() //close file \newline //Read from a File: \newline File read\_file = new File("File.txt"); \newline System.out.println(read\_file.nextLine()); \newline read\_file.close();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import java.io.File -{}-\textgreater{} Create or Open File -{}-\textgreater{} Read or Write in to file -{}-\textgreater{} Close the file} \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}{Inheritance}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{object of a parent class cannot be cast down to object of the child class} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}One one = new Two(); -\textgreater{} Two two = (Two) one;} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Polymorphism for static methods does not work in the same way as instance methods.} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}In Java, a child class can make the method abstract which is inherited from the parent class.} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{If a reference of a parent class is pointing to the object of a child class, it can only call methods defined by the parent class.} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}"The method print() is undefined for the type One".} \tn % Row Count 12 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Unlike methods, constructors are not inherited by the child class and hence they cannot be overridden.} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}method needs a return type} \tn % Row Count 16 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Java constructors do not have return type.} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}error "The constructor One(String) is undefined".} \tn % Row Count 19 (+ 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}{Interface}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{interface Sun\{ \newline void rays(); \newline \} \newline class Mercury implements Sun\{ \newline public void rays()\{System.out.println("Mercury");\} \newline \} \newline class Earth implements Sun\{ \newline public void rays()\{System.out.println("Earth");\} \newline \} \newline class Sunlight\{ \newline public static void main(String args{[}{]})\{ \newline Sun e =new Earth(); \newline e.rays(); \newline \}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{An interface in Java helps to implement the concept of abstraction. In an interface class, we only have abstract methods and no method body. With interfaces, we can also achieve multiple inheritance in Java.} \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}{Method Overloading}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{When a class has two or more methods with the same name but different parameters, it is method overloading.} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Example of Overload:}} \newline public printer(String x)\{\} \newline public printer(String x, String y)\{\} \newline \newline If the input is 2 string, it will go to the second method instead of first one. \newline \newline {\bf{But you cannot overload by using the same input type sequence. For example:}} \newline public printer(String x)\{\} \newline public printer(String x, String y)\{\} // conflict \newline public printer(String y, String x)\{\} // conflict \newline \newline Java will not allow this to be run, because it cannot determine the value.} \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}{Override}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{When you have inherit some of the class from parents, but you want to do something different. In override feature, all the subclass/class object will use the newer method. \newline % Row Count 4 (+ 4) To make sure JDK knows what you are doing, type @Override in front of the public name. If the override is unsuccessful, JDK will returns error. \newline % Row Count 7 (+ 3) {\bf{Example of overriden helloWorld() method :}} \newline % Row Count 8 (+ 1) Class Student \newline % Row Count 9 (+ 1) public void helloWorld()\{ \newline % Row Count 10 (+ 1) \seqsplit{System.out.println("Hello");} \newline % Row Count 11 (+ 1) \} \newline % Row Count 12 (+ 1) Class GradStudent extends Student \newline % Row Count 13 (+ 1) @Override \newline % Row Count 14 (+ 1) public void helloWorld()\{ \newline % Row Count 15 (+ 1) \seqsplit{System.out.println("Hello} World"); \newline % Row Count 16 (+ 1) \} \newline % Row Count 17 (+ 1) {\bf{Rules of Overridden methods:}} \newline % Row Count 18 (+ 1) 1. Access modifier priority can only be narrower or same as superclass \newline % Row Count 20 (+ 2) 2. There is the same name method in superclass / libraries% Row Count 22 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}