SlideShare a Scribd company logo
Java - Overview
Basics of Java
Introduction of Java
• Java is one of the powerful general-purpose programming languages, created
in 1995 by Sun Microsystems (now owned by Oracle).
• Java is Object-Oriented.
• Syntax of Java is similar to C/C++ but doesn’t allow low level
programming functionality like pointers.
• Java code is always written in the form of classes and objects. Android
heavily relies on the Java programming language all the
• SDKs required to build for Android applications use the standard
libraries of Java.
Features of Java
Java is a simple language:
• Syntax is similar to C++.
• It has excluded some features like explicit pointers and operator
overloading.
• It has provided the garbage collector for memory management. It
helps in automatically delete unused objects.
Java is a multi-threaded language:
• Java can perform many tasks at once by defining multiple
threads.
• For example, a program that manages a Graphical User
Interface (GUI) while waiting for input from a network
connection uses another thread to perform and waits instead
of using the default GUI thread for both tasks. This keeps the
GUI responsive.
Features of Java
Java is an object-oriented programming language.
Java is a platform-independent language:
• The programs written in Java language, after compilation, are
converted into an intermediate-level language called the bytecode,
which is a part of the Java platform irrespective of the machine on
which the programs run.
• This makes Java highly portable as its bytecodes can be run on any
machine by an interpreter called the Java Virtual Machine(JVM), and
thus, java provides ‘reusability of code’.
Identifiers in Java
Identifiers: Identifiers in Java are symbolic namesused for identification.
They can be a class name, variable name, method name, package name, constant
name, and more.
Identifiers in Java
All the marked section of the above code snippet is a type of identifier.
They are described as:
1 HelloJava (Class name),
2 main (main method),
3 String (Predefined Class name),
4 args (String variables),
5 System (Predefined class),
6 out (Variable name), and
7 println (method).
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1
1. Primitive data types: The primitive data types include boolean,
char, byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types include
Classes, Interfaces, and Arrays.
Data Types in Java
Data Types in Java
Figure 1: List of different datatypes
Boolean Type
A boolean data type can store either True or False.
Figure 2: Example illustrating boolean datatype
Figure 3: Output of the above code
Character Type
The char data type stores a single character.
Figure 4: Example illustrating char datatype
Figure 5: Output of the above code
Integer
Type
An integer type stores an integer number with no fractional or decimal places.
Java has four integer types – byte, short, int, and long.
Figure 6: Example illustrating integer datatype
Integer
Type
Figure 7: Output of the above
code
Float Type
Floating-point is used for expressions involving fractional precision. It has two
types: float and double.
Figure 8: Example illustrating float datatype
Arrays
In Java, Array is a group of like-typed variables referred to by a common name.
Arrays in Java work differently than they do in C/C++. Following aresome
important points about Java arrays.
1. In Java, all arrays are dynamically allocated. Arrays may be stored in
contiguous memory.
2. Since arrays are objects in Java, we can find their length using the object
property length. This is different from C/C++, where we find length using
sizeof.
3. A Java array variable can also be declared like other variables with [] after
the data type.
4. The variables in the array are ordered, and eachhas an index beginning with
0.
5. Java array can also be used as a static field, a local variable, or a method
parameter.
Creating, Initializing, and Accessing an Arrays
One-Dimensional Arrays
Syntax to declare an array
 dataType var-name[];
 dataType[] var-name;
Instantiation of an Array in
Java
arrayRefVar=new
datatype[size];
• Multidimensional Array
Syntax to declare multi-
dimensional array
 dataType[][] arrayRefVar;
 dataType arrayRefVar[][];
Instantiation of a multi-dimensional
array in Java
Example of 1-D Array
Figure 10: 1-D array code
Figure 11: Output of the above c ode
Example of 2-D Array
Figure 12: 2-D array
code
Example of 2-D Array
Figure 13: Output of the above
code
ArrayIndexOutOfBoundsException
The Java Virtual Machine (JVM) throws an
ArrayIndexOutOfBoundsException if length of the array in negative, equal to the
array size or greater than the array size while traversing the array.
Figure 14: Erroneous code
Figure 15: Output of the above code
Decision Making Statements
in Java
if-else Statements
A programming language uses control statements to control the flow of
execution of a program based on certain conditions.
These areused to cause the flow of execution to advance and branch based on
changes to the state of a program.
• if statement: It is used to decide whether a certain statement or block of
statements will beexecutedor not depending on certain conditions.
• else if statement: It is similar to if statement. It is usedwhen we want to put
more than on condition.
• else statement: It comes at the end of the if-else-if ladder. It will run
when none of the conditions is true.
if-else Statement
Figure 16: Basic structure of if-else-if statements
if-else Statement
Figure 17: Output will be 20 in the above case
Loops in Java
Looping in programming languages is a feature that facilitates the
execution of a set of instructions/functions repeatedly while some
condition evaluates to true.
Java provides 3 types of loops.
1. while: A while loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition. The while
loop can be thought of as a repeating if statement.
2. for: Unlike a while loop, a for statement consumes the initialization,
condition and increment/decrement in one line.
3. do while: do while loop is similar to a while loop with the only
difference that it checks for the conditions after executing the
statements once.
while Loop
Figure 18: Simple example displaying function of while loop
while Loop
Figure 19: Output for the above case (Same for and do-while, which is coming in the next slides)
for Loop
Figure 20: Simple example displaying function of for loop
do while Loop
Figure 21: Simple example displaying function of do while loop
for-each Loop
For-each is another array traversing technique like for loop, while loop, do-
while loop introduced in Java5.
Figure 22: Simple example displaying function of for each loop
Figure 23: Output for the above for-each loop code
Type conversion in Java
An overview
Type Conversion
When we assign a value of one data type to another, the two types might not be
compatible with each other. If the data types are compatible, then Java will
perform the conversion automatically, known asAutomatic Type Conversion, and
if not, then they need to be cast or converted explicitly. For example, assigning an
int value to a long variable.
Widening or Automatic Type Conversion: Widening conversion occurs when
two data types are automatically converted. This happens when:
• The two data types are compatible.
• When we assign a value of a smaller data type to a bigger data type.
Narrowing or Explicit Conversion: If we want to assign a value of a larger data
type to a smaller data type, we perform explicit type casting or narrowing.
• This is useful for incompatible data types where automatic conversion
cannot be done.
• Here, the target type specifies the desired type to convert the specified
value to.
Explicit Conversion Example
Strings in Java
An overview
Introduction of String
The string is a sequence of characters. In Java, objects of String are immutable
which means a constant and cannot be changed once created.
Creating a String: There are two ways to create a string in Java.
String literal
String s = “GeeksforGeeks”;
Using new keyword
String s = new String (“GeeksforGeeks”);
String’s Method in Java
int length()
Returns the number of characters in the String.
“GeeksforGeeks”.length(); // returns 13
Char charAt(int i)
Returns the character at ith index.
“GeeksforGeeks”.charAt(3); // returns ‘k’
String substring (int i)
Return the substring from the ith index character to end.
“GeeksforGeeks”.substring(3); // returns “ksforGeeks”
String substring (int i, int j)
Returns the substring from i to j-1 index.
“GeeksforGeeks”.substring(2, 5); // returns “eks”
String’s Method in Java
String concat( String str)
1Concatenates specified string to the end of this string.
2String s1 = ”Geeks”;
String s2 = ”forGeeks”;
String output =
s1.concat(s2); // returns
“GeeksforGeeks”
int indexOf (String s)
Returns the index within the string of the first occurrence of the
specified string.
String s = ”Learn Share Learn”;
int output = s.indexOf(“Share”); // returns 6
int indexOf (String s, int i)
Returns the index within the string of the first occurrence of the
specified string, starting at the specified index.
String s = ”Learn Share Learn”;
int output = s.indexOf(”ea”,3);// returns 13
Exception Handling in Java
An
overview
Exception Handling in Java
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that the normal flow of the application can be maintained.
What is Exception in Java?
In Java, an exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
What is Exception Handling?
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException,
RemoteException, etc.
Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow
of the application. An exception normally disrupts the normal flow of the
application; that is why we need to handle exceptions.
Hierarchy of Java Exception Classes
The java.lang.Throwable class is the root class of Java Exception hierarchy
inherited by two subclasses: Exception and Error. The hierarchy of Java
Exception classes is given below:
Hierarchy of Java Exception Classes
Java Exception Keywords
Keyword Description
try
The ”try” keyword is used to specify a block where we
should place an exception code. It means we can’t use try
block alone. The try block must be followed by either catch
or finally.
catch
The ”catch” block is used to handle the exception. It
must be precededby try block which means we can’t use
catch block alone. It can be followed by finally block later.
finally
The ”finally” block is used to execute the necessary
code of the program. It is executed whether an
exception is handled or not.
throw The ”throw” keyword is used to throw an exception.
throws
The ”throws” keyword is used to declare exceptions.
It specifies that there may occur an exception in the
method. It doesn’t throw an exception. It is always used
with method signature.
Java try-catch Block
Java try block is used to enclose the code that might throw an exception.If an
exception occurs at the particular statement in the try block, the rest of the block
code will not execute. So, it is recommended not to keep the code in try block that
will not throw an exception.
Java catch block is used to handle the Exception by declaring the type of
exception within the parameter. The declared exception must be the parent class
exception ( i.e., Exception) or the generated exception type.
Try-catch Example
Figure 24: Try-catch block example
Figure 25: Output of the above c ode
Thank You

More Related Content

Similar to JAVA Basics Presentation for introduction to JAVA programming languauge (20)

UNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptxUNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptx
amanuel236786
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
University of Potsdam
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
RajkumarHarishchandr1
 
2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf
AbhishekSingh757567
 
Let's start with Java- Basic Concepts
Let's start with Java- Basic ConceptsLet's start with Java- Basic Concepts
Let's start with Java- Basic Concepts
Aashish Jain
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx
320126552027SURAKATT
 
JAVA Class Presentation.pdf Vsjsjsnheheh
JAVA Class Presentation.pdf VsjsjsnhehehJAVA Class Presentation.pdf Vsjsjsnheheh
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Java
JavaJava
Java
Raghu nath
 
Unit-1 Data Types and Operators.pptx to computers
Unit-1 Data Types and Operators.pptx to computersUnit-1 Data Types and Operators.pptx to computers
Unit-1 Data Types and Operators.pptx to computers
csangani1
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
JitendraYadav351971
 
Basics java programing
Basics java programingBasics java programing
Basics java programing
Darshan Gohel
 
Java_Roadmap.pptx
Java_Roadmap.pptxJava_Roadmap.pptx
Java_Roadmap.pptx
ssuser814cf2
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
ManikandanRamanujam
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
Madishetty Prathibha
 
Java 8
Java 8Java 8
Java 8
Sudipta K Paik
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
chnrketan
 
Data types ^J variables and arrays in Java.pptx
Data types ^J variables and arrays in Java.pptxData types ^J variables and arrays in Java.pptx
Data types ^J variables and arrays in Java.pptx
sksumayasumaya5
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
Java basic
Java basicJava basic
Java basic
Pooja Thakur
 
UNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptxUNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptx
amanuel236786
 
2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf
AbhishekSingh757567
 
Let's start with Java- Basic Concepts
Let's start with Java- Basic ConceptsLet's start with Java- Basic Concepts
Let's start with Java- Basic Concepts
Aashish Jain
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx
320126552027SURAKATT
 
JAVA Class Presentation.pdf Vsjsjsnheheh
JAVA Class Presentation.pdf VsjsjsnhehehJAVA Class Presentation.pdf Vsjsjsnheheh
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Unit-1 Data Types and Operators.pptx to computers
Unit-1 Data Types and Operators.pptx to computersUnit-1 Data Types and Operators.pptx to computers
Unit-1 Data Types and Operators.pptx to computers
csangani1
 
Basics java programing
Basics java programingBasics java programing
Basics java programing
Darshan Gohel
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
Madishetty Prathibha
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
chnrketan
 
Data types ^J variables and arrays in Java.pptx
Data types ^J variables and arrays in Java.pptxData types ^J variables and arrays in Java.pptx
Data types ^J variables and arrays in Java.pptx
sksumayasumaya5
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 

Recently uploaded (20)

SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) ProjectMontreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdfIntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdf
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) ProjectMontreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
Ad

JAVA Basics Presentation for introduction to JAVA programming languauge

  • 2. Introduction of Java • Java is one of the powerful general-purpose programming languages, created in 1995 by Sun Microsystems (now owned by Oracle). • Java is Object-Oriented. • Syntax of Java is similar to C/C++ but doesn’t allow low level programming functionality like pointers. • Java code is always written in the form of classes and objects. Android heavily relies on the Java programming language all the • SDKs required to build for Android applications use the standard libraries of Java.
  • 3. Features of Java Java is a simple language: • Syntax is similar to C++. • It has excluded some features like explicit pointers and operator overloading. • It has provided the garbage collector for memory management. It helps in automatically delete unused objects. Java is a multi-threaded language: • Java can perform many tasks at once by defining multiple threads. • For example, a program that manages a Graphical User Interface (GUI) while waiting for input from a network connection uses another thread to perform and waits instead of using the default GUI thread for both tasks. This keeps the GUI responsive.
  • 4. Features of Java Java is an object-oriented programming language. Java is a platform-independent language: • The programs written in Java language, after compilation, are converted into an intermediate-level language called the bytecode, which is a part of the Java platform irrespective of the machine on which the programs run. • This makes Java highly portable as its bytecodes can be run on any machine by an interpreter called the Java Virtual Machine(JVM), and thus, java provides ‘reusability of code’.
  • 5. Identifiers in Java Identifiers: Identifiers in Java are symbolic namesused for identification. They can be a class name, variable name, method name, package name, constant name, and more.
  • 6. Identifiers in Java All the marked section of the above code snippet is a type of identifier. They are described as: 1 HelloJava (Class name), 2 main (main method), 3 String (Predefined Class name), 4 args (String variables), 5 System (Predefined class), 6 out (Variable name), and 7 println (method).
  • 7. Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: 1 1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. 2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays. Data Types in Java
  • 8. Data Types in Java Figure 1: List of different datatypes
  • 9. Boolean Type A boolean data type can store either True or False. Figure 2: Example illustrating boolean datatype Figure 3: Output of the above code
  • 10. Character Type The char data type stores a single character. Figure 4: Example illustrating char datatype Figure 5: Output of the above code
  • 11. Integer Type An integer type stores an integer number with no fractional or decimal places. Java has four integer types – byte, short, int, and long. Figure 6: Example illustrating integer datatype
  • 12. Integer Type Figure 7: Output of the above code
  • 13. Float Type Floating-point is used for expressions involving fractional precision. It has two types: float and double. Figure 8: Example illustrating float datatype
  • 14. Arrays In Java, Array is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following aresome important points about Java arrays. 1. In Java, all arrays are dynamically allocated. Arrays may be stored in contiguous memory. 2. Since arrays are objects in Java, we can find their length using the object property length. This is different from C/C++, where we find length using sizeof. 3. A Java array variable can also be declared like other variables with [] after the data type. 4. The variables in the array are ordered, and eachhas an index beginning with 0. 5. Java array can also be used as a static field, a local variable, or a method parameter.
  • 15. Creating, Initializing, and Accessing an Arrays One-Dimensional Arrays Syntax to declare an array  dataType var-name[];  dataType[] var-name; Instantiation of an Array in Java arrayRefVar=new datatype[size]; • Multidimensional Array Syntax to declare multi- dimensional array  dataType[][] arrayRefVar;  dataType arrayRefVar[][]; Instantiation of a multi-dimensional array in Java
  • 16. Example of 1-D Array Figure 10: 1-D array code Figure 11: Output of the above c ode
  • 17. Example of 2-D Array Figure 12: 2-D array code
  • 18. Example of 2-D Array Figure 13: Output of the above code
  • 19. ArrayIndexOutOfBoundsException The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length of the array in negative, equal to the array size or greater than the array size while traversing the array. Figure 14: Erroneous code Figure 15: Output of the above code
  • 21. if-else Statements A programming language uses control statements to control the flow of execution of a program based on certain conditions. These areused to cause the flow of execution to advance and branch based on changes to the state of a program. • if statement: It is used to decide whether a certain statement or block of statements will beexecutedor not depending on certain conditions. • else if statement: It is similar to if statement. It is usedwhen we want to put more than on condition. • else statement: It comes at the end of the if-else-if ladder. It will run when none of the conditions is true.
  • 22. if-else Statement Figure 16: Basic structure of if-else-if statements
  • 23. if-else Statement Figure 17: Output will be 20 in the above case
  • 24. Loops in Java Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Java provides 3 types of loops. 1. while: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. 2. for: Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line. 3. do while: do while loop is similar to a while loop with the only difference that it checks for the conditions after executing the statements once.
  • 25. while Loop Figure 18: Simple example displaying function of while loop
  • 26. while Loop Figure 19: Output for the above case (Same for and do-while, which is coming in the next slides)
  • 27. for Loop Figure 20: Simple example displaying function of for loop
  • 28. do while Loop Figure 21: Simple example displaying function of do while loop
  • 29. for-each Loop For-each is another array traversing technique like for loop, while loop, do- while loop introduced in Java5. Figure 22: Simple example displaying function of for each loop Figure 23: Output for the above for-each loop code
  • 30. Type conversion in Java An overview
  • 31. Type Conversion When we assign a value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically, known asAutomatic Type Conversion, and if not, then they need to be cast or converted explicitly. For example, assigning an int value to a long variable. Widening or Automatic Type Conversion: Widening conversion occurs when two data types are automatically converted. This happens when: • The two data types are compatible. • When we assign a value of a smaller data type to a bigger data type. Narrowing or Explicit Conversion: If we want to assign a value of a larger data type to a smaller data type, we perform explicit type casting or narrowing. • This is useful for incompatible data types where automatic conversion cannot be done. • Here, the target type specifies the desired type to convert the specified value to.
  • 33. Strings in Java An overview
  • 34. Introduction of String The string is a sequence of characters. In Java, objects of String are immutable which means a constant and cannot be changed once created. Creating a String: There are two ways to create a string in Java. String literal String s = “GeeksforGeeks”; Using new keyword String s = new String (“GeeksforGeeks”);
  • 35. String’s Method in Java int length() Returns the number of characters in the String. “GeeksforGeeks”.length(); // returns 13 Char charAt(int i) Returns the character at ith index. “GeeksforGeeks”.charAt(3); // returns ‘k’ String substring (int i) Return the substring from the ith index character to end. “GeeksforGeeks”.substring(3); // returns “ksforGeeks” String substring (int i, int j) Returns the substring from i to j-1 index. “GeeksforGeeks”.substring(2, 5); // returns “eks”
  • 36. String’s Method in Java String concat( String str) 1Concatenates specified string to the end of this string. 2String s1 = ”Geeks”; String s2 = ”forGeeks”; String output = s1.concat(s2); // returns “GeeksforGeeks” int indexOf (String s) Returns the index within the string of the first occurrence of the specified string. String s = ”Learn Share Learn”; int output = s.indexOf(“Share”); // returns 6 int indexOf (String s, int i) Returns the index within the string of the first occurrence of the specified string, starting at the specified index. String s = ”Learn Share Learn”; int output = s.indexOf(”ea”,3);// returns 13
  • 37. Exception Handling in Java An overview
  • 38. Exception Handling in Java The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained. What is Exception in Java? In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. What is Exception Handling? Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Advantage of Exception Handling The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application; that is why we need to handle exceptions.
  • 39. Hierarchy of Java Exception Classes The java.lang.Throwable class is the root class of Java Exception hierarchy inherited by two subclasses: Exception and Error. The hierarchy of Java Exception classes is given below:
  • 40. Hierarchy of Java Exception Classes
  • 41. Java Exception Keywords Keyword Description try The ”try” keyword is used to specify a block where we should place an exception code. It means we can’t use try block alone. The try block must be followed by either catch or finally. catch The ”catch” block is used to handle the exception. It must be precededby try block which means we can’t use catch block alone. It can be followed by finally block later. finally The ”finally” block is used to execute the necessary code of the program. It is executed whether an exception is handled or not. throw The ”throw” keyword is used to throw an exception. throws The ”throws” keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn’t throw an exception. It is always used with method signature.
  • 42. Java try-catch Block Java try block is used to enclose the code that might throw an exception.If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception. Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type.
  • 43. Try-catch Example Figure 24: Try-catch block example Figure 25: Output of the above c ode