0% found this document useful (0 votes)
7 views44 pages

Core Java

The document provides a comprehensive overview of Core Java fundamentals, covering key concepts such as Object-Oriented Programming principles, data types, exception handling, and the structure of a Java program. It outlines various Java features including encapsulation, inheritance, polymorphism, and the differences between static and non-static variables and functions. Additionally, it discusses collections, file manipulation, and access specifiers, serving as a foundational guide for learners with basic programming knowledge.

Uploaded by

balamurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views44 pages

Core Java

The document provides a comprehensive overview of Core Java fundamentals, covering key concepts such as Object-Oriented Programming principles, data types, exception handling, and the structure of a Java program. It outlines various Java features including encapsulation, inheritance, polymorphism, and the differences between static and non-static variables and functions. Additionally, it discusses collections, file manipulation, and access specifiers, serving as a foundational guide for learners with basic programming knowledge.

Uploaded by

balamurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Core Java Fundamentals

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

1. Basic Knowledge of Computers


2. Basic Knowledge of Programming
(Conditional Statements , Looping, Functions , compilation)
3. Basic Knowledge of Object Oriented Design
(which is generally the first chapter in Java Programming Books)

4
Object Oriented Programming
Principles

5
Data abstraction

• Abstraction involves extracting only


relevant information .
• In other words, Abstraction means
“looking for what you want “ in an object
or a class
• Ex: An Automobile salesperson is aware
that different people have different
preferences.some people are interested
in the speed of car, some in the engine,
some in style.although all of them want
to buy a car, each of them is interested in
a specific attribute or feature.
• Salesman knows all the details of a car,
but he presents only the relevant
information to a potential customer.As a
result, the sales man practices
abstraction and presents only relevant
details to customer.
6
Encapsulation

• Encapsulation literally means “to enclose in or


as if in a capsule”.
• Encapsulation is defined as the process of
enclosing one or more items with in a physical
or logical section.
• It involves preventing access to non essential
details
• Ex:when u plug in the cord of the vaccum
cleaner and turn on the switch, the vaccum
cleaner starts.we do not see the complex
process needed to actually convert electricity
into suction power.
• In other words,the exact working of the
cleaner has been encapsulated.
• Therefore encapsulation is also defined as
information hiding or data hiding because it
involves hiding many of the important details
of an object from the user.

7
Polymorphism

• It is the ability that helps in


executing different operations in
response to the same message.

• We can implement polymorphism


by creating more than one
function with in a function within
a class that have the same name.

• The difference in the functions lies


in the number and types of
parameters passed to each
function.

8
Inheritance

• Deriving the properties from


parent to child is inheritance

• with parent reference we can


access child class methods

• With child reference, we cannot


access parent class methods

9
Concept of Class and Object

• Class is a collection of properties and


methods
• Object is the instance of a class
• Example of a class =car
• Properties= colour, power, speed,
model,height
• Methods=fwd(),bkd(),stop(), start()
• Example of an object= Honda car , Toyota
car…..

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

• The output of java compiler is not executable code. It is byte code.


• Byte code is highly optimized set of instructions designed to be executed
by java run time system, which is called java virtual machine.
• Translating a java program into byte code helps make it easier to run a
program in a wide variety of environments. Only the JVM needs to be
implemented for each platform.

13
Data Types in Java

14
String class

2.String class object


1.charArray String name
Char =“oneforce”;
name[]={‘o’,’n’,’e’,’f’,’o’,’r’,’c’ or
,’e’} String
name= new String(“oneforce”);
name =
name.concat(“jayanagar”);

3.String Buffer class


StringBuffer sb =new
StringBuffer(“oneforce”);
Sb.append(“jayanagar”);

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

• Grouping the data objects of same type in a contiguous block of


memory.
• Array is an object created with new operator.
• Array index starts with zero.
• Array cannot be resized.
Array Declaration:
<Data type> [ ]<array name >
Ex: int[] myArray;
Constructing an Array:
<array name>=new<data type> <array Size >
myArray=new int[5];

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

Static Function Non Static Function

public class A { public class A {


static int add(int i,int j) { int add(int i,int j) {
return( I + j); return( I + j);
} }
} }
public class B extends A {
public class B extends A { public static void main(String args[]) {
public static void main(String args[]) { B b =new B();
int s= 9; int s= 9 ;
System.out.println(add(s, 6 )); System.out.println(b.add(s, 6 ));
} }
} }

20
Call By Value

• Passing a value held in the variable as an


argument to the method.
• The value is copied to method
parameters and changes takes place to
that parameters.
• That is why changes made to the
variable within the method had no
effect on the variable that was passed.

21
Call By Reference

• The Object is passed as an argument to the method.


• No copy of object is made here.
• Changes made in the method will be reflected in the original object

22
Constructors

• It can be tedious to initialize all of the variables in a class each time an


instance is created.
• It would be simpler and more concise to have all of the setup done at the
time the object is first created.
• Java allows objects to initialize themselves when they are created. This
automatic initialization is performed through the use of a constructor.
• Constructor is a special block in java class invoked at the time of object
creation.
• Constructor should always be public.
• A constructor name is same as class name ,the constructor should not
have return type and return statement.
• Whenever we create an object , the constructor body will get executed.

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

• Final is the keyword to avoid inheritance


• We use final in three ways
1.Before a variable(you can’t change)
2.Before a method (you can’t over ride)
3.Before a class(you can’t inherit)

26
Types of inheritance

There are 3 types of inheritance


• Single
• Circular(no prg supports )
• Multiple (java does not support)

27
Interface

• Java supports 3 different structures • It is a class with no implemented


1. Fully implemented methods.
2. Fully unimplemented • Interface must not be static.
3. Partly implemented/unimplemented • It contains only declarations.
• Interface contains abstract methods(with • Methods in interface must be static .
no body). • All variables must be assigned a value.
• Interface is an fully unimplemented • Interface variables are static ;we cannot
structure. change values.
• We need to provide body for all methods • Interface methods are neither static nor
in interface. non static.
• In interface, we cannot create the object • Creating interface reference ,we can
of interface. access implemented class methods which
• Can contain constants, but not variables. are defined in interface class but not
• All methods, variables are public static methods defined in implemented class.
final.

28
Abstract class

• It is partly unimplemented or partly implemented class which contains


zero or more abstract methods.
• Concrete method is the method with body.
• Abstract method is the method with out body.
• If user want to inherit the abstract class;he need to extend but not
implement.

29
Interfaces vs. Abstract classes

30
Overloading and Overriding methods

31
Packages

• Java Package is a mechanism for organizing Java classes into a namespace.


• Classes present in the package can access the each others class members.
• Classes can be imported from the package.
• Package name usually starts with lower case.
• Classes within a package can access classes and members declared with
default access and class members declared with the protected.

32
Exception Handling

Exception is an Event which halts normal execution abruptly and alternatively


the program will be terminated.
• Exception occurs when our code asks JVM to do technically impossible tasks.
• Ex:Divide by Zero.
• All Exception classes are subclasses of Throwable.
• Throwable has two subclass :Exception and Error

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

3)Error describes internal error and resource exhaustion.


Mostly beyond the programmers control.

34
Exception Handling contd..

35
Collections And Array List

Collections Array List


• Collection is a set containing the We create the ArrayList as
classes and interfaces which ArrayList<String> list =
implements reusable data structures
newArrayList<String>();
like List,Array,HashTable.
• There are so many interfaces available To add elements to the list:
like list.add("A");
java.util.ArrayList; List.add(2,”S”);//index=2
java.util.Iterator; To get the element from list
java.util.List; list.get(index).
java.util.ListIterator;

Sample Code for


Understanding
Collections

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 the Length of a String


String bandName = "The Who";
System.out.println(("The Who".length())); This would
display a result of 7 as there are seven characters in
the String. This means the character index will go up to a
value of 6 (The index starts from 0)

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

• Java Access Specifiers (also known as Visibility Specifiers )


regulate access to classes, fields and methods in Java.These
Specifiers determine whether a field or method in a class,
can be used or invoked by another method in another class
or sub-class. Access Specifiers can be used to restrict
access. Access Specifiers are an integral part of object-
oriented programming.

Types Of Access Specifiers :

In java we have four Access Specifiers and they are listed


below.

1. public
2. private
3. protected
4. default(no specifier)

39
File Manipulation

• Determine a home directory Deleting a File


• Use System.getProperty("user.home") to determine the Deleting a file is also handled through the java.io.File package by
home directory of the user running the application. invoking the delete method.
• Create a file HomeDirEx.java Create the file DeleteFileEx.java

import java.io.File; class DeleteFileEx { public static void


• class HomeDirEx { public static void main(String[] args)
main(String args[]) { File f = new File(FILENAME); if (f.exists())
{ System.out.println(System.getProperty("user.home")); } }
{ f.delete(); } } }
The code simply prints the home directory of the user
Simply checks if the file exists and if so delete it.
account the application is running under.
• Verifying a file/directory exists Move/Rename
• The package java.io.File is used to handle get and set simple When moving file within a filesystem the File objects renameTo
file information and perform a few simple operations. method will move a file.
Verifying the existance of a file is as easy as invoking the Create MoveFileEx.java
exists() method.
• Create a file VerifyFileExistsEx.java import java.io.File; class MoveFileEx { public static void main(String
import java.io.File; class VerifyFileExistsEx { public static void args[]) { File f = new File(FILENAME); if (f.exists()) { f.renameTo(new
main(String[] args) { File f = new File(FILENAME); if File(NEW_FILENAME)); } } }
(f.exists()) { System.out.println("File exists"); } else Open a File object, verify it exists, and if so rename it to a new
{ System.out.println("File does not exist"); } } } filename. This will not work across filesystems. To do that you need
The code simply opens a File object pointing at FILENAME. to do a copy and delete. There's no easy interface to copying a file,
Then the boolean method exists is invoked in the if that will be covered later on with writing files.
statement to check the files existence. Ref. Speaker notes for Reading, Writing, Appending and Copying
a file

40
Multithreading

• Multithreading in java is a process of


executing multiple threads simultaneously.
• Thread is basically a lightweight sub-
process, a smallest unit of processing.
Multiprocessing and multithreading, both
are used to achieve multitasking.
• We use multithreading than
multiprocessing because threads share a
common memory area. They don't
allocate separate memory area so saves
memory, and context-switching between
the threads takes less time than process.
• Java Multithreading is mostly used in
games, animation etc.

41
Java Garbage collection

• In java, garbage means unreferenced objects.


• Garbage Collection is process of reclaiming the
runtime unused memory automatically. In other
words, it is a way to destroy the unused objects.
• To do so, we were using free() function in C
language and delete() in C++. But, in java it is
performed automatically. So, java provides
better memory management.
• Advantage of Garbage Collection
• It makes java memory efficient because garbage
collector removes the unreferenced objects
from heap memory.
• It is automatically done by the garbage
collector(a part of JVM) so we don't need to
make extra efforts.

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

You might also like