[go: up one dir, main page]

0% found this document useful (0 votes)
5 views22 pages

java_1

This document provides an introduction to Java programming, covering its history, applications, and core concepts such as classes, methods, and data types. It includes examples of Java programs, programming structure, and key features like access modifiers, arrays, and the ArrayList class. Additionally, it discusses advanced topics like inner classes and static variables/methods.

Uploaded by

Anup Sarker
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)
5 views22 pages

java_1

This document provides an introduction to Java programming, covering its history, applications, and core concepts such as classes, methods, and data types. It includes examples of Java programs, programming structure, and key features like access modifiers, arrays, and the ArrayList class. Additionally, it discusses advanced topics like inner classes and static variables/methods.

Uploaded by

Anup Sarker
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
You are on page 1/ 22

CSE 12031

Object Oriented Programming [C++]

Chapter 5:
Java Programming -01
Java: Introduction
What is Java? 2 API: Application Programming Interface

Java is a popular object oriented programming


language, created in 1995.
It is owned by Oracle, and more than 3
billion devices run Java.
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications Java Editions: 3 types
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!

Java Specification: JDK: Java Development Kit


java syntax & semantic
Java: Introduction
IDE: Integrated Development 3 Java: Class Structure
Environment

Java : Methods

Java: Basic Concepts

Java : Method Calling


Java: Introduction
Java: Naming Convension 4 Java : Package

(class Name)

(variable, Method Name)

Java: println() method


Java: Programming Structure

Java : System.out

Note: Each java program must have a class that


contain main() method
Java: Introduction
Java: Compiling 5

Java Virtual Machine, or JVM, loads, verifies and executes Java bytecode. It is
known as the interpreter or the core of Java programming language because it
executes Java programming.
Java: Program
Program 1: print a message/text 6 Program 2 (ex): call main() of second
package CSE1203; package CSE1203;
public class First {
public static void main(String[] args) {
public class First {
System.out.println("Welcome to Java World");
public static void main(String[] args) { Second.main(null);
System.out.print("Welcome to Java World"); }
} }
}
Output
Output
Welcome to Java World
Welcome to Java World Talk less listen more
Notes: The class name must be the same as the .java Notes: In the First class just write the class name
source filename and package should be declared at first if you want to call other classes method

Program 2: print a message/text


package CSE1203;
public class Second {
public static void main(String[] args) {
System.out.print("Talk less listen more");
}
}
Notes: A class Second is created under package
CSE1203
Java: Program
Program 2 (ex): change Second class 7 package in Java
package CSE1203;
public class Second {
public static void main(String[] args) {
System.out.println("Talk less listen more");
Display();
}
public static void Display() {
System.out.println("Honesty is the best policy");
}
}

Output Access Modifiers


Welcome to Java World 1. public : access from everywhere
Talk less listen more 2. private: access only inside the class
Honesty is the best policy 3. protected: access from everywhere
4. default: access from everywhere
Notes: From First class, main() of Second class is
called and inside the main() of Second, Display
method is called

There are no restrictions on the number of classes


that can be present in one Java program. But each
Java program should have only one class declared
with public access specifier. There cannot be two
public classes in a single Java program.
Java: Program
Program 3: private method 8 Program 4: static method
package CSE1203; package CSE1203;
public class Second { public class Second {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Talk less listen more"); System.out.println("Talk less listen more");
Display(); Second.Display();
} }
private static void Display() { private static void Display() {
System.out.println("Honesty is the best policy"); System.out.println("Honesty is the best policy");
} }
} }

Output Notes: static method can be called by its class


name with (.) operator. But if you remove the static
Talk less listen more from Display() in the following then it can not be
Honesty is the best policy called from main(). Remember that a non-static
method can not be called from a static method
Notes: It is run from Second but in the following it
is run from First. It produces error as Display() is package CSE1203;
private to Second class public class Second {
public static void main(String[] args) {
package CSE1203; System.out.println("Talk less listen more");
public class First { Display(); //produces error
public static void main(String[] args) { }
System.out.println("Welcome to Java World"); Private void Display() {
Second.Display()); System.out.println("Honesty is the best policy");
} }
} }
Java: Program
Programming Style 9 Data Types: primitives

Notes: Use proper spacing and indentation


Data Types: reference
Programming Style : block

string class: few methods


charAt() isEmpty() toLowerCase()
compareTo() length() toUpperCase()
concat() replace() toString()
equals() substring() trim()
Java: Program
Primitive vs Reference Types 10 Immutable Objects: primitives

Memory Allocation
String class creates objects in String
Constant Pool (SCP) and only one
object is created for same literal
Java: Program
scanner class : next() method 11 scanner class : nextInt() method
package CSE1203;

import java.util.Scanner;

public class First {


Kamal public static void main(String[] args) {
Your name is Kamal Scanner scan=new Scanner(System.in);
int a=scan.nextInt();
int b=scan.nextInt();
int s=a+b;
Methods: scanner class System.out.println("sum="+s);
}
}

Here two integers input from keyboard


stored in variable a and b. Then s stored
sum of a & b

Built in Statements
if, switch, for, while,do-while
are the same as C/C++l
Java: Program
Local Variable and its Scope 12 Java: array declaration

dataType[] arr; (or)


dataType []arr; (or)
dataType arr[];

Java: array instantiation


arrayRefVar=new datatype[size];

Example: Local Variable Example: array


package CSE1203; package CSE1203;
public class First { public class First {

public static void main(String[] args) { public static void main(String[] args) {
if(true) { int[] ax=new int[5];
int i=5; ax[0]=10; ax[1]=20;
System.out.println("i="+i); for(int i=0;i<5;i++)
} System.out.println(" "+ax[i]);
System.out.println("i="+i); }
} }

Here error occurs at second println as i


declared inside block if
Java: Program
Example: array pass by reference 13 Example: anonymous object
package CSE1203;
import java.awt.Point;
public class First {
public static void main(String[] args) {
Point p=getPoint();
System.out.println("p="+p);
}
private static Point getPoint() {
return new Point(1,2); //annonymous object
}
}

Java: array of anonymous object


package CSE1203;
Here array is an object and it is passed by import java.awt.Point;
public class First {
reference public static void main(String[] args) {
int[] numbers=getNumber();
printArray(numbers);
Java: anonymous object }
public static void printArray(int[] numbers) {
Anonymous means Nameless. An anonymous for(int i=0;i<numbers.length;i++)
object is basically a value that has been created but System.out.print(" "+numbers[i]);
}
has no name. public static int[] getNumber() {
return new int[] {1,2,3,4,5};
}
}
Java: Program
Java: Array class methods for 14 Java: Arrays BinarySearch() method
package CSE1203;
import java.util.Arrays;
public class First {
public static void main(String[] args) {
int[] numbers= {10,20,70,90,30,80};
System.out.println("Initial Array:");
printArray(numbers);
Arrays.sort(numbers);
System.out.println("\nSorted Array:");
Java: Arrays sort() method printArray(numbers);
int i=Arrays.binarySearch(numbers, 44);
package CSE1203;
//returns index or if not found returns -ve number
import java.util.Arrays; System.out.println("\nIndex of found element="+i);
}
public class First { private static void printArray(int[] numbers) {
public static void main(String[] args) { for(int i=0;i<numbers.length;i++)
int[] numbers= {10,20,70,90,30,80}; System.out.print(" "+numbers[i]);
System.out.println("Initial Array:"); }
printArray(numbers); }
Arrays.sort(numbers);
System.out.println("\nSorted Array:");
Java: anonymous object
printArray(numbers); Java: Arrays fill() method
}
private static void printArray(int[] numbers) {
for(int i=0;i<numbers.length;i++)
System.out.print(" "+numbers[i]);
}
}
Java: Program
Java: Arrays toString() method 15 Java: Variable Length Parameter(…)
package CSE1203; package CSE1203;
import java.util.Arrays; import java.util.Arrays;
public class First { public class First {
public static void main(String[] args) { public static void main(String[] args) {
int[] ax= {10,20,70,90,30,80}; int[] ax= {10,20,70,90,30,80};
System.out.println("Initial Array:"); System.out.println(sum(1,2,3));
System.out.println(Arrays.toString(ax)); System.out.println(sum(1,2,3,4));
} System.out.println(sum(ax));
} }
public static int sum(int...ax) {
int s=0;
Output for(int i=0;i<ax.length;i++)
s=s+ax[i];
Initial Array: return s;
[10, 20, 70, 90, 30, 80] }
}
Note: The toString() method returns the
String representation of the object. By Java: 2D array Declaration
overriding the toString() method of the
Object class, we can return values of the
object, so we don't need to write much code.
Java: Program
Java: ArrayList class 16Example: ArrayList class
package CSE1203;
import java.util.ArrayList;
import java.util.Arrays;
public class First {
public static void main(String[] args) {
ArrayList<String> fruits=new ArrayList<>();
fruits.add("Apple");//insert at back
fruits.add("Mango");//insert at back
fruits.add("Orange");//insert at back
System.out.println(fruits);
fruits.add(0,"Banana");//insert at index 0
Note: The ArrayList class is a resizable/ System.out.println(fruits);
variable length array. It includes methods add(), System.out.println(fruits.get(0)); //get element
set(), remove(), size(), clear() etc. of index 0
fruits.set(1, "Guava"); //change value at index 1
Note: To display the content of an array, it System.out.println(fruits);
fruits.remove(2); //delete value at index 2
should be written inside the print() method. To System.out.println(fruits);
write toString() method after array object is fruits.remove("Orange"); //delete by value
not mandatory System.out.println(fruits.toString()); //same
System.out.println(fruits.size()); //Total
elements
fruits.sort(null); //sort elements
Collections.sort(fruits); //Alternative sort
fruits.clear(); //delete all elements
System.out.println(fruits);
}
}
Java: Program
Java: for-each loop 17

Example: for-each loop


package CSE1203;
import java.util.ArrayList;
import java.util.Arrays;

public class First {


public static void main(String[] args) {
ArrayList<String> fruits=new ArrayList<>();
fruits.add("Orange");//insert at back
fruits.add("Mango");//insert at back
fruits.add("Apple");//insert at back
for(String i:fruits)
System.out.print(" "+i);
}
}
Java: class & object
Java: class-object 18

package CSE1203; package CSE1203;


import java.awt.Point;
import java.awt.Point;
public class First {
class Circle{
public static void main(String[] args) { Point c;
int r;
Circle c1=new Circle(new Point(4,2),3);
System.out.println("Center="+c1.getCenter()); public
System.out.println("Area="+c1.Area()); Circle(Point c,int r){
} this.c=c;
} this.r=r;
}
double Area() {
return 2*3.14*r;
Note: here new Point(4,2) is anonymous }
object. Class Circle can be define in the same Point getCenter() {
file with main() or in the different file. return c;
}
}
Java: static variable/method
Java: static variable/method 19 package CSE1203;
import java.awt.Point;

class Circle{
Point c;
int r;
static int count=0;
public
Circle(Point c,int r){
this.c=c;
this.r=r;
count++;
Java: static variable/method }
double Area() {
package CSE1203; return 2*3.14*r;
import java.awt.Point; }
Point getCenter() {
public class First { return c;
}
public static void main(String[] args) { static int getCount() {
Circle c1=new Circle(new Point(4,2),3); return count;
Circle c2=new Circle(new Point(1,2),4); }
System.out.println("Center="+c1.getCenter()); }
System.out.println("Area="+c1.Area());
System.out.println("Circle
Count="+c1.getCount());
}
Note: here count is a static variable and it
} is common for all objects. To return a
static variable a static method is used.
Java: static variable/method
Java: Visibility Modifiers 20 Java: Inner class

In Java, inner class refers to the class that is


declared inside class or interface.Inner classes
are a security mechanism in Java. We know a
class cannot be associated with the access modifier
private, but if we have the class as a member of
other class, then the inner class can be made
private. And this is also used to access the private
members of a class.

package CSE1203;
public class First {
Example: Visibility Modifiers public static void main(String[] args) {
CSE1203_01 CSE1203_02 //outer is Outer class object
Outer outer=new Outer();
outer.Display();
//in is Inner class object
Outer.Inner in = new Outer().new Inner(45);
in.getY();
}
}

Note: Here object of private Inner class is created


via public Outer class. Outer class private members
Note: public class can be access from any package can be accessed by inner class
but default class can be accessed within the
package
Java: inner class
Java: Outer class and 21
Java: Inner class example
inner class example package CSE1203;
public class First {
package CSE1203;
public static void main(String[] args) {
import java.awt.Point; //outer is Outer class object
Outer outer=new Outer();
public class Outer{ outer.Display();
private int x; //in is Inner class object
class Inner{ Outer.Inner in = new Outer().new Inner(45);
private int y; in.sum();
}
public Inner(int y) {
}
this.y=y;
}
package CSE1203; public void sum() {
public void setY(int y) {
System.out.println("inside
this.y=y; class Outer{ sum="+(x+y));
} private int x=20; }
public int getY() { class Inner{ }
return y; private int y;
public void Display() {
} public Inner(int y) {
this.y=y;
Inner inner=new Inner(0);
} inner.setY(12);
}
public void setY(int y) { System.out.println(inner.getY());
public void Display() {
this.y=y; }
Inner inner=new Inner(0);
} }
inner.setY(12);
public int getY() {
System.out.println(inner.getY()); return y;
} }
}
22

THANK YOU

You might also like