0% found this document useful (0 votes)
19 views191 pages

Introduction to Java Programming Basics

Java is an object-oriented, class-based programming language developed by Sun Microsystems in 1995. It supports various application types, including desktop, web, enterprise, and mobile applications, and operates on multiple platforms such as Java SE, EE, ME, and JavaFX. The document also covers the structure of a basic Java program, the requirements for running Java applications, and the fundamental concepts of Java variables, data types, and the Java Development Kit (JDK).

Uploaded by

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

Introduction to Java Programming Basics

Java is an object-oriented, class-based programming language developed by Sun Microsystems in 1995. It supports various application types, including desktop, web, enterprise, and mobile applications, and operates on multiple platforms such as Java SE, EE, ME, and JavaFX. The document also covers the structure of a basic Java program, the requirements for running Java applications, and the fundamental concepts of Java variables, data types, and the Java Development Kit (JDK).

Uploaded by

Jayadeep Royal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is JAVA?

Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Object Oriented
Programming
Module 1

Dr. Nikhil Mhala

[Link]@[Link]
School of Computer Science and Engineering (SCOPE)
VIT-AP University (Besides AP Secretariat) 522237

April 27, 2022

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 1 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Agenda I

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 2 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Agenda II
1 What is JAVA? 14 Java Switch Statement
2 Application 15 Java While Loop
16 Java break Statement
3 Types of Java Applications
17 Java continue Statement
4 Java Platforms / Editions
18 Java Array
5 Features of Java
19 Looping Through Array Ele-
6 Java Hello World Program ments
7 The requirement for Java 20 Multidimensional Arrays
Hello World Example 21 Java Copy Arrays
How Java "Hello World ! " 22 Java OOPs Concepts
program works? 23 Java Class and Objects
8 Java JDK, JRE and JVM 24 Java Methods
9 Java Variables and Literals 25 Java Method Overloading
10 Java Data Types 26 Java Constructor
11 Java Operators 27 Constructors Overloading in
12 Java Basic Input and Output Java
13 Conditional and Control State- 28 Java String
ment in Java 29 Java Access Modifiers
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 3 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What is JAVA?

Java is an object-oriented, class-based, concurrent, secured and


general-purpose computer-programming language.
Java was developed by Sun Microsystems (which is now the
subsidiary of Oracle) in the year 1995
James Gosling is known as the father of Java.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 4 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Application

1 Desktop Applications such as acrobat reader, media player,


antivirus, etc.
2 Web Applications such as [Link], [Link], etc.
3 Enterprise Applications such as banking applications.
4 Mobile
5 Embedded System
6 Smart Card
7 Robotics
8 Games, etc.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 5 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Types of Java Applications

1 Standalone Application
2 Web Application
3 Enterprise Application
4 Mobile Application

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 6 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Platforms / Editions

1 Java SE (Java Standard Edition)


2 Java EE (Java Enterprise Edition)
3 Java ME (Java Micro Edition)
4 JavaFX

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 7 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Features of Java

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 8 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Hello World Program

1 // Your First Program


2
3 class HelloWorld {
4 public static void main ( String [] args ) {
5 System . out . println ( " Hello , World ! " ) ;
6 }
7 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 9 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

The requirement for Java Hello World Exam-


ple

Install Java and set the path


Install the JDK if you don’t have installed it, download the JDK
and install it.
Set path of the jdk/bin directory.
Create the Java program
Compile and run the Java program

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 10 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How Java "Hello World ! " program works?

// Your First Program


In Java, any line starting with // is a comment.

class HelloWorld { ... }


In Java, every application begins with a class definition. In the
program, HelloWorld is the name of the class,
Java application has a class definition, and the name of the class
should match the filename in Java

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 11 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How Java "Hello World ! " program works?

public static void main(String[] args) { ... }


This is the main method. Every application in Java must contain
the main method. The Java compiler starts executing the code
from the main method

[Link]("Hello, World!");
The code above is a print statement. It prints the text Hello, World! to
standard output (your screen). The text inside the quotation marks is
called String in Java.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 12 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Things to take away

Every valid Java Application must have a class definition that


matches the filename (class name and file name should be
same).
The main method must be inside the class definition.
The compiler executes the codes starting from the main function.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 13 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java JDK, JRE and JVM

What is JVM?
JVM (Java Virtual Machine) is an abstract machine that
enables your computer to run a Java program.
When you run the Java program, Java compiler first compiles
your Java code to bytecode. Then, the JVM translates bytecode
into native machine code
Java is a platform-independent language.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 14 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What is JRE?
JRE (Java Runtime Environment) is a software package that
provides Java class libraries, Java Virtual Machine (JVM), and
other components that are required to run Java applications.
JRE is the superset of JVM

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 15 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What is JDK?
JDK (Java Development Kit) is a software development kit
required to develop applications in Java. When you download
JDK, JRE is also downloaded with it.
In addition to JRE, JDK also contains a number of development
tools (compilers, JavaDoc, Java Debugger, etc)

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 16 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Variables and Literals

Java Variables
A variable is a location in memory (storage area) to hold data.
To indicate the storage area, each variable should be given a
unique name (identifier).

Creating a variable and changing values of variable


1 int speedLimit = 80;
2
3 speedLimit = 90;
4

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 17 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Rules for Naming Variables in Java

Java is case sensitive


Variables must start with either a letter or an underscore, _ or a
dollar, $ sign
Variable names cannot start with numbers
Variable names can’t use whitespace.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 18 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java literals
Literals are data used for representing fixed values. They can be
used directly in the code.
1 // example
2 int a = 1;
3 float b = 2.5;
4 char c = ’F ’;
5

1. Boolean Literals
1 boolean flag1 = false ;
2 boolean flag2 = true ;
3

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 19 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. Integer Literals

An integer literal is a numeric value.


There are 4 types of integer literals in Java
1 binary (base 2)
2 decimal (base 10)
3 octal (base 8)
4 hexadecimal (base 16)

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 20 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Integer Literals: Example

1 // binary
2 int binaryNumber = 0 b10010 ;
3 // octal
4 int octalNumber = 027;
5
6 // decimal
7 int decNumber = 34;
8
9 // hexadecimal
10 int hexNumber = 0 x2F ; // 0 x represents hexadecimal
11 // binary
12 int binNumber = 0 b10010 ; // 0 b represents binary
13

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 21 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Floating-point Literals

1 class FloatLiteral {
2 public static void main ( String [] args ) {
3
4 double myDouble = 3.4;
5 float myFloat = 3.4 F ;
6
7 // 3.445*10^2
8 double m y D o u b l e S c i e n t i f i c = 3.445 e2 ;
9
10 System . out . println ( myDouble ) ; // prints 3.4
11 System . out . println ( myFloat ) ; // prints 3.4
12 System . out . println ( m y D o u b l e S c i e n t i f i c ) ; // prints
344.5
13 }
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 22 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

4. Character Literals and 5. String literals

Character literals are unicode character enclosed inside single


quotes.
A string literal is a sequence of characters enclosed inside
double-quotes

1 char letter = ’a ’;
2
3 String str1 = " Java Programming " ;
4 String str2 = " OOPs " ;
5

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 23 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Data Types

As the name suggests, data types specify the type of data that
can be stored inside variables in Java.
There are 8 Primitive data types in Java

1 boolean type 5 long type


2 byte type 6 double type
3 short type 7 float type
4 int type 8 char type

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 24 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. boolean type

The boolean data type has two possible values, either true or
false.
Default value: false.
They are usually used for true/false conditions

1 class Boolean {
2 public static void main ( String [] args ) {
3
4 boolean flag = true ;
5 System . out . println ( flag ) ; // prints true
6 }
7 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 25 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. byte type

The byte data type can have values from -128 to 127 (8-bit
signed two’s complement integer).
If it’s certain that the value of a variable will be within -128 to 127,
then it is used instead of int to save memory.
Default value: 0

1 class Byte {
2 public static void main ( String [] args ) {
3
4 byte range ;
5 range = 124;
6 System . out . println ( range ) ; // prints 124
7 }
8 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 26 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. short type

The short data type in Java can have values from -32768 to
32767 (16-bit signed two’s complement integer).
If it’s certain that the value of a variable will be within -32768 and
32767, then it is used instead of other integer data types (int,
long).
Default value: 0

1 class Short {
2 public static void main ( String [] args ) {
3
4 short temperature ;
5 temperature = -200;
6 System . out . println ( temperature ) ; // prints -200
7 }
8 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 27 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

4. int type

The int data type can have values from −231 to 231 − 1 (32-bit
signed two’s complement integer). If you are using Java 8 or
later, you can use an unsigned 32-bit integer. This will have a
minimum value of 0 and a maximum value of 232 − 1. Default
value: 0

1 class Int {
2 public static void main ( String [] args ) {
3
4 int range = -4250000;
5 System . out . println ( range ) ; // print -4250000
6 }
7 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 28 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

5. long type

The long data type can have values from −263 to263 − 1 (64-bit
signed two’s complement integer). If you are using Java 8 or
later, you can use an unsigned 64-bit integer with a minimum
value of 0 and a maximum value of 264 − 1. Default value: 0

1 class LongExample {
2 public static void main ( String [] args ) {
3
4 long range = -42332200000 L ;
5 System . out . println ( range ) ; // prints -42332200000
6 }
7 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 29 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

6. double type

The double data type is a double-precision 64-bit floating-point.


It should never be used for precise values such as currency.
Default value: 0.0 (0.0d)

1 class DoubleExample {
2 public static void main ( String [] args ) {
3
4 double number = -42.3;
5 System . out . println ( number ) ; // prints -42.3
6 }
7 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 30 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

7. float type

The float data type is a single-precision 32-bit floating-point


It should never be used for precise values such as currency.
Default value: 0.0 (0.0f)

1 class FloatExample {
2 public static void main ( String [] args ) {
3
4 float number = -42.3 f ;
5 System . out . println ( number ) ; // prints -42.3
6 }
7 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 31 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

8. char type
It’s a 16-bit Unicode character. The minimum value of the char
data type is ’\u0000’ (0) and the maximum value of the is
’\uffff’.
Default value: ’\u0000’
1 class CharExample {
2 public static void main ( String [] args ) {
3
4 char letter = ’\ u0051 ’;
5 System . out . println ( letter ) ; // prints Q
6
7 char letter1 = ’9 ’;
8 System . out . println ( letter1 ) ; // prints 9
9
10 char letter2 = 65;
11 System . out . println ( letter2 ) ; // prints A
12
13 }
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 32 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Operators

Operators in Java can be classified into 6 types:


1 Arithmetic Operators
2 Assignment Operators
3 Relational Operators
4 Logical Operators
5 Unary Operators
6 Bitwise Operators

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 33 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. Arithmetic Operators
1 class Ar i t h m e t i c O p e r a t o r {
2 public static void main ( String [] args ) {
3 // declare variables
4 int a = 12 , b = 5;
5
6 // addition operator
7 System . out . println ( " a + b = " + ( a + b ) ) ;
8 // subtraction operator
9 System . out . println ( " a - b = " + ( a - b ) ) ;
10
11 // mul tiplic ation operator
12 System . out . println ( " a * b = " + ( a * b ) ) ;
13
14 // division operator
15 System . out . println ( " a / b = " + ( a / b ) ) ;
16
17 // modulo operator
18 System . out . println ( " a % b = " + ( a % b ) ) ;
19 }
20 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 34 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

[Link] Operators
1 class As s i g n m e n t O p e r a t o r {
2 public static void main ( String [] args ) {
3 // create variables
4 int a = 4;
5 int var ;
6 // assign value using =
7 var = a ;
8 System . out . println ( " var using =: " + var ) ;
9 // assign value using +=
10 var += a ;
11 System . out . println ( " var using +=: " + var ) ;
12 // assign value using *=
13 var *= a ;
14 System . out . println ( " var using *=: " + var ) ;
15 // assign value using /=
16 var /= a ;
17 System . out . println ( " var using /=: " + var ) ;
18 }
19 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 35 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Java Relational Operators

1 class Re l a t i o n a l O p e r a t o r {
2 public static void main ( String [] args ) {
3
4 // create variables
5 int a = 7 , b = 11;
6
7 // value of a and b
8 System . out . println ( " a is " + a + " and b is " + b ) ;
9
10 // == operator
11 System . out . println ( a == b ) ; // false
12
13 // != operator
14 System . out . println ( a != b ) ; // true

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 36 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Java Relational Operators

16 // > operator
17 System . out . println ( a > b ) ; // false
18
19 // < operator
20 System . out . println ( a < b ) ; // true
21
22 // >= operator
23 System . out . println ( a >= b ) ; // false
24
25 // <= operator
26 System . out . println ( a <= b ) ; // true
27 }
28 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 37 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

4. Java Logical Operators

1 class Lo gi c al O pe ra t or {
2 public static void main ( String [] args ) {
3
4 // && operator
5 System . out . println ((5 > 3) && (8 > 5) ) ; // true
6 System . out . println ((5 > 3) && (8 < 5) ) ; // false
7
8 // || operator
9 System . out . println ((5 < 3) || (8 > 5) ) ; // true
10 System . out . println ((5 > 3) || (8 < 5) ) ; // true
11 System . out . println ((5 < 3) || (8 < 5) ) ; // false
12
13 // ! operator
14 System . out . println (!(5 == 3) ) ; // true
15 System . out . println (!(5 > 3) ) ; // false
16 }
17 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 38 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

5. Java Unary Operators


1 class UnaryOperator {
2 public static void main ( String [] args ) {
3 // declare variables
4 int a = 12 , b = 12;
5 int result1 , result2 ;
6
7 // original value
8 System . out . println ( " Value of a : " + a ) ;
9
10 // increment operator
11 result1 = ++ a ;
12 System . out . println ( " After increment : " + result1 ) ;
13 System . out . println ( " Value of b : " + b ) ;
14
15 // decrement operator
16 result2 = --b ;
17 System . out . println ( " After decrement : " + result2 ) ;
18
19 }
20 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 39 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

6. Java Bitwise Operators

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 40 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

6. Java Bitwise Operators

1 Bitwise complement Operation of 35


2
3 35 = 00100011 ( In Binary )
4
5 ~ 00100011
6 ________
7 11011100 = 220 ( In decimal )
8

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 41 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java instanceof Operator

1 class I n s t a n c e O p e r a t o r {
2 public static void main ( String [] args ) {
3
4 String str = " Program " ;
5 boolean result ;
6
7 // checks if str is an instance of
8 // the String class
9 result = str instanceof String ;
10 System . out . println ( " Is str an object of String ? " +
result ) ;
11 }
12 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 42 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Ternary Operator

1 class Te rn a ry L ea pY e ar {
2 public static void main ( String [] args ) {
3
4 int februaryDays = 29;
5 String result ;
6
7 // ternary operator
8 result = ( februaryDays == 28) ? " Not a leap year " : "
Leap year " ;
9 System . out . println ( result ) ;
10 }
11 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 43 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Output

1 System . out . println () ; or


2 System . out . print () ; or
3 System . out . printf () ;
4

1 class Output {
2 public static void main ( String [] args ) {
3
4 System . out . println ( " 1. println " ) ;
5 System . out . println ( " 2. println " ) ;
6
7 System . out . print ( " 1. print " ) ;
8 System . out . print ( " 2. print " ) ;
9 }
10 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 44 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Printing Variables and Literals

1 class Variables {
2 public static void main ( String [] args ) {
3
4 double number = -10.6;
5
6 System . out . println (5) ;
7 System . out . println ( number ) ;
8 }
9 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 45 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Print Concatenated Strings

1 class Pr intVar iables {


2 public static void main ( String [] args ) {
3
4 double number = -10.6;
5
6 System . out . println ( " I am concating the " + " string . "
);
7 System . out . println ( " Number = " + number ) ;
8 }
9 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 46 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Input

To get input from user using the object of Scanner class.


In order to use the object of Scanner, we need to import [Link]
package.
1 import java . util . Scanner ;
2
3 // create an object of Scanner
4 Scanner input = new Scanner ( System . in ) ;
5
6 // take input from the user
7 int number = input . nextInt ()
8

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 47 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Get Integer Input From the User


1 import java . util . Scanner ;
2
3 class Input {
4 public static void main ( String [] args ) {
5
6 Scanner input = new Scanner ( System . in ) ;
7
8 System . out . print ( " Enter an integer : " ) ;
9 int number = input . nextInt () ;
10 System . out . println ( " You entered " + number ) ;
11
12 // closing the scanner object
13 input . close () ;
14 }
15 }

./code/[Link]

we can use nextLong(), nextFloat(), nextDouble(), and next() methods


to get long, float, double, and string input respectively from the user.
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 48 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Get float, double and String Input


1 import java . util . Scanner ;
2 class Input2 {
3 public static void main ( String [] args ) {
4 Scanner input = new Scanner ( System . in ) ;
5 // Getting float input
6 System . out . print ( " Enter float : " ) ;
7 float myFloat = input . nextFloat () ;
8 System . out . println ( " Float entered = " + myFloat ) ;
9
10 // Getting double input
11 System . out . print ( " Enter double : " ) ;
12 double myDouble = input . nextDouble () ;
13 System . out . println ( " Double entered = " + myDouble ) ;
14
15 // Getting String input
16 System . out . print ( " Enter text : " ) ;
17 String myString = input . next () ;
18 System . out . println ( " Text entered = " + myString ) ;
19 }
20 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 49 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. Java if (if-then) Statement


The syntax of an if-then statement is:
1 if ( condition ) {
2 // statements
3 }
4

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 50 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example 1: Java if Statement

1 class IfStatement {
2 public static void main ( String [] args ) {
3
4 int number = 10;
5
6 // checks if number is less than 0
7 if ( number < 0) {
8 System . out . println ( " The number is negative . " ) ;
9 }
10
11 System . out . println ( " Statement outside if block " ) ;
12 }
13 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 51 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example 2: Java if with String

1 class If SringE xample {


2 public static void main ( String [] args ) {
3 // create a string variable
4 String language = " Java " ;
5
6 // if statement
7 if ( language == " Java " ) {
8 System . out . println ( " Best Programming Language " ) ;
9 }
10 }
11 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 52 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. Java if...else (if-then-else) Statement

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 53 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example 3: Java if...else Statement

1 class IfElse {
2 public static void main ( String [] args ) {
3 int number = 10;
4
5 // checks if number is greater than 0
6 if ( number > 0) {
7 System . out . println ( " The number is positive . " ) ;
8 }
9
10 // execute this block
11 // if number is not greater than 0
12 else {
13 System . out . println ( " The number is not positive . " ) ;
14 }
15
16 System . out . println ( " Statement outside if ... else block " ) ;
17 }
18 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 54 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Java if...else...if Statement

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 55 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example 4: Java if...else...if Statement


1 class IfElseLadder {
2 public static void main ( String [] args ) {
3 int number = 0;
4
5 // checks if number is greater than 0
6 if ( number > 0) {
7 System . out . println ( " The number is positive . " ) ;
8 }
9
10 // checks if number is less than 0
11 else if ( number < 0) {
12 System . out . println ( " The number is negative . " ) ;
13 }
14
15 // if both condition is false
16 else {
17 System . out . println ( " The number is 0. " ) ;
18 }
19 }
20 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 56 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

4. Java Nested if..else Statement

1 class NestedIfElse {
2 public static void main ( String [] args ) {
3
4 // declaring double type variables
5 Double n1 = -1.0 , n2 = 4.5 , n3 = -5.3 , largest ;
6
7 // checks if n1 is greater than or equal to n2
8 if ( n1 >= n2 ) {
9
10 // if ... else statement inside the if block
11 // checks if n1 is greater than or equal to n3
12 if ( n1 >= n3 ) {
13 largest = n1 ;
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 57 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

4. Java Nested if..else Statement


16 else {
17 largest = n3 ;
18 }
19 } else {
20
21 // if .. else statement inside else block
22 // checks if n2 is greater than or equal to n3
23 if ( n2 >= n3 ) {
24 largest = n2 ;
25 }
26
27 else {
28 largest = n3 ;
29 }
30 }
31
32 System . out . println ( " Largest Number : " + largest ) ;
33 }
34 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 58 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Practice Questions

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 59 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Switch Statement


The switch statement allows us to execute a block of code among
many alternatives
1 switch ( expression ) {
2
3 case value1 :
4 // code
5 break ;
6
7 case value2 :
8 // code
9 break ;
10
11 ...
12 ...
13
14 default :
15 // default statements
16 }

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 60 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How does the switch-case statement work?

The expression is evaluated once and compared with the values


of each case
If expression matches with value1, the code of case value1 are
executed. Similarly, the code of case value2 is executed if
expression matches with value2.
If there is no match, the code of the default case is executed.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 61 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java switch Statement


1 class SwitchDemo {
2 public static void main ( String [] args ) {
3 int number = 44; String size ;
4 switch ( number ) {
5 case 29:
6 size = " Small " ;
7 break ;
8 case 42:
9 size = " Medium " ;
10 break ;
11 case 44:
12 size = " Large " ;
13 break ;
14 case 48:
15 size = " Extra Large " ;
16 break ;
17 default :
18 size = " Unknown " ;
19 break ; }
20 System . out . println ( " Size : " + size ) ;
21 } }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 62 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

break statement in Java switch...case


The break statement is used to terminate the switch-case statement.
If break is not used, all the cases after the matching case are also
executed.
1 class SwitchWoBreak {
2 public static void main ( String [] args ) {
3 int expression = 2;
4 // switch statement to check size
5 switch ( expression ) {
6 case 1:
7 System . out . println ( " Case 1 " ) ;
8 // matching case
9 case 2:
10 System . out . println ( " Case 2 " ) ;
11 case 3:
12 System . out . println ( " Case 3 " ) ;
13
14 default :
15 System . out . println ( " Default case " ) ;
16 }
17 }
18 }
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 63 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java for Loop


Java for loop is used to run a block of code for a certain number of
times. The syntax of for loop is:
1 for ( i n i t i a l E x p r e s s i o n ; testE xpress ion ;
u p da t e E x p r e s s i o n ) {
2 // body of the loop
3 }
4

1 The initialExpression initializes and/or declares variables and


executes only once.
2 The condition is evaluated. If the condition is true, the body of
the for loop is executed.
3 The updateExpression updates the value of initialExpression.
4 The condition is evaluated again. The process continues until
the condition is false.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 64 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Flowchart of Java for loop

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 65 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Display numbers from 1 to 5

1 // Program to print numbers from 1 to 5


2
3 class ForNumbers {
4 public static void main ( String [] args ) {
5
6 int n = 5;
7 // for loop
8 for ( int i = 1; i <= n ; ++ i ) {
9 System . out . println ( i ) ;
10 }
11 }
12 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 66 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Practice: Display Sum of n Natural Numbers


(assume n =1000)

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 67 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Practice: Display Sum of n Natural Numbers


(assume n =1000)
1 // Program to find the sum of natural numbers from 1 to
1000.
2
3 class Fo r N a t u r a l N u m S u m {
4 public static void main ( String [] args ) {
5
6 int sum = 0;
7 int n = 1000;
8
9 // for loop
10 for ( int i = 1; i <= n ; ++ i ) {
11 // body inside for loop
12 sum += i ; // sum = sum + i
13 }
14
15 System . out . println ( " Sum = " + sum ) ;
16 }
17 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 67 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Guess the output

1 class ForLoop {
2 public static void main ( String [] args ) {
3
4 int sum = 0;
5
6 for ( int i = 1; i <= 10; --i ) {
7 System . out . println ( " Hello " ) ;
8 }
9 }
10 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 68 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java While Loop

Java while loop is used to run a specific code until a certain condition
is met. The syntax of the while loop is:
1 while ( t estExp ressio n ) {
2 // body of loop
3 }

A while loop evaluates the textExpression inside the


parenthesis ().
If the textExpression evaluates to true, the code inside the
while loop is executed.
The textExpression is evaluated again.
This process continues until the textExpression is false.
When the textExpression evaluates to false, the loop stops.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 69 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Flowchart of Java While loop

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 70 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Display Numbers from 1 to 5

1 // Program to display numbers from 1 to 5


2
3 class WhileNumber {
4 public static void main ( String [] args ) {
5
6 // declare variables
7 int i = 1 , n = 5;
8
9 // while loop from 1 to 5
10 while ( i <= n ) {
11 System . out . println ( i ) ;
12 i ++;
13 }
14 }
15 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 71 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Sum of Positive Numbers Only

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 72 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Sum of Positive Numbers Only


1 // Java program to find the sum of positive numbers
2 import java . util . Scanner ;
3 class SumOfNumbers {
4 public static void main ( String [] args ) {
5 int sum = 0;
6 // create an object of Scanner class
7 Scanner input = new Scanner ( System . in ) ;
8 // take integer input from the user
9 System . out . println ( " Enter a number " ) ;
10 int number = input . nextInt () ;
11 // while loop continues
12 // until entered number is positive
13 while ( number >= 0) {
14 // add only positive numbers
15 sum += number ;
16 System . out . println ( " Enter a number " ) ;
17 number = input . nextInt () ;
18 }
19 System . out . println ( " Sum = " + sum ) ;
20 input . close () ;
21 }
22 }
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 72 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java do...while loop


The do...while loop is similar to while loop. However, the body of
do...while loop is executed once before the test expression is checked.
1 do {
2 // body of loop
3 } while ( tex tExpre ssion ) ;
4

The body of the loop is executed at first. Then the


textExpression is evaluated.
If the textExpression evaluates to true, the body of the loop
inside the do statement is executed again.
The textExpression is evaluated once again.
If the textExpression evaluates to true, the body of the loop
inside the do statement is executed again.
This process continues until the textExpression evaluates to
false. Then the loop stops.
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 73 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Flowchart of do...while loop

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 74 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Display Numbers from 1 to 5

1 // Java Program to display numbers from 1 to 5


2
3 import java . util . Scanner ;
4
5 // Program to find the sum of natural numbers from 1 to 100.
6
7 class DoWhileNumber {
8 public static void main ( String [] args ) {
9
10 int i = 1 , n = 5;
11
12 // do ... while loop from 1 to 5
13 do {
14 System . out . println ( i ) ;
15 i ++;
16 } while ( i <= n ) ;
17 }
18 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 75 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Sum of Positive Numbers

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 76 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Sum of Positive Numbers


1 // Java program to find the sum of positive numbers
2 import java . util . Scanner ;
3 class D o W h i l e P o s i t i v e N u m b e r s {
4 public static void main ( String [] args ) {
5 int sum = 0;
6 int number = 0;
7 // create an object of Scanner class
8 Scanner input = new Scanner ( System . in ) ;
9 // do ... while loop continues
10 // until entered number is positive
11 do {
12 // add only positive numbers
13 sum += number ;
14 System . out . println ( " Enter a number " ) ;
15 number = input . nextInt () ;
16 } while ( number >= 0) ;
17
18 System . out . println ( " Sum = " + sum ) ;
19 input . close () ;
20 }
21 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 76 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Guess the output

1 class Output2
2 {
3 public static void main ( String s [])
4 {
5 int i = 1 , j = 10;
6
7 do
8 {
9 if ( i > j )
10 break ;
11 j - -;
12
13 } while (++ i < 5) ;
14
15 System . out . println ( " i = " + i + " and j = " + j ) ;
16 }
17 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 77 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java break Statement

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 78 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Java break statement


1 import java . util . Scanner ;
2
3 class UserInputSum {
4 public static void main ( String [] args ) {
5 Double number , sum = 0.0;
6 // create an object of Scanner
7 Scanner input = new Scanner ( System . in ) ;
8
9 while ( true ) {
10 System . out . print ( " Enter a number : " ) ;
11 // takes double input from user
12 number = input . nextDouble () ;
13 // if number is negative the loop terminates
14 if ( number < 0.0) {
15 break ;
16 }
17 sum += number ;
18 }
19 System . out . println ( " Sum = " + sum ) ;
20 }
21 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 79 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java break and Nested Loop

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 80 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Labeled break Statement

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 81 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Labeled break Statement


1 class LabeledBreak {
2 public static void main ( String [] args ) {
3
4 // the for loop is labeled as first
5 first :
6 for ( int i = 1; i < 5; i ++) {
7
8 // the for loop is labeled as second
9 second :
10 for ( int j = 1; j < 3; j ++ ) {
11
12 System . out . println ( " i = " + i + " ; j = " + j )
;
13 // the break statement terminates the loop
labeled as second
14 if ( i == 2)
15 break second ;
16 }
17 }
18 }
19 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 82 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Working of Java continue statement

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 83 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Compute the sum of 5 positive


numbers
1 import java . util . Scanner ;
2 class Continue1 {
3 public static void main ( String [] args ) {
4 Double number , sum = 0.0;
5 // create an object of Scanner
6 Scanner input = new Scanner ( System . in ) ;
7 for ( int i = 1; i < 6; ++ i ) {
8 System . out . print ( " Enter number " + i + " : " ) ;
9 // takes input from the user
10 number = input . nextDouble () ;
11 // if number is negative // continue statement is
executed
12 if ( number <= 0.0) {
13 continue ;
14 }
15 sum += number ;
16 }
17 System . out . println ( " Sum = " + sum ) ;
18 input . close () ;
19 }
20 } Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 84 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java continue with Nested Loop

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 85 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: continue with Nested Loop


1 class Continue2 {
2 public static void main ( String [] args ) {
3 int i = 1 , j = 1;
4 // outer loop
5 while ( i <= 3) {
6 System . out . println ( " Outer Loop : " + i ) ;
7 // inner loop
8 while ( j <= 3) {
9
10 if ( j == 2) {
11 j ++;
12 continue ;
13 }
14 System . out . println ( " Inner Loop : " + j ) ;
15 j ++;
16 }
17 i ++;
18 }
19 }
20 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 86 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Labeled continue Statement

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 87 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: labeled continue Statement

1 class Continue3 {
2 public static void main ( String [] args ) {
3
4 // outer loop is labeled as first
5 first :
6 for ( int i = 1; i < 6; ++ i ) {
7
8 // inner loop
9 for ( int j = 1; j < 5; ++ j ) {
10 if ( i == 3 || j == 2)
11
12 // skips the current iteration of outer loop
13 continue first ;
14 System . out . println ( " i = " + i + " ; j = " + j ) ;
15 }
16 }
17 }
18 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 88 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Array

An array is a collection of similar types of data

How to declare an array in Java?


1 dataType [] arrayName ; ( or )
2 dataType [] arrayName ; ( or )
3 dataType arr [];
4

dataType - it can be primitive data types like int, char, double, byte,
etc. or Java objects
arrayName - it is an identifier

example of Array
double[] data;

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 89 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How many elements can array hold?

1 // declare an array
2 double [] data ;
3
4 // allocate memory
5 data = new double [10];
6
7 // or
8
9 double [] data = new double [10];
10

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 90 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How to Initialize Arrays in Java?

1 // declare and initialize and array


2 int [] age = {12 , 4 , 5 , 2 , 5};
3
4 // declare an array
5 int [] age = new int [5];
6
7 // initialize array
8 age [0] = 12;
9 age [1] = 4;
10 age [2] = 5;
11 ..
12

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 91 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Few points on Array

Array indices always start from 0. That is, the first element of an
array is at index 0.
If the size of an array is n, then the last element of the array will
be at index n-1.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 92 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How to Access Elements of an Array in Java?


1 // access array elements
2 array [ index ]

1 class AccessArray {
2 public static void main ( String [] args ) {
3
4 // create an array
5 int [] age = {12 , 4 , 5 , 2 , 5};
6 // access each array elements
7 System . out . println ( " Accessing Elements of Array : " ) ;
8 System . out . println ( " First Element : " + age [0]) ;
9 System . out . println ( " Second Element : " + age [1]) ;
10 System . out . println ( " Third Element : " + age [2]) ;
11 System . out . println ( " Fourth Element : " + age [3]) ;
12 System . out . println ( " Fifth Element : " + age [4]) ;
13 }
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 93 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Looping Through Array Elements using for


loop
1 class Lo op i ng U si ng F or {
2 public static void main ( String [] args ) {
3
4 // create an array
5 int [] age = {12 , 4 , 5};
6
7 // loop through the array
8 // using for loop
9 System . out . println ( " Using for Loop : " ) ;
10 for ( int i = 0; i < age . length ; i ++) {
11 System . out . println ( age [ i ]) ;
12 }
13 }
14 }

./code/[Link]

Here, we are using the length property of the array to get the size of
the array.
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 94 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Looping Through Array Elements using for-


each loop

1 class L o o p i n g U s i n g F o r E a c h {
2 public static void main ( String [] args ) {
3
4 // create an array
5 int [] age = {12 , 4 , 5};
6
7 // loop through the array
8 // using for loop
9 System . out . println ( " Using for - each Loop : " ) ;
10 for ( int a : age ) {
11 System . out . println ( a ) ;
12 }
13 }
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 95 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Compute Sum and Average of Ar-


ray Elements
1 class ArrayExample {
2 public static void main ( String [] args ) {
3 int [] numbers = {2 , -9 , 0 , 5 , 12 , -25 , 22 , 9 , 8 , 12};
4 int sum = 0;
5 double average ;
6 // access all elements using for each loop
7 // add each element in sum
8 for ( int number : numbers ) {
9 sum += number ;
10 }
11 // get the total number of elements
12 int arrayLength = numbers . length ;
13 // calculate the average
14 // convert the average from int to double
15 average = (( double ) sum / ( double ) arrayLength ) ;
16 System . out . println ( " Sum = " + sum ) ;
17 System . out . println ( " Average = " + average ) ;
18 }
19 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 96 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Multidimensional Arrays

A multidimensional array is an array of arrays. Each element of a


multidimensional array is an array itself.

Example
int[][] a = new int[3][4];

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 97 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How to initialize a 2d array in Java?

1 int [][] a = {
2 {1 , 2 , 3} ,
3 {4 , 5 , 6 , 9} ,
4 {7} ,
5 };
6

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 98 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: 2-Dimensional Array


1 class M u l t i d i m e n s i o n a l A r r a y {
2 public static void main ( String [] args ) {
3
4 // create a 2 d array
5 int [][] a = {
6 {1 , 2 , 3} ,
7 {4 , 5 , 6 , 9} ,
8 {7} ,
9 };
10
11 // calculate the length of each row
12 System . out . println ( " Length of row 1: " + a [0]. length
);
13 System . out . println ( " Length of row 2: " + a [1]. length
);
14 System . out . println ( " Length of row 3: " + a [2]. length
);
15 }
16 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 99 / 187


What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Print all elements of 2d array Us-


ing Loop
1 class M u l t i d i m e n s i o n a l A r r a y P r i n t {
2 public static void main ( String [] args ) {
3 int [][] a = {
4 {1 , -2 , 3} ,
5 { -4 , -5 , 6 , 9} ,
6 {7} ,
7 };
8 for ( int i = 0; i < a . length ; ++ i ) {
9 for ( int j = 0; j < a [ i ]. length ; ++ j ) {
10 System . out . println ( a [ i ][ j ]) ;
11 }
12 }
13 // first for ... each loop access the individual array
inside the 2 d array
14 for ( int [] innerArray : a ) {
15 // second for ... each loop access each
element inside the row
16 for ( int data : innerArray ) {
17 System . out . println ( data ) ;
18 }
19
Dr. Nikhil Mhala VIT-AP
} CSE1012 April 27, 2022 100 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How to initialize a 3d array in Java?


1 class ThreeArray {
2 public static void main ( String [] args ) {
3 // create a 3 d array
4 int [][][] test = {
5 {
6 {1 , -2 , 3} ,
7 {2 , 3 , 4}
8 },
9 {
10 { -4 , -5 , 6 , 9} ,
11 {1} ,
12 {2 , 3}
13 }
14 };
15 // for .. each loop to iterate through elements of 3 d
array
16 for ( int [][] array2D : test ) {
17 for ( int [] array1D : array2D ) {
18 for ( int item : array1D ) {
19 System . out . println ( item ) ;
20 }
21 } } }
22 } Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 101 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. Copying Arrays Using Assignment Opera-


tor

1 class CopyArray1 {
2 public static void main ( String [] args ) {
3
4 int [] numbers = {1 , 2 , 3 , 4 , 5 , 6};
5 int [] p os it i ve Nu m be rs = numbers ; // copying
arrays
6
7 // change value of first array
8 numbers [0] = -1;
9
10 // printing the second array
11 for ( int number : p o si t iv eN u mb er s ) {
12 System . out . print ( number + " , " ) ;
13 }
14 }
15 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 102 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. Using Looping Construct to Copy Arrays

1 import java . util . Arrays ;


2
3 class CopyArray2 {
4 public static void main ( String [] args ) {
5
6 int [] source = {1 , 2 , 3 , 4 , 5 , 6};
7 int [] destination = new int [6];
8
9 // iterate and copy elements from source to
destination
10 for ( int i = 0; i < source . length ; ++ i ) {
11 destination [ i ] = source [ i ];
12 }
13 source [0]=10;
14 // converting array to string
15 System . out . println ( Arrays . toString ( destination ) ) ;
16 }
17 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 103 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Copying Arrays Using arraycopy() method

Syntax of arraycopy() method


1 arraycopy ( Object src , int srcPos , Object dest , int
destPos , int length )

Here
src - source array you want to copy
srcPos - starting position (index) in the source array
dest - destination array where elements will be copied from the
source
destPos - starting position (index) in the destination array
length - number of elements to copy

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 104 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Copying Arrays Using arraycopy() method


1 // To use Arrays . toString () method
2 import java . util . Arrays ;
3
4 class CopyArray3 {
5 public static void main ( String [] args ) {
6 int [] n1 = {2 , 3 , 12 , 4 , 12 , -2};
7
8 int [] n3 = new int [5];
9
10 // Creating n2 array of having length of n1 array
11 int [] n2 = new int [ n1 . length ];
12
13 // copying entire n1 array to n2
14 System . arraycopy ( n1 , 0 , n2 , 0 , n1 . length ) ;
15 System . out . println ( " n2 = " + Arrays . toString ( n2 ) ) ;
16
17 // copying elements from index 2 on n1 array
18 // copying element to index 1 of n3 array
19 // 2 elements will be copied
20 System . arraycopy ( n1 , 2 , n3 , 1 , 2) ;
21 System . out . println ( " n3 = " + Arrays . toString ( n3 ) ) ;
22 }
23 } Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 105 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

4. Copying Arrays Using copyOfRange() method


1 // To use toString () and copyOfRange () method
2 import java . util . Arrays ;
3
4 class ArraysCopy4 {
5 public static void main ( String [] args ) {
6
7 int [] source = {2 , 3 , 12 , 4 , 12 , -2};
8
9 // copying entire source array to destination
10 int [] destination1 = Arrays . copyOfRange ( source , 0 ,
source . length ) ;
11 System . out . println ( " destination1 = " + Arrays .
toString ( destination1 ) ) ;
12
13 // copying from index 2 to 5 (5 is not included )
14 int [] destination2 = Arrays . copyOfRange ( source , 2 ,
5) ;
15 System . out . println ( " destination2 = " + Arrays .
toString ( destination2 ) ) ;
16 }
17 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 106 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

5. Copying 2d Arrays Using Loop


1 import java . util . Arrays ;
2 class Co p y 2 D A r r a y F o r L o o p {
3 public static void main ( String [] args ) {
4 int [][] source = {
5 {1 , 2 , 3 , 4} ,
6 {5 , 6} ,
7 {0 , 2 , 42 , -4 , 5}
8 };
9 int [][] destination = new int [ source . length ][];
10 for ( int i = 0; i < destination . length ; ++ i ) {
11 // allocating space for each row of destination
array
12 destination [ i ] = new int [ source [ i ]. length ];
13 for ( int j = 0; j < destination [ i ]. length ; ++ j )
{
14 destination [ i ][ j ] = source [ i ][ j ];
15 }
16 }
17 // displaying destination array
18 System . out . println ( Arrays . deepToString ( destination ) )
;
19 }
20 } Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 107 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Notes about 2d array

Here, the deepToString() method is used to provide a better


representation of the 2-dimensional array
To make the above code more simpler, we can replace the inner
loop with [Link]() as in the case of a
single-dimensional array.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 108 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Copying 2d Array using arraycopy()


1 import java . util . Arrays ;
2 class C o p y 2 d A r r a y A r r a y C o p y {
3 public static void main ( String [] args ) {
4 int [][] source = {
5 {1 , 2 , 3 , 4} ,
6 {5 , 6} ,
7 {0 , 2 , 42 , -4 , 5}
8 };
9 int [][] destination = new int [ source . length ][];
10 for ( int i = 0; i < source . length ; ++ i ) {
11 // allocating space for each row of destination
array
12 destination [ i ] = new int [ source [ i ]. length ];
13 System . arraycopy ( source [ i ] , 0 , destination [ i ] ,
0 , destination [ i ]. length ) ;
14 }
15 // displaying destination array
16 System . out . println ( Arrays . deepToString ( destination ) )
;
17 }
18 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 109 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Object-Oriented Programming System


Object-Oriented Programming is a paradigm that provides many
concepts, such as inheritance, data binding, polymorphism, etc.

OOPs Provides the concepts like:

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 110 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Object

Any entity that has state and behavior is known as an object


Objects are always called instances of a class
Objects are created from class in java or any other language.
Example: A dog is an object because it has states like color,
name, breed, etc. as well as behaviors like wagging the tail,
barking, eating, etc.
An object contains an address and takes up some space in
memory.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 111 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Class

Collection of objects is called class. It is a logical entity.


A class can also be defined as a blueprint from which you can
create an individual object.
Class doesn’t consume any space.
Class is also called a template of an object.
Classes have members which can be fields, methods and
constructors.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 112 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Inheritance

When one object acquires all the properties and behaviors of a


parent object, it is known as inheritance.
It provides code reusability.
It is used to achieve runtime polymorphism.
The subclass is a class which inherits properties of the
superclass. This is also called a derived class. A superclass is a
base class or parental class from which subclass inherits
properties.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 113 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Polymorphism
If one task is performed in different ways, it is known as
polymorphism
Example can be to speak something; for example, a cat speaks
meow, dog barks woof, etc.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 114 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Abstraction

Hiding internal details and showing functionality is known as


abstraction
For example phone call, we don’t know the internal processing
In Java, we use abstract class and interface to achieve
abstraction.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 115 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Encapsulation

Binding (or wrapping) code and data together into a single unit
are known as encapsulation.
For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation.
In the encapsulation, the data is hidden from other classes and
can be accessed only through the current class’s methods.
Hence, it is also known as data hiding.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 116 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Class

A class is a blueprint for the object. Before we create an object,


we first need to define the class
We can think of the class as a sketch (prototype) of a house. It
contains all the details about the floors, doors, windows, etc.
Based on these descriptions we build the house. House is the
object.
Since many houses can be made from the same description, we
can create many objects from a class.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 117 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Create a class in Java

1 \\ Syntax of class in Java


2 class ClassName {
3 // fields
4 // methods
5 }
6

Here, fields (variables) and methods represent the state and


behavior of the object respectively.
fields are used to store data
methods are used to perform some operations

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 118 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example:class for bicycle object

1 class Bicycle {
2
3 // state or field
4 private int gear = 5;
5
6 // behavior or method
7 public void braking () {
8 System . out . println ( " Working of Braking " ) ;
9 }
10 }
11

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 119 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: class for bicycle object

In the above example, we have created a class named Bicycle. It


contains a field named gear and a method named braking().
Here, Bicycle is a prototype. Now, we can create any number of
bicycles using the prototype. And, all the bicycles will share the
fields and methods of the prototype.
We have used keywords private and public. These are known as
access modifiers.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 120 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Objects

An object is called an instance of a class. For example, suppose Bi-


cycle is a class then MountainBicycle, SportsBicycle, TouringBicycle,
etc can be considered as objects of the class.

Creating an Object in Java


1 className object = new className () ;
2
3 // for Bicycle class
4 Bicycle sportsBicycle = new Bicycle () ;
5
6 Bicycle to uringB icycle = new Bicycle () ;
7

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 121 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Creating an Object in Java

We have used the new keyword along with the constructor of the
class to create an object
Constructors are similar to methods and have the same name as
the class
For example, Bicycle() is the constructor of the Bicycle class.
Note: Fields and methods of a class are also called members of
the class.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 122 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Access Members of a Class


We can use the name of objects along with the . operator to
access members of a class.
1 class Bicycle {
2
3 // field of class
4 int gear = 5;
5
6 // method of class
7 void braking () {
8 ...
9 }
10 }
11
12 // create object
13 Bicycle sportsBicycle = new Bicycle () ;
14
15 // access field and method
16 sportsBicycle . gear ;
17 sportsBicycle . braking () ;
18

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 123 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java Class and Objects

1 class Lamp {
2
3 // stores the value for light
4 // true if light is on
5 // false if light is off
6 boolean isOn ;
7
8 // method to turn on the light
9 void turnOn () {
10 isOn = true ;
11 System . out . println ( " Light on ? " + isOn ) ;
12
13 }
14
15 // method to turnoff the light

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 124 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java Class and Objects

1 class Lamp {
2
3 // stores the value for light
4 // true if light is on
5 // false if light is off
6 boolean isOn ;
7
8 // method to turn on the light
9 void turnOn () {
10 isOn = true ;
11 System . out . println ( " Light on ? " + isOn ) ;
12
13 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 125 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java Class and Objects

1 // method to turnoff the light


2 void turnOff () {
3 isOn = false ;
4 System . out . println ( " Light on ? " + isOn ) ;
5 }
6 }
7
8 class Main {
9 public static void main ( String [] args ) {
10
11 // create objects led and halogen
12 Lamp led = new Lamp () ;
13 Lamp halogen = new Lamp () ;

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 126 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java Class and Objects

1 // turn on the light by


2 // calling method turnOn ()
3 led . turnOn () ;
4
5 // turn off the light by
6 // calling method turnOff ()
7 halogen . turnOff () ;
8 }
9 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 127 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Create objects inside the same


class
1 class Lamp2 {
2 // stores the value for light
3 // true if light is on
4 // false if light is off
5 boolean isOn ;
6
7 // method to turn on the light
8 void turnOn () {
9 isOn = true ;
10 System . out . println ( " Light on ? " + isOn ) ;
11
12 }
13 public static void main ( String [] args ) {
14 // create an object of Lamp
15 Lamp led = new Lamp2 () ;
16 // access method using object
17 led . turnOn () ;
18 }
19 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 128 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Methods

A method is a block of code that performs a specific task.

Suppose you need to create a program to create a circle and color it.
You can create two methods to solve this problem:
a method to draw the circle
a method to color the circle

In Java, there are two types of methods:


User-defined Methods: We can create our own method based on
our requirements.
Standard Library Methods: These are built-in methods in Java
that are available to use.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 129 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Declaring a Java Method

1 \\ syntax to declare a method :


2 returnType methodName () {
3 // method body
4 }
5

returnType - It specifies what type of value a method returns For


example if a method has an int return type then it returns an
integer value.
If the method does not return a value, its return type is void.
methodName - It is an identifier that is used to refer to the
particular method in a program.
method body - It includes the programming statements that are
used to perform some tasks. The method body is enclosed
inside the curly braces .

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 130 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Detailed syntax of declaring a method in Java

1 modifier static returnType nameOfMethod ( parameter1 ,


parameter2 , ...) {
2 // method body
3 }
4

modifier - It defines access types whether the method is public,


private, and so on.
static - If we use the static keyword, it can be accessed without
creating objects.
For example, the sqrt() method of standard Math class is static.
Hence, we can directly call [Link]() without creating an
instance of Math class.
parameter1/parameter2 - These are values passed to a method.
We can pass any number of arguments to a method.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 131 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Calling a Method in Java


In the above example, we have declared a method named addNum-
bers(). Now, to use the method, we need to call it.
1 // calls the method
2 addNumbers ()
3

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 132 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Java Methods


1 class AddNumbers {
2 // create a method
3 public int addNumbers ( int a , int b ) {
4 int sum = a + b ;
5 // return value
6 return sum ;
7 }
8 public static void main ( String [] args ) {
9 int num1 = 25;
10 int num2 = 15;
11 // create an object of Main
12 Main obj = new Main () ;
13 // calling method
14 int result = obj . addNumbers ( num1 , num2 ) ;
15 System . out . println ( " Sum is : " + result ) ;
16 }
17 }

./code/[Link]

Note: The method is not static. Hence, we are calling the method
using the object of the class.
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 133 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Method Return Type


A Java method may or may not return a value to the function call. We
use the return statement to return any value. For example,
1 class R e t u r n M e t h o d D e m o {
2 // create a method
3 public static int square ( int num ) {
4 // return statement
5 return num * num ;
6 }
7 public static void main ( String [] args ) {
8 int result ;
9
10 // call the method
11 // store returned value to result
12 result = square (10) ;
13 System . out . println ( " Squared value of 10 is : " + result ) ;
14 }
15 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 134 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Method Return Type

If the method does not return any value, we use the void keyword as
the return type of the method. For example,
1 public void square ( int a ) {
2 int square = a * a ;
3 System . out . println ( " Square is : " + a ) ;
4 }
5

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 135 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Method Parameters in Java

A method parameter is a value accepted by the method. As men-


tioned earlier, a method can also have any number of parameters
1 // method with two parameters
2 int addNumbers ( int a , int b ) {
3 // code
4 }
5
6 // method with no parameter
7 int addNumbers () {
8 // code
9 }
10

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 136 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Calling the Method with Parameters

1 // calling the method with two parameters


2 addNumbers (25 , 15) ;
3
4 // calling the method with no parameters
5 addNumbers ()
6

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 137 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Method Parameters


1 class M e t h o d P a r a m e t e r s {
2 // method with no parameter
3 public void display1 () {
4 System . out . println ( " Method without parameter " ) ;
5 }
6 // method with single parameter
7 public void display2 ( int a ) {
8 System . out . println ( " Method with a single parameter : " +
a);
9 }
10 public static void main ( String [] args ) {
11 // create an object of Main
12 M e th o d P a r a m e t e r s obj = new M e t h o d P a r a m e t e r s () ;
13 // calling method with no parameter
14 obj . display1 () ;
15 // calling method with the single parameter
16 obj . display2 (24) ;
17 }
18 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 138 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Standard Library Methods

The standard library methods are built-in methods in Java that are
readily available for use.
These standard libraries come along with the Java Class Library
(JCL) in a Java archive (*.jar) file with JVM and JRE.
sqrt() is a method of Math class. It returns the square root of a num-
ber.
1 public class Main {
2 public static void main ( String [] args ) {
3
4 // using the sqrt () method
5 System . out . print ( " Square root of 4 is : " + Math .
sqrt (4) ) ;
6 }
7 }
8

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 139 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What are the advantages of using methods?


1. The main advantage is code reusability We can write a method
once, and use it multiple times. We do not have to rewrite the entire
code each time. "write once, reuse multiple times"
1 // Example 5: Java Method for Code Reusability
2 public class M e t h o d R e u s a b i l i t y {
3 // method defined
4 private static int getSquare ( int x ) {
5 return x * x ;
6 }
7 public static void main ( String [] args ) {
8 for ( int i = 1; i <= 5; i ++) {
9 // method call
10 int result = getSquare ( i ) ;
11 System . out . println ( " Square of " + i + " is : " + result
);
12 }
13 }
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 140 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What are the advantages of using methods?

2. Methods make code more readable and easier to debug. Here,


the getSquare() method keeps the code to compute the square in a
block. Hence, makes it more readable.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 141 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Method Overloading

In Java, two or more methods may have the same name if they differ
in parameters (different number of parameters, different types of
parameters, or both)
These methods are called overloaded methods and this feature is
called method overloading.
1 \\ example
2 void func () { ... }
3 void func ( int a ) { ... }
4 float func ( double a ) { ... }
5 float func ( int a , float b ) { ... }
6

Here, the func() method is overloaded. These methods have the


same name but accept different arguments.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 142 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Method Overloading

Note: The return types of the above methods are not the same. It
is because method overloading is not associated with return types.
Overloaded methods may have the same or different return types, but
they must differ in parameters.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 143 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Why method overloading?

Suppose, you have to perform the addition of given numbers but


there can be any number of arguments (let’s say either 2 or 3
arguments for simplicity).
In order to accomplish the task, you can create two methods
sum2num(int, int) and sum3num(int, int, int) for two and three
parameters respectively.
However, other programmers, as well as you in the future may
get confused as the behavior of both methods are the same but
they differ by name.
The better way to accomplish this task is by overloading
methods. And, depending upon the argument passed, one of the
overloaded methods is called. This helps to increase the
readability of the program.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 144 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How to perform method overloading in Java?

1. Overloading by changing the number of parameters


1 class M e t h o d O v e r l o a d i n g {
2 private static void display ( int a ) {
3 System . out . println ( " Arguments : " + a);
4 }
5 private static void display ( int a , int b){
6 System . out . println ( " Arguments : " + a + " and " + b ) ;
7 }
8 public static void main ( String [] args ) {
9 display (1) ;
10 display (1 , 4) ;
11 }
12 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 145 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

How to perform method overloading in Java?

2. Method Overloading by changing the data type of parameters


1 class Me t h o d O v e r l o a d i n g 2 {
2 // this method accepts int
3 private static void display ( int a ) {
4 System . out . println ( " Got Integer data . " ) ;
5 }
6 // this method accepts String object
7 private static void display ( String a ) {
8 System . out . println ( " Got String object . " ) ;
9 }
10 public static void main ( String [] args ) {
11 display (1) ;
12 display ( " Hello " ) ;
13 }
14 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 146 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

A real-world example
1 class HelperService {
2 private String formatNumber ( int value ) {
3 return String . format ( " % d " , value ) ;
4 }
5 private String formatNumber ( double value ) {
6 return String . format ( " %.3 f " , value ) ;
7 }
8
9 private String formatNumber ( String value ) {
10 return String . format ( " %.2 f " , Double . parseDouble (
value ) ) ;
11 }
12 public static void main ( String [] args ) {
13 HelperService hs = new HelperService () ;
14 System . out . println ( hs . formatNumber (500) ) ;
15 System . out . println ( hs . formatNumber (89.9934) ) ;
16 System . out . println ( hs . formatNumber ( " 550 " ) ) ;
17 }
18 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 147 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Important Points

Two or more methods can have the same name inside the same
class if they accept different arguments. This feature is known as
method overloading.
Method overloading is achieved by either:
changing the number of arguments.
or changing the data type of arguments.
It is not method overloading if we only change the return type of
methods. There must be differences in the number of
parameters.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 148 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What is a Constructor?

A constructor in Java is similar to a method that is invoked


when an object of the class is created.
Unlike Java methods, a constructor has the same name as that of the
class and does not have any return type.
1 class Test {
2 Test () {
3 // constructor body
4 }
5 }
6

Here, Test() is a constructor. It has the same name as that of the


class and doesn’t have a return type.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 149 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example : Java Constructor

1 class Ja va C on s tr uc t or {
2 private String name ;
3
4 // constructor
5 J av aCo ns tr u ct or () {
6 System . out . println ( " Constructor Called : " ) ;
7 name = " Programiz " ;
8 }
9
10 public static void main ( String [] args ) {
11
12 // constructor is invoked while
13 // creating an object of the Main class
14 J av aC o ns t ru ct o r obj = new J av aC o ns tr u ct or () ;
15 System . out . println ( " The name is " + obj . name ) ;
16 }
17 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 150 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Types of Constructor

In Java, constructors can be divided into 3 types:


1 No-Arg Constructor
2 Parameterized Constructor
3 Default Constructor

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 151 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. Java No-Arg Constructors

Similar to methods, a Java constructor may or may not have any


parameters (arguments).
If a constructor does not accept any parameters, it is known as a
no-argument constructor. For example,

1 private Constructor () {
2 // body of the constructor
3 }
4

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 152 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java Private no-arg constructor

1 class No Ar g Co n st uc t or {
2 int i ;
3 // constructor with no parameter
4 private No A rg Co n st uc t or () {
5 i = 5;
6 System . out . println ( " Constructor is called " ) ;
7 }
8 public static void main ( String [] args ) {
9 // calling the constructor without any parameter
10 N oA rg C on s tu ct o r obj = new N oA rg C on st u ct or () ;
11 System . out . println ( " Value of i : " + obj . i ) ;
12 }
13 }

./code/[Link]

In the above example, we have created a constructor NoArgConstruc-


tor(). Here, the constructor does not accept any parameters. Hence,
it is known as a no-arg constructor.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 153 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. Java No-Arg Constructors

Notice that we have declared the constructor as private.


Once a constructor is declared private, it cannot be accessed
from outside the class. So, creating objects from outside the
class is prohibited using the private constructor.
Here, we are creating the object inside the same class. Hence,
the program is able to access the constructor.
However, if we want to create objects outside the class, then we
need to declare the constructor as public.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 154 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Example: Java public no-arg constructors

1 class Company {
2 String name ;
3
4 // public constructor
5 public Company () {
6 name = " VIT - AP " ;
7 }
8 }
9
10 class Main {
11 public static void main ( String [] args ) {
12
13 // object is created in another class
14 Company obj = new Company () ;
15 System . out . println ( " Company name = " + obj . name ) ;
16 }
17 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 155 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. Java Parameterized Constructor


A Java constructor can also accept one or more parameters.
Such constructors are known as parameterized constructors
(constructor with parameters).
1 class P a r a m e t e r i z e d C o n s t r u c t o r {
2 String languages ;
3 // constructor accepting single value
4 P a r a m e t e r i z e d C o n s t r u c t o r ( String lang ) {
5 languages = lang ;
6 System . out . println ( languages + " Programming Language " ) ;
7 }
8 public static void main ( String [] args ) {
9 // call constructor by passing a single value
10 P a r a m e t e r i z e d C o n s t r u c t o r obj1 = new
P a r a m e t e r i z e d C o n s t r u c t o r ( " Java " ) ;
11 P a r a m e t e r i z e d C o n s t r u c t o r obj2 = new
P a r a m e t e r i z e d C o n s t r u c t o r ( " Python " ) ;
12 P a r a m e t e r i z e d C o n s t r u c t o r obj3 = new
ParameterizedConstructor ("C");
13 }
14 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 156 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. Java Parameterized Constructor

In the above example, we have created a constructor named Parame-


terizedConstructor(). Here, the constructor takes a single parameter.
Notice the expression,
1 P a r a m e t e r i z e d C o n s t r u c t o r obj1 = new
P a r a m e t e r i z e d C o n s t r u c t o r ( " Java " ) ;
2

Here, we are passing the single value to the constructor. Based on


the argument passed, the language variable is initialized inside the
constructor.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 157 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Java Default Constructor

If we do not create any constructor, the Java compiler automat-


ically create a no-arg constructor during the execution of the
program. This constructor is called default constructor.
1 class D e f a u l t C o n s t u c t o r {
2 int a ;
3 boolean b ;
4 public static void main ( String [] args ) {
5 // A default constructor is called
6 D e f a u l t C o n s t u c t o r obj = new D e f a u l t C o n s t u c t o r () ;
7 System . out . println ( " Default Value : " ) ;
8 System . out . println ( " a = " + obj . a ) ;
9 System . out . println ( " b = " + obj . b ) ;
10 }
11 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 158 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Java Default Constructor

1 // The above program is equivalent to :


2 class De f a u l t C o n s t u c t o r 2 {
3 int a ;
4 boolean b ;
5
6 D e f a u l t C o n s t u c t o r 2 () {
7 a = 0;
8 b = false ;
9 }
10 public static void main ( String [] args ) {
11 // call the constructor
12 D e f a u l t C o n s t u c t o r 2 obj = new D e f a u l t C o n s t u c t o r 2 () ;
13 System . out . println ( " Default Value : " ) ;
14 System . out . println ( " a = " + obj . a ) ;
15 System . out . println ( " b = " + obj . b ) ;
16 }
17 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 159 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Important Notes on Java Constructors

Constructors are invoked implicitly when you instantiate objects.


The two rules for creating a constructor are:
The name of the constructor should be the same as the class.
A Java constructor must not have a return type.
If a class doesn’t have a constructor, the Java compiler
automatically creates a default constructor during run-time.
The default constructor initializes instance variables with default
values.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 160 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Important Notes on Java Constructors

Constructor types:
No-Arg Constructor - a constructor that does not accept any
arguments
Parameterized constructor - a constructor that accepts
arguments
Default Constructor - a constructor that is automatically created
by the Java compiler if it is not explicitly defined.
A constructor cannot be abstract or static or final
A constructor can be overloaded but can not be overridden.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 161 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Constructor Overloading in Java


Similar to Java method overloading, we can also create two
or more constructors with different parameters. This is called
constructors overloading.
1 class C o n s t u c t o r O v e r l o a d i n g {
2 String language ;
3 // constructor with no parameter
4 C o n s t u c t o r O v e r l o a d i n g () { this . language = " Java " ;}
5 // constructor with a single parameter
6 C o n s t u c t o r O v e r l o a d i n g ( String language ) {
7 this . language = language ; }
8 public void getName () {
9 System . out . println ( " Programming Langauage : " + this .
language ) ;}
10 public static void main ( String [] args ) {
11 // call constructor with no parameter
12 C o n s t u c t o r O v e r l o a d i n g obj1 = new C o n s t u c t o r O v e r l o a d i n g ()
;
13 // call constructor with a single parameter
14 C o n s t u c t o r O v e r l o a d i n g obj2 = new C o n s t u c t o r O v e r l o a d i n g ( "
Python " ) ;
15 obj1 . getName () ; obj2 . getName () ;}}
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 162 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What is String in Java?

In Java, a string is a sequence of characters.


For example, "hello" is a string containing a sequence of characters
’h’, ’e’, ’l’, ’l’, and ’o’.
We use double quotes to represent a string in Java.
Java String objects are immutable,

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 163 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Create a String in Java


There are two ways to create a String object:
1 By using string literal (literal is nothing but double quotes “”)
2 By using new keyword

By using string literal


1 String s = " Welcome " ;
2

By using new keyword


1 String s = new String ( " Welcome " ) ;
2

It creates two objects (in String pool and in heap) and one reference
variable where
Dr. Nikhil Mhala VIT-AP the variable "s" will refer to the object in the heap.
CSE1012 April 27, 2022 164 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String Pool:

Java String pool refers to a collection of Strings which are stored


in heap memory
If we create string using first method then the compiler first
checks the string pool to see if the string already exists
If the string already exists, the new string is not created. Instead,
the new reference, s points to the already existed string
(Welcome).
If the string doesn’t exist, the new string (Welcome is created.)

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 165 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String pool: Example

1 class StringPool {
2 public static void main ( String [] str )
3 {
4 String s1 = " apple " ;
5 String s2 = " mango " ;
6 String s3 = " apple " ;
7 System . out . println
8 ( s1 == s2 ) ;
9 System . out . println
10 ( s1 == s3 ) ;
11 }
12 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 166 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String pool

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 167 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. While creating strings using the new key-


word

1 String s = new String ( " Welcome " ) ;


2

Here, the value of the string is not directly provided. Hence, a new
"Welcome" string is created even though "Welcome" is already present
inside the memory pool.

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 168 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String Methods

Java String length():


1 // The Java String length () method tells the length of the
string .
2 class StringLength {
3 public static void main ( String args []{
4 String s1 = " hello " ;
5 String s2 = " whatsup " ;
6 System . out . println ( " string length is : " + s1 . length () ) ;
7 System . out . println ( " string length is : " + s2 . length () ) ;
8 }}

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 169 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String Methods


Java String compareTo():
1 /*
2 The Java String compareTo () method compares the given string
with current string
3 if s1 > s2 , it returns a positive number
4 if s1 < s2 , it returns a negative number
5 if s1 == s2 , it returns 0
6 */
7 class Co m p a r e T o E x a m p l e {
8 public static void main ( String args []) {
9 String s1 = " hello " ;
10 String s2 = " hello " ;
11 String s3 = " hemlo " ;
12 String s4 = " flag " ;
13 System . out . println ( s1 . compareTo ( s2 ) ) ; // 0 because both are
equal
14 System . out . println ( s1 . compareTo ( s3 ) ) ; // -1 because " l " is
only one time lower than " m "
15 System . out . println ( s1 . compareTo ( s4 ) ) ; // 2 because " h " is 2
times greater than " f "
16 }}
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 170 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String concat() :

1 /*
2 The Java String concat () method combines a specific string
at the end of another string and ultimately returns a
combined string . It is like appending another string .
3 */
4 class ConcatExample {
5 public static void main ( String args []) {
6 String s1 = " hello " ;
7 s1 = s1 . concat ( " how are you " ) ;
8 System . out . println ( s1 ) ;
9 }}

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 171 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String IsEmpty():

1 /* This method checks whether the String contains anything or


not . If the java String is Empty , it returns true else
false .
2 */
3 class Is EmptyE xample {
4 public static void main ( String args []) {
5 String s1 = " " ;
6 String s2 = " hello " ;
7 System . out . println ( s1 . isEmpty () ) ; // true
8 System . out . println ( s2 . isEmpty () ) ; // false
9 }}

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 172 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String Trim():

1 /*
2 The Java string trim () method removes the leading and
trailing spaces . It checks the Unicode value of space
character ( ’\ u0020 ’) before and after the string . If it
exists , then removes the spaces and return the omitted
string .
3 */
4 public class S t r i n g T r i m E x a m p l e {
5 public static void main ( String args []) {
6 String s1 = " hello ";
7 System . out . println ( s1 + " how are you " ) ; // without trim
()
8 System . out . println ( s1 . trim () + " how are you " ) ; // with trim ()
9 }}

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 173 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String toLowerCase() :

1 /* The java string toLowerCase () method converts all the


characters of the String to lower case
2 */
3 public class S t r i n g L o w e r E x a m p l e {
4 public static void main ( String args []) {
5 String s1 = " HELLO HOW Are You ? " ;
6 String s1lower = s1 . toLowerCase () ;
7 System . out . println ( s1lower ) ;}
8 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 174 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String toUpper() :

1 /* The Java String toUpperCase () method converts all the


characters of the String to upper case .
2 */
3 public class S t r i n g U p p e r E x a m p l e {
4 public static void main ( String args []) {
5 String s1 = " hello how are you " ;
6 String s1upper = s1 . toUpperCase () ;
7 System . out . println ( s1upper ) ;
8 }}

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 175 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java String ValueOf()

1 /* This method converts different types of values into a


string . Using this method , you can convert int to string
, long to string , Boolean to string , character to string
, float to a string , double to a string , object to
string and char array to string . */
2
3 public class S t r i n g V a l u e O f E x a m p l e {
4 public static void main ( String args []) {
5 int value =20;
6 String s1 = String . valueOf ( value ) ;
7 System . out . println ( s1 +17) ; // concatenating string with
10
8 }}

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 176 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Methods of java String


Here are some of those methods:

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 177 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Methods of java String

Here are some of those methods:

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 178 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Methods of java String

Here are some of those methods:

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 179 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

What are Access Modifiers?

In Java, access modifiers are used to set the accessibility (visibility)


of classes, interfaces, variables, methods, constructors, data mem-
bers, and the setter methods.
1 class Animal {
2 public void method1 () {...}
3
4 private void method2 () {...}
5 }
6

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 180 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Types of Access Modifier

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 181 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Summary Access Modifier

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 182 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java final keyword

In Java, the final keyword is used to denote constants. It can be


used with variables, methods, and classes
Once any entity (variable, method or class) is declared final, it can be
assigned only once. That is,
the final variable cannot be reinitialized with another value
the final method cannot be overridden
the final class cannot be extended

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 183 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

1. Java final variable

1 class J a v a F i n a l V a r i a b l e {
2 public static void main ( String [] args ) {
3
4 // create a final variable
5 final int AGE = 32;
6
7 // try to change the final variable
8 AGE = 45;
9 System . out . println ( " Age : " + AGE ) ;
10 }
11 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 184 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

2. Java final Method

1 class FinalDemo {
2 // create a final method
3 public final void display () {
4 System . out . println ( " This is a final method . " ) ;
5 }
6 }
7
8 class Main extends FinalDemo {
9 // try to override final method
10 public final void display () {
11 System . out . println ( " The final method is overridden . " ) ;
12 }
13
14 public static void main ( String [] args ) {
15 Main obj = new Main () ;
16 obj . display () ;
17 }
18 }

./code/[Link]

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 185 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

3. Java final Class


1 // In Java , the final class cannot be inherited by another
class .
2 // create a final class
3 final class FinalClass {
4 public void display () {
5 System . out . println ( " This is a final method . " ) ;
6 }
7 }
8
9 // try to extend the final class
10 class Main extends FinalClass {
11 public void display () {
12 System . out . println ( " The final method is overridden . " ) ;
13 }
14
15 public static void main ( String [] args ) {
16 Main obj = new Main () ;
17 obj . display () ;
18 }
19 }

./code/[Link]
Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 186 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Java Non Access Modifiers

The non-access modifiers in Java, do not change the accessibility of


variables and methods rather they provide special properties. These
modifiers can alter the behavior of elements as well

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 187 / 187
What is JAVA? Application Types of Java Applications Java Platforms / Editions Features of Java Java Hello World Program The requirement for Java He

Dr. Nikhil Mhala

Contact:
[Link]@[Link]
Landline: 0863-2370961 (Mon-Fri 9 am - 5 pm)
Extension: 5961

Dr. Nikhil Mhala VIT-AP CSE1012 April 27, 2022 188 / 187

You might also like