Introduction to Java Programming Basics
Introduction to Java Programming Basics
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
[Link]@[Link]
School of Computer Science and Engineering (SCOPE)
VIT-AP University (Besides AP Secretariat) 522237
Agenda I
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?
Application
1 Standalone Application
2 Web Application
3 Enterprise Application
4 Mobile Application
Features of Java
./code/[Link]
[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.
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.
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
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)
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).
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
2. Integer Literals
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
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]
1 char letter = ’a ’;
2
3 String str1 = " Java Programming " ;
4 String str2 = " OOPs " ;
5
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
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]
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]
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]
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]
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]
6. double type
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]
7. float type
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]
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]
Java Operators
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]
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]
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]
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]
./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
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]
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]
Java Output
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]
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]
./code/[Link]
Java Input
./code/[Link]
./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 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]
./code/[Link]
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]
./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
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]
./code/[Link]
Practice Questions
./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
./code/[Link]
./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
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]
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 }
./code/[Link]
./code/[Link]
./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
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]
./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
./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
./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
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]
Java Array
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;
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
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.
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]
./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
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]
./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
Example
int[][] a = new int[3][4];
1 int [][] a = {
2 {1 , 2 , 3} ,
3 {4 , 5 , 6 , 9} ,
4 {7} ,
5 };
6
./code/[Link]
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
./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
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
./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
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
./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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
./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
./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
./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
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
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
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
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
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
./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
./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
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
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
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
./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
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
./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
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
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
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
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
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
./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
./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?
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
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
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 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
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]
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
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
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
./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
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
./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
./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
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
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
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
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
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
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
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
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
./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
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
./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
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
./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
./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
./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
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
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
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
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
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
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
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 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
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
./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
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
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