\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{evanescesn09} \pdfinfo{ /Title (core-java.pdf) /Creator (Cheatography) /Author (evanescesn09) /Subject (Core 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}{50E2F2} \definecolor{LightBackground}{HTML}{E9FBFD} \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{Core Java Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{evanescesn09} via \textcolor{DarkBackground}{\uline{cheatography.com/88543/cs/20288/}}} \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}evanescesn09 \\ \uline{cheatography.com/evanescesn09} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 16th August, 2019.\\ Updated 16th August, 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*}{2} \begin{tabularx}{8.4cm}{x{1.064 cm} x{3.268 cm} x{3.268 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{OOP}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Encapsulation} & The process of binding related classes, objects and operations together is called Encapsulation & Using access modifiers, packages \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \seqsplit{Abstraction} & The process of specifying what to do without specifying how to do it & Using abstract classes and interfaces \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{Inheritance} & When one class inherits the properties of another class & Using Aggregation, Composition \tn % Row Count 14 (+ 4) % Row 3 \SetRowColor{white} \seqsplit{Polymorphism} & Same thing is done in different ways & Using compile-time and run-time polymorphism \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.44 cm} x{6.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Encapsulation}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{default} & accessible to classes only in the same package \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} public & accessible to all classes in any package \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{private} & accessible to only a specific method, or class \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{protected} & accessible to classes in the same package, and sub-classes of this class \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.14 cm} x{3.192 cm} x{3.268 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Abstraction}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Abstract} Class & When a class has one or more unimplemented methods, it should be declared abstract. The sub-classes should provide implementation for the unimplemented methods, else they should also be declared abstract & Used: When default implementation is needed for some methods, and specific implementations for other methods based on the class implementing them \tn % Row Count 13 (+ 13) % Row 1 \SetRowColor{white} \seqsplit{Interface} & Blueprint of a class. It contains only static, final variables and only unimplemented methods. Classes implement the interface should implement all methods of the interface, or be declared abstract & Used: to support multiple inheritance \tn % Row Count 26 (+ 13) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{Abstract classes don't support multiple inheritance, whereas interfaces do} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.14 cm} x{4.104 cm} x{2.356 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Inheritance}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Aggregation} & When one class contains a reference of another class & Loosely coupled classes \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{Association} & When one class is made up of another class & Tightly coupled classes \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{Java does't support multiple inheritance directly, it supports it only via Interfaces} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.84 cm} x{6.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Polymorphism}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Compile-time} & Also called overloading. When methods have same name but different signature (return-type, number of parameters, type of parameters etc) \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} Run-time & Also called overriding. When child-classes over-write method implementations of parent-class. \tn % Row Count 9 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2 cm} x{6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{static keyword}} \tn % Row 0 \SetRowColor{LightBackground} static field & Shared by all members of the class. It can be accessed before objects are created \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} static method & Can be accessed without creating an instance of the class. They can only access static variables and static methods. Cannot access this or super \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} static block & Used when some computation is to be done to initialize the static variables. This block is executed once when the class is initially loaded into memory \tn % Row Count 14 (+ 6) % Row 3 \SetRowColor{white} static class & We cannot declare top-level classes as static. Only inner classes can be static. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class \tn % Row Count 21 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.28 cm} x{6.72 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{final}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{fields} & treated as constants \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{methods} & cannot be overridden by child classes \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{classes} & cannot be inherited \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{finalize( )}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{finalize() method is a protected and non-static method of java.lang.Object class. This method will be available in all objects you create in java. This method is used to perform some final operations or clean up operations on an object before it is removed from the memory} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Creation}} \tn % Row 0 \SetRowColor{LightBackground} Literal : String s = " " & Creates Strings in String pool, in JVM. Multiple strings can have same value. Only one copy of the word exists in the String pool, and the references of it are updated. \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} Object: String s = new String( ); & Creates a string object in heap. The heap in-turn checks the JVM String Pool to see if there exists a string with same value. \tn % Row Count 14 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{String s1 = "abc"; \newline String s2 = "abc"; \newline s1 == s2 returns true; \newline ====================== \newline String s1 = new String("abc"); \newline String s2 = new String("abc"); \newline s1 == s2 returns false; \newline But s1.equals(s2) returns true;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Immutability}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Strings in java are immutable because changing the value of a String literal changes the value of other Strings that reference the literal, which leads to inconsistency in the program. To prevent this, strings in java are immutable.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Storing passwords in Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{It is best to store passwords as char{[} {]} because if passwords are stored as Strings, the string tends to be in the JVM pool even after all references to it no longer exist. This causes a vulnerability in the system. In case of Char{[} {]}, once all the references to char{[} {]} are gone, the Java Garbage Collector deletes the char{[} {]} to preserve memory. So, it's safer.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2 cm} x{6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{StringBuilder, StringBuffer}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{StringBuilder} & To create mutable strings in Java \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{StringBuffer} & To create thread-safe mutable strings in Java \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String methods}} \tn % Row 0 \SetRowColor{LightBackground} & s.charAt(int index) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} & s.compareTo(s2), \seqsplit{s.compareToIgnoreCase(s2)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} & s.concat(s2) \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} & s.contains(sequence of characters) \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} & s.equals(s2), s.equalsIgnoreCase(s2) \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} & s.length() \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} & s.replace(character, replacement) ) \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} & s.replaceAll(character, replacement) \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} & s.subString(int startIndex) \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} & s.subString(int startIndex, int endIndex) \tn % Row Count 12 (+ 2) % Row 10 \SetRowColor{LightBackground} & s.toUpperCase( ), s.toLowerCase( ) \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} & s.toCharArray() \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} & s.trim( ) \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} & String s = String.valueOf(int, or long or double) \tn % Row Count 17 (+ 2) % Row 14 \SetRowColor{LightBackground} & String{[}{]} s1 = s.split( String regex) \tn % Row Count 18 (+ 1) % Row 15 \SetRowColor{white} & String{[}{]} s1 = s.split(String regex, int limit ) \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{StringBuffer, StribgBuilder methods}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{s.append(s2)} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{s.deleteCharAt(int index)} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{s.indexOf(string ), s.indexOf(string, fromIndex)} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{s.insert(int index, objectValue)} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{s.replace(int startIndex, int endIndex, String)} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{s.reverse( )} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{s.toString( )} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{s.trimToSize( )} \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{s.setCharAt(int index, charSequence)} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}