Core Java
Core Java
1
Overview
2
Learning Objectives
Learn
• Core Java Fundamentals •Global & Local Variables
• Features of Java •Static and Non Static Variables
•Static Function
• OOP Principles
•Call By Value
• Data abstraction •Call By Reference
• Encapsulation •Constructors
• Polymorphism •Difference between constructor and
• Inheritance method
• Concept of Class and Object •Super keyword
•final
• Structure of a Java program
•Types of inheritance
• Platform independence •Interface
• Data Types in Java •Abstract class
• String class •Overloading and overriding methods
• Arrays •Packages
•Exception Handling
3
Prerequisites to learn Java
4
Object Oriented Programming
Principles
5
Data abstraction
7
Polymorphism
8
Inheritance
9
Concept of Class and Object
10
Did you know?
A Program is made up of
1. Keywords
2. Identifiers
3. Literals
Ex: Int EmpId =10310;
Int = keyword
EmpId =identifier
10310 =literal
11
Structure of a Java program
12
Platform independence
13
Data Types in Java
14
String class
15
Conditional statements and Operators
Loops
• If(condition){ statment1;}
• If-else
• If (condition){stat1}else{stat2;}
• If(condition){stat1}-else if(condition){stat2}
• Switch statement allows a variable to be tested for equality against
a list of values.
switch(expression){
case value1 : //Statements
break;
default : //Statements }
Loops
For
While
Do-while
16
Arrays
17
Global & Local Variables
18
Static and NonStatic Variables
• Static variables are declared with the static keyword in a class, but
outside a method, constructor or a block.
• A single copy will be maintained irrespective of number of objects.
• Static variables can be accessed by calling:
• With classname
• Without object
• NonStatic variables are can be accessed by object.
• It can have multiple copies as much as object creation for class
19
Static and NonStatic Function
20
Call By Value
21
Call By Reference
22
Constructors
23
Difference between constructor and
method
Method Constructor
• Method can be executed when • Constructor gets executed only
we explicitly call it. when object is created.
• Method name will not have same • Constructor name will be same as
name as class name. class name.
• Method should have return type. • Constructor should not have
• A method can be executed n return type.
number of times on a object. • Constructor will get executed
only once per object.
24
Super keyword
• We can call immediate super class instance variable.
this keyword
1.this keyword can be used to refer current class instance variable.
2.this() can be used to invoke current class constructor.
3.this keyword can be used to invoke current class method (implicitly).
4.this can be passed as an argument in the method call.
5.this can be passed as argument in the constructor call.
6.this keyword can also be used to return the class instance.
7.We cannot use this and super at a time.
25
final
26
Types of inheritance
27
Interface
28
Abstract class
29
Interfaces vs. Abstract classes
30
Overloading and Overriding methods
31
Packages
32
Exception Handling
33
Exception Handling contd..
Types of Exception
1)Checked Exception: A checked exception is an exception that is typically a
user error or a problem that cannot be foreseen by the programmer.
Ex: If a file is to be opened, but the file cannot be found, an exception
occurs
2)A runtime exception is an exception that occurs that probably could have
been avoided by the programmer.
Ex: ArrayIndexOutOfBoundException
34
Exception Handling contd..
35
Collections And Array List
36
Stack And HashTable
Stack HashTable
• Stack is a subclass of Vector that • The java.util.Hashtable class
implements a standard last-in, implements a hashtable, which
first-out stack. maps keys to values.
• boolean empty() • Hash function will compute unique
• Object peek( ) value as a index to the key.
• Object pop( ) • Methods are:
• Object push(Object element) • Set<Map.Entry<K,V>> entrySet()
• int search(Object element) • Collection<String> collection
=ht.values();
• Set<String> set1 =ht.keySet()
37
String Manipulation
The String class has several methods for manipulating the contents of a String.
Finding a Substring
It can be useful to find if a String contains a sequence of Concatenating Strings
characters. For example, we could search Two Strings can be added together to make a
the bandname variable for the String "Who". To look for the bigger String. There are a couple of ways to do this.
substring "Who" we can used the indexof method: The + operator is the easiest way:
int index = bandName.indexOf("Who"); the result is newBandName = newBandName + "Clash“;
an int specifying the index number - in this case it will be 4 as
that is the position of the W character. Trimming Strings
String newBandName = bandName.substring(0,index); String tooManySpaces = " Neil Armstrong.. ";
This results in newBandName containing the string "The". tooManySpaces = tooManySpaces.trim();
38
Access Specifiers
1. public
2. private
3. protected
4. default(no specifier)
39
File Manipulation
40
Multithreading
41
Java Garbage collection
42
System Requirements
Mac OS X
• Java 8 System Requirements
Intel-based Mac running Mac OS X
• Detailed information on system requirements
10.8.3+, 10.9+
for Java 8 are available at
Administrator privileges for
Java 8 Supported System Configurations.
installation
• Windows
64-bit browser
• Windows 8 (Desktop) A 64-bit browser (Safari, Firefox, or
• Windows 7 Chrome for example) is required to
• Windows Vista SP2 run Oracle Java on Mac OS X.
• Windows Server 2008 R2 SP1 (64-bit) Linux
• Windows Server 2012 (64-bit) Oracle Linux 5.5+1
• RAM: 128 MB Oracle Linux 6.x (32-bit), 6.x (64-bit)2
• Disk space: 124 MB for JRE; 2 MB for Java Oracle Linux 7.x (64-bit)2
Update Red Hat Enterprise Linux 5.5+1, 6.x (32-
• Processor: Minimum Pentium 2 266 MHz bit), 6.x (64-bit)2
processor Ubuntu Linux 12.04 LTS, 13.x
• Browsers: Internet Explorer 9 and above, Firefox, Suse Linux Enterprise Server 10 SP2+,
Chrome 11.x
Browsers: Firefox
Solaris System Requirements
43
Summary
There were five primary goals in the creation of the Java language.
• It must be "simple, object-oriented and familiar"
• It must be "robust and secure"
• It must be "architecture-neutral and portable"
• It must execute with "high performance"
• It must be "interpreted, threaded, and dynamic"
They have hence been achieved. The key takeaways of this session are learning
Object oriented concepts, the different implementations and features of the
Java Language.
44