-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaBasics.tex
More file actions
720 lines (536 loc) · 14.8 KB
/
JavaBasics.tex
File metadata and controls
720 lines (536 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
\documentclass[11pt]{beamer}
% list of packages used
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{listings}
% Author details
\author{Vinay @EduTechLabs}
\title{Java Basics}
% configs
%%set theme
\usetheme{JuanLesPins}
%%configure listings package for java
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{8pt} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{8pt} % for normal
%custom colours
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}
\definecolor{javared}{rgb}{0.6,0,0} % for strings
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
%configuring listings package to highlight java syntax
\newcommand\javastyle{\lstset{
language=Java,
basicstyle=\ttfamily,
keywordstyle=\color{javapurple}\bfseries,
stringstyle=\color{javared},
commentstyle=\color{javagreen},
morecomment=[s][\color{javadocblue}]{/**}{*/},
numbers=left,
numberstyle=\tiny\color{black},
stepnumber=1,
tabsize=4,
showspaces=false,
otherkeywords={this, class, enum}, % Add keywords here
%keywordstyle=\ttb\color{deepblue},
emph={MyClass,Pizza,className}, % Custom highlighting
emphstyle=\ttb\color{pred}, % Custom highlighting style
moredelim=[il][\textcolor{pgrey}]{$$}, %$$ %the double dollar to prevent texmaker from going crazy
%moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
%frame=tb, % Any extra options here
showstringspaces=false,
}}
% java environment
\lstnewenvironment{java}[1][]
{
\javastyle
\lstset{#1}
}
{}
% java for external files
\newcommand\javaexternal[2][]{{
\javastyle
\lstinputlisting[#1]{#2}}}
% java for inline
\newcommand\javainline[1]{{\javastyle\lstinline!#1!}}
%%%%%%%%%%%% begin writing the actual document %%%%%%%%%%%%%%%%%
\begin{document}
\section{Welcome!}
%get the title page
\frame{\maketitle}
% welcome page
\begin{frame}{Welcome to the course}
\begin{center}
Welcome to \emph{\color{pblue}EduTechLab's} basic java Course.
\end{center}
\end{frame}
%%%%%%%%%%%%%%% about java
\begin{frame}{About Java}
\begin{enumerate}
\item Java is this..
\item Java is that...
\end{enumerate}
\huge Goto Wikipedia !!\\
\normalsize Not going to happen !!
\end{frame}
%%%%%%%%%%%%%%%%%%%%
%START MAKING SLIDES FROM HERE ON
%%%%%%%%%%%%%%%%%%%%
\section{The Bare Basics}
\begin{frame}[containsverbatim]{Hello World}
\begin{java}
public class MyClass{
/* This will program print
* `Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");
// prints Hello World
}
}
\end{java}
\end{frame}
\begin{frame}[containsverbatim]{Compile and run java program}
\begin{flushleft}
\item{Running a java program} \\
\begin{java}
javac HelloWorld.java
java HelloWorld
\end{java}
\item{Note: navigate to java file location through terminal before executing above commands.}
\end{flushleft}
\end{frame}
\begin{frame}[containsverbatim]{Java Comments}
\begin{java}
public class HelloWorld{
public static void main(String[]args){
// an example of single line comment
System.out.println("Hello World");
/* Multi line comments
are given like this. */
}
}
\end{java}
\end{frame}
\section{Classes}
\begin{frame}[containsverbatim]{Java Class Structure}
\begin{java}
public class ClassName{
String name; // instance variable
static int count; // class variable
public ClassName(String name){ // constructor
this.name = name;
}
void method1(){
String address; // local variable
}
}
\end{java}
\end{frame}
\begin{frame}[containsverbatim]{Basic Class Syntax}
\begin{java}
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
\end{java}
\end{frame}
\subsection{Constructors}
\begin{frame}[containsverbatim]{Constructors}
\begin{java}
public class Person{
String name;
public Person(){
}
public Person(String name){
this.name = name;
}
}
\end{java}
\end{frame}
\begin{frame}[containsverbatim]{Java Constructor}
\begin{java}
public class Dog{
String name;
public Dog(){
}
public Dog(String name){
this.name = name;
}
}
\end{java}
\begin{java}
Dog Bruno = new Dog();
Dog Bruno = new Dog("Bruno");
\end{java}
\end{frame}
\subsection{Getters and Setters}
\begin{frame}[containsverbatim]{Getters and Setters}
\begin{java}
public class Dog{
private String name;
public Dog(){
}
public Dog(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}
\end{java}
\end{frame}
\subsection{Classes - things to remember}
\begin{frame}[containsverbatim,allowframebreaks]{Things to remember while creating Classes}
\begin{enumerate}
\item One .java file can have multiple classes.
\item Only one class per .java file can be public.
\item All remaining classes must be non-public.
\item Name of the public class must be the name of .java class.
\item class declared in package must begin with the package identification line
\begin{java}
package com.example;
\end{java}
\item import statements must be written between package declaration and class definition
\begin{java}
package com.example;
import ...
public class Dog{
....
}
\end{java}
\end{enumerate}
\end{frame}
\subsection{Inheritance}
\begin{frame}[containsverbatim]{Inheretence -- IS -- A Relation}
\begin{java}
public class Animal{
.....
}
public class Mammal extends Animal{
.....
}
public class Dog extends Mammal{
.....
}
public class Reptile extends Animal{
.....
}
\end{java}
\end{frame}
\begin{frame}[containsverbatim]{IS -- A vs HAS -- A Relation}
\begin{java}
public class Vehicle{
....
}
public class Engine{
....
}
public class Car extends Vehicle{//IS-A relation
private Engine e; //HAS-A relation
....
}
\end{java}
\end{frame}
\subsection{Singleton}
\begin{frame}[containsverbatim]{Singleton Class}
\begin{java}
public class SingletonClass{
private static SingletonClass instance = null;
protected SingletonClass(){
// Exists only to defeat instantiation.
}
public static SingletonClass getInstance(){
if(instance == null){
instance = new SingletonClass();
}
return instance;
}
}
\end{java}
\end{frame}
\section{Objects}
\begin{frame}[containsverbatim]{Creating Objects}
\begin{java}
import package.name.Person;
public class PersonTest{
public static void main(String[]args){
// created the reference which
// will point to object
Person ram;
// assign an object to reference
ram = new Person("Ram");
}
}
\end{java}
\end{frame}
\section{Polymorphism}
\begin{frame}[containsverbatim]{Polymorphism}
\end{frame}
\section{Overloading}
\subsection{Method Overloading}
\begin{frame}[containsverbatim,allowframebreaks]{Method Overloading}
\end{frame}
\subsection{Constructor Overloading}
\begin{frame}[containsverbatim,allowframebreaks]{Constructor Overloading}
\end{frame}
\section{Java Collections}
\begin{frame}[containsverbatim,allowframebreaks]{Collections}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{Arraylist}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{Iterator}
\end{frame}
\section{Java Looping}
\begin{frame}[containsverbatim,allowframebreaks]{Loops}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{for loop}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{while loop}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{do while}
\huge \color{javared} Community frowns upon the use of this one !
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{for loop iterator}
\end{frame}
\section{Typecasting}
\begin{frame}[containsverbatim,allowframebreaks]{typecasting}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{Access Modifiers -- Default, Public, Private, Protected}
\begin{enumerate}
\item \emph{Default} -- Visible to the package, No modifiers are needed.
\item \emph{Private} -- Visible to the class only.
\item \emph{Public} -- Visible to the world.
\item \emph{Protected} -- Visible to the package and all subclasses.
\end{enumerate}
\end{frame}
\section{Primitive Data Types}
\begin{frame}[containsverbatim,allowframebreaks]{Data Types}
\begin{enumerate}
\item Primitive Data Types
\begin{enumerate}
\item byte
\item short
\item int
\item long
\item float
\item double
\item boolean
\item char
\end{enumerate}
\item Object Data Types
\begin{enumerate}
\item Dog
\item Person ...
\end{enumerate}
\end{enumerate}
\end{frame}
\section{Enums}
\begin{frame}[containsverbatim]{Enum Example}
\begin{java}
class Pizza{
enum PizzaSize{ SMALL, MEDUIM, LARGE }
PizzaSize size;
}
public class PizzaTest{
public static void main(String args[]){
Pizza p =new Pizza();
p.size =Pizza.PizzaSize.MEDUIM ;
}
}
\end{java}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% prashant
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[containsverbatim,allowframebreaks]{static, final, abstract}
\begin{java}
import java.io.*;
public class Employee{
// salary variable is a private static variable
private static double salary;
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";
public static void main(String args[]){
salary = 1000;
System.out.println(DEPARTMENT+"average salary:"+salary);
}
}
\end{java}
\begin{enumerate}
\item \emph{static modifier} -- used to create methods and variables which can be called without creating class object.
\item \emph{final modifier} -- used to finalize the implementations of classes, methods, and variables.
\item \emph{abstract modifier} -- for creating abstract classes and methods.
\item Java provides a number of non-access modifiers to achieve many other functionality.
\item The static modifier for creating class methods and variables
\item The final modifier for finalizing the implementations of classes, methods, and variables.
\item The abstract modifier for creating abstract classes and methods.
\end{enumerate}
\end{frame}
\begin{frame}[containsverbatim]{try catch finally}
\begin{java}
try{
....
//Protected code
}catch(ExceptionName e1){
....
// code to handle exception
}finally{
....
//clean-up code
}
\end{java}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{if else}
\begin{java}
if(condition){
....
}else if(condition){
....
}else{
....
}
\end{java}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{switch}
\begin{java}
switch(expression){
case value :
//Statements
break;//optional
case value :
//Statements
break;//optional
//You can have any number of case statements.
default://Optional
//Statements
}
\end{java}
\begin{enumerate}
\item The variable used in a switch statement can only be a byte, short, int, or char.
\item The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
\item When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
\item Not every case needs to contain a break. If no break appears, the flow of control will fall throughto subsequent cases until a break is reached.
\item The default
case can be used for performing a task when none of the cases is true. No break is needed in the default
case.
\end{enumerate}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{Interfaces}
\begin{java}
class InterfaceExampleTwo implements interfaceTwo {
@Override
public void methhod() {
System.out.println(var);
}
}
interface interfaceTwo {
public final int var = 9;
public abstract void methhod();
}
\end{java}
\begin{enumerate}
\item All Interface methods are implicitly public and abstract.
\item Interfaces can declare only Constant. Instance variables are not allowed. It means all variables inside Interface must be public, static, final.
\item Variables inside interface are implicitly public static final.
\item Interface methods can not be static, final.
\item Interface can extend one or more other interface. Note: Interface can only extend other interface.
\end{enumerate}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{utility functions -- parseint etc.}
\begin{java}
int i = Integer.parseInt("245");
float f = Float.parseFloat("245");
\end{java}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{threads}
\begin{java}
class MyThread extends Thread{
public void run(){
System.out.println("in MyThread");
}
}
MyThread t = new MyThread();
class MyRunnable implements Runnable{
Public void run(){
System.out.println("in MyRunnable");
}
}
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
\end{java}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{Inner class}
\begin{java}
class OuterClass {
private int i = 9;
public void createInner() {
InnerClass i1 = new InnerClass();
i1.getValue();
}
// inner class declarataion
class InnerClass {
public void getValue(){
System.out.println("value of i:" + i);
}
}
}
\end{java}
\begin{enumerate}
\item Inner classes are class within Class. Inner class instance has special relationship with Outer class.
\item Inner class can be accessed only through live instance of outer class.
\end{enumerate}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{scope}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{packages}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{HashMap}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{stack memory}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{static memory}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{heap memory -- object location}
\end{frame}
\begin{frame}[containsverbatim,allowframebreaks]{Array of Objects}
\begin{java}
//Single Dimensional Array of Primitives
int[] arr; //recommended
int arr[];
int[][] arr; //Multi Dimensional Array
int arr[][];
int[] arr[];Array Of Objects
//Single Dimensional Array Of Objects
String[] arr; //recommened
String arr[];
String[][] arr; //Multi Dimensional Array
String arr[][];
String[] arr[];
\end{java}
\begin{enumerate}
\item Arrays are a data structure type which is used to store multiple variables of the same type.
\item Once arrays created and space assigned to it next thing would be to add elements in it.Initialization of array is the place where we can do Adding Element in array.
\item Size of the array means how much element an array can contains.
\item Arrays can hold Primitives as well as Object,array will always be an Object in Heap.
\end{enumerate}
\end{enumerate}
\end{frame}
\end{document}