3-1. Fundamentals of Java
3-1. Fundamentals of Java
By:
Archana Chaudhari
26-04-2021 1
Syllabus of Unit III
Fundamentals of JAVA, Arrays: one dimensional array, multi-
dimensional array, alternative array declaration statements,
String Handling: String class methods, Classes and Methods: class
fundamentals, declaring objects, assigning object reference
variables, adding methods to a class, returning a value,
constructors, this keyword, garbage collection, finalize() method,
overloading methods, argument passing, object as parameter,
returning objects, access control, static, final, nested and inner
classes, command line arguments, variable -length arguments.
26-04-2021 2
Why Java is Important?
• Two reasons :
– Trouble with C/C++ language is that they are not portable and are not
platform independent languages.
26-04-2021 3
Why to learn JAVA ?
26-04-2021 4
History
• James Gosling - Sun Microsystems in 1991
• Oak - Java, May 20, 1995, Sun World
• Java Editions
– J2SE(Java 2 Standard Edition) - to develop client-side standalone
applications or applets.
– J2ME(Java 2 Micro Edition ) - to develop applications for mobile
devices such as cell phones.
– J2EE(Java 2 Enterprise Edition ) - to develop server-side applications
such as Java servlets and Java Server Pages
26-04-2021 5
What is Java
26-04-2021 6
JAVA APPLICATIONS
26-04-2021 7
How is Java different from C
• C Language:
– Major difference is that C is a structure oriented language and Java is
an object oriented language and has mechanism to define classes
and objects
– Java does not support an explicit pointer type
– Java does not have preprocessor, so we cant use #define, #include
and #ifdef statements.
– Java does not include structures, unions and enum data types.
– Java does not include keywords like goto, sizeof and typedef.
– Java adds many features required for object oriented programming.
26-04-2021 8
How is Java different from C++
• C++ Language:
Features removed in java:
– Java doesn’t support pointers to avoid unauthorized access of
memory locations.
– Java does not include structures, unions and enum data types.
– Java does not support operator over loading.
– Preprocessor plays less important role in C++ and so eliminated
entirely in java.
– Java does not perform automatic type conversions that result in loss
of precision
26-04-2021 9
How is Java different from C++
(Cont..)
– Java does not support global variables. Every method and variable is
declared within a class and forms part of that class.
– Java does not allow default arguments.
– Java does not support inheritance of multiple super classes by a sub
class (i.e., multiple inheritance). This is accomplished by using
‘interface’ concept.
– It is not possible to declare unsigned integers in java.
– In java objects are passed by reference only. In C++ objects may be
passed by value or reference
26-04-2021 10
New features added in Java
• Multithreading, that allows two or more pieces of the same
program to execute concurrently.
• C++ has a set of library functions that use a common header file.
But java replaces it with its own set of API classes.
• It adds packages and interfaces.
• Java supports automatic garbage collection.
• break and continue statements have been enhanced in java to
accept labels as targets.
• The use of unicode characters ensures portability.
• Java has replaced the destructor function with a finalize() function.
• C++ supports exception handling that is similar to java's. However,
in C++ there is no requirement that a thrown exception be caught. 11
26-04-2021
JAVA FEATURES
26-04-2021 12
Object-oriented
• Java is an object-oriented programming language.
• Everything in Java is an object.
• Basic concepts of OOPs are:
– Object
– Class
– Inheritance
– Polymorphism
– Abstraction
– Encapsulation
26-04-2021 13
Simple
• Java is very easy to learn, and its syntax is simple, clean and easy to
understand.
• According to Sun, Java language is a simple programming language
because:
– Java syntax is based on C++ (so easier for programmers to learn it after
C++).
– Java has removed many complicated and rarely-used features, Ex. explicit
pointers, operator overloading, etc.
– There is no need to remove unreferenced objects because there is an
Automatic Garbage Collection in Java.
26-04-2021 14
Secured
• Java is secured because:
– No explicit pointer
– Java Programs run inside a virtual machine sandbox
26-04-2021 15
Platform Independent
• Java provides a software-based platform.
• It has two components:
– Runtime Environment
– API(Application Programming Interface)
• Java code can be run on multiple platforms
• Java code is compiled by the compiler and
converted into bytecode.
• This bytecode is a platform-independent code
because it can be run on multiple platforms,
i.e., Write Once and Run Anywhere(WORA).
26-04-2021 16
Robust
• Robust simply means strong. Java is robust because:
– It uses strong memory management.
– There is a lack of pointers that avoids security problems.
– There is automatic garbage collection in java which runs on the Java
Virtual Machine to get rid of objects which are not being used by a
Java application anymore.
– There are exception handling and the type checking mechanism in
Java.
26-04-2021 17
Portable
• Java is portable because it facilitates you to carry the Java
bytecode to any platform. It doesn't require any
implementation.
26-04-2021 18
Architecture-neutral
• Java is architecture neutral because there are no
implementation dependent features,
– for example, the size of primitive types is fixed.
• In C programming, int data type occupies 2 bytes of memory
for 32-bit architecture and 4 bytes of memory for 64-bit
architecture.
• However, it occupies 4 bytes of memory for both 32 and 64-bit
architectures in Java.
26-04-2021 19
Dynamic
• Java is a dynamic language. It supports dynamic loading of
classes. It means classes are loaded on demand.
• It also supports functions from its native languages, i.e., C and
C++.
• Java supports dynamic compilation and automatic memory
management (garbage collection).
26-04-2021 20
Interpreted & High Performance
• Java is faster than other traditional interpreted programming
languages because Java bytecode is "close" to native code.
• It is still a little bit slower than a compiled language (e.g., C++).
Java is an interpreted language that is why it is slower than
compiled languages, e.g., C, C++, etc.
26-04-2021 21
Multithreaded
• A thread is like a separate program, executing concurrently.
• We can write Java programs that deal with many tasks at once
by defining multiple threads.
• Advantage
– it doesn't occupy memory for each thread.
– It shares a common memory area.
• Threads are important for multi-media, Web applications, etc.
26-04-2021 22
Distributed
• Java is distributed because it facilitates users to create
distributed applications in Java.
• RMI and EJB are used for creating distributed applications.
• This feature of Java makes us able to access files by calling the
methods from any machine on the internet.
26-04-2021 23
JAVA VIRTUAL MACHINE (JVM)
26-04-2021 24
Java Program Structure
• It’s optional write a comment in this
• A package is a group of classes section
that are defined by a name.
• package javatpoint;
if you want to use a class of
another package, then you can do
this by importing it directly into
• It’s optional your program
• Interfaces are like a class except import java.util.Scanner;
it contains only constants and
method declarations
• By using the implements Classes are the main and essential
keyword elements of any Java program.
• It is an essential part
• the main method as the starting
point of the program.
• There may be many classes in a
Java program, and only one class
26-04-2021 25
defines the main method.
First Java Program
• For executing any java program, you need to Install the JDK if
you don't have installed it, download the JDK and install it.
• Set path of the jdk/bin directory.
http://www.javatpoint.com/how-to-set-path-in-java
• Create the java program
• Compile and run the java program
26-04-2021 26
Demo
//Name of this file will be "Simple.java"
class Simple
{
/* Writes the words "Hello Java" on the screen */
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
26-04-2021 27
Parameter Used in the Program
public class Simple • This creates a class called Simple.
• All class names must start with a capital letter.
• The public word means that it is accessible from any other classes.
/* Comments */ The compiler ignores comment block.
Braces Two curly brackets {...} are used to group all the commands, so it is known
that the commands belong to that class or method.
public static void • When the main method is declared public, it means that it can also be
main used by code outside of its class, due to which the main method is
declared public.
• The word static used when we want to access a method without creating
its object, as we call the main method, before creating any class objects.
• The word void indicates that a method does not return a value. main() is
declared as void because it does not return a value.
• main is a method; this is a starting point of a Java program.
26-04-2021 28
Parameter Used in the Program
String[] args • It is an array where each element of it is a string, which has been
named as "args".
• If your Java program is run through the console, you can pass the input
parameter, and main() method takes it as input.
System.out.printl • This statement is used to print text on the screen as output,
n(); • where the system is a predefined class, and out is an object of
the PrintWriter class defined in the system.
• The method println prints the text on the screen with a new line.
• You can also use print() method instead of println() method.
• All Java statement ends with a semicolon.
26-04-2021 29
VARIABLE IN JAVA
26-04-2021 30
VARIABLE IN JAVA
26-04-2021 31
VARIABLE IN JAVA (CONT..)
26-04-2021 32
VARIABLE IN JAVA (CONT..)
26-04-2021 33
VARIABLE IN JAVA(CONT..)
This is called as
Variable. Which
stored last updates
made
26-04-2021 34
DECLARE VARIABLE IN JAVA
26-04-2021 35
TYPES OF VARIABLE
26-04-2021 36
Local Variable
• A variable defined within a block public class StudentDetails {
public void StudentAge()
or method or constructor is {
// local variable age
called local variable. int age = 0;
• scope is only within the block in age = age + 5;
System.out.println("Student age is : "
which the variable is declared. + age);
}
i.e. we can access these variable
only within that block. public static void main(String args[])
{
• Initialization of Local Variable is StudentDetails obj = new
StudentDetails();
Mandatory. obj.StudentAge();
}
}
26-04-2021 37
Instance Variable
• Instance variables are non-static import java.io.*;
class Marks { // These variables are instance variables.
variables and are declared in a int engMarks;
int mathsMarks;
class outside any method, int phyMarks;
}
constructor or block. class MarksDemo {
• Initialization of Instance Variable is public static void main(String args[]) {
// first object
not Mandatory. Its default value is Marks obj1 = new Marks();
obj1.engMarks = 50;
0 obj1.mathsMarks = 80;
obj1.phyMarks = 90;
• Instance Variable can be accessed // second object
Marks obj2 = new Marks();
only by creating objects. obj2.engMarks = 80;
obj2.mathsMarks = 60;
26-04-2021 obj2.phyMarks = 85; 38
}}
Static Variable
• Static variables are also known as import java.io.*;
class Emp {
Class variables.
• Unlike instance variables, we can // static variable salary
public static double salary;
only have one copy of a static public static String name = "Harsh";
variable per class irrespective of }
how many objects we create.
public class EmpDemo {
• Its default value is 0 public static void main(String args[])
• To access static variables, we need {
not create an object of that class, // accessing static variable without object
we can simply access the variable Emp.salary = 1000;
System.out.println(Emp.name + "'s average
as salary:"
class_name.variable_name; + Emp.salary);
26-04-2021 } 39
}
Instance variable Vs Static variable
Instance Variable Static Variable
• Each object will have its own copy of • only one copy of a static variable per
instance variable class irrespective of how many objects
• Changes made in an instance variable we create
using one object will not be reflected • changes will be reflected in other
in other objects as each object has its objects as static variables are common
own copy of instance variable. to all object of a class.
• We can access instance variables • Static Variables can be accessed
through object references directly using class name
• int b; //instance variable • static int a; //static variable
26-04-2021 40
DATA TYPES IN JAVA(CONT..)
26-04-2021 41
DATA TYPES IN JAVA(CONT..)
Range, the number of negative values of the data types is same as that
of positive values
=>-2^(8-1) to (2^(8-1))-1
=>-2^(7) to (2^(7))-1
=>-128 to 127
26-04-2021 42
DATA TYPES IN JAVA(CONT..)
26-04-2021 43
Why char uses 2 byte in java and what is \u0000 ?
• It is because java uses Unicode system not ASCII code system.
• The \u0000 is the lowest range of Unicode system.
26-04-2021 44
STATEMENTS IN JAVA(CONT..)
26-04-2021 45
STOP 1 STOP 2 STOP 3
EMPTY
BUS
26-04-2021 46
STOP 1 STOP 2 STOP 3
2 passengers pickup
26-04-2021 47
STOP 1 STOP 2 STOP 3
3 passengers pickup
and 2 passengers drop
26-04-2021 48
STOP 1 STOP 2 STOP 3
5 passengers
pickup and 3
passengers drop
26-04-2021 49
STATEMENT IN JAVA(CONT..)
Java
Statements
Control
Declaration Expression
Flow
26-04-2021 51
DECLARATION & EXPRESSION STATEMENT
IF ELSE
DO WHILE SWITCH
LOOP
CONTROL
STATEMENTS
WHILE
FOR LOOP
LOOP
26-04-2021 53
CONTROL STATEMENT IN JAVA
(cont..)
26-04-2021 54
CONTROL STATEMENT IN JAVA
(cont..)
If (light == “red”)
System.out.println(“STOP”);
else if( light==“yellow”)
System.out.println(“What??”);
else if (light==“green”)
System.out.println(“Go”);
else
System.out.println(“No signal”);
26-04-2021 55
26-04-2021 56
exp //input to case
switch (exp)
{
case 1:
break;
case 2:
break;
.
.
.
.
.
.
26-04-2021 57
26-04-2021 58
for ( initialization; condition; increment/decrement)
or
for (type variableName : arrayName)
{
// code block to be executed
}
initialization
while(condition)
{
increment/decrement
}
initialization
do
{
increment/decrement
} while(condition);
26-04-2021 59
IMAGINE BELOW IMAGES
26-04-2021 60
ARRAYS
26-04-2021 61
ARRAYS (cont…)
26-04-2021 63
Types of Array in java
• Single Dimensional Array
• Multidimensional Array
26-04-2021 64
ONE DIMENSIONAL ARRAY
26-04-2021 65
Declaration, Instantiation and Initialization of
Java Array
26-04-2021 66
Return an Array in Java
Fundamentals of JAVA
class TestReturnArray{
static int[] get()
{
return new int[]{10,30,50,90,60};
}
public static void main(String args[])
{
int arr[]=get();
for(int i=0;i<arr.length ; i++) //printing array using for-each loop
System.out.println(arr[i]);
}
}
26-04-2021 67
Anonymous Array in Java
Java supports the feature of an anonymous array, so you don't need to declare the array
while passing an array to the method
public class TestAnonymousArray
{
static void printArray(int arr[]) //creating a method which receives an array as a parameter
{
for(int i:arr)
System.out.println(arr[i]);
}
public static void main(String args[])
{
printArray(new int[]{10,22,44,66}); //passing anonymous array to method
}
}26-04-2021 68
Multidimensional Array
• Data is stored in row and column based index (also known as
matrix form).
26-04-2021 69
Syntax to Declare Multidimensional Array in Java
26-04-2021 70
Example of Multidimensional Java Array
class Testarray3{
public static void main(String args[]){ //declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
26-04-2021 71
Jagged Array in Java
• It is an array of arrays
with different number
of columns.
26-04-2021 72
Home Work
26-04-2021 73