UCSC012 Internet Programming: Dr.S.Sumathi, Assistant Professor - Senior Grade Sri Ramakrishna Institute of Technology

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

UCSC012

INTERNET
PROGRAMMING
Dr.S.Sumathi,
Assistant Professor – Senior Grade
Sri Ramakrishna Institute of Technology
Introduction to Java Programming

■ Java is a class based object oriented programming language that is used in distributed environment on the internet .
■ It is a high level programming language that is easy to read and understand.
■ Java is popularly used in console, graphical interface, web and mobile applications, gaming applications, embedded
systems.
■ Java is also used as a software to develop devices such as washing machine, televisions, air conditioners, online
banking and shopping.
■ James Gosling and Sun Microsystems – 1991.
■ Write once and run anywhere – Code will be written only once but it run on any platforms
■ Java Standard Edition – core libraries like java.lang, java.util
■ Java Enterprise Edition – JSP, Java Servlets
■ Java Micro Edition – cell phones, set top boxes, hand helds

11/13/2021 Internet Programming - SRIT 2


Java Virtual Machine

■ Java code is converted into bit code


using a compiler and then executed
by Java interpreter. The whole java
code runs on Java Virtual machine
which provides a run time
environment.

11/13/2021 Internet Programming - SRIT 3


A Simple Program

■ class Example
■ {
■ public static void main(String args[])
■ {
■ System.out.println(“Hello welcome to Java Programming”);
■ }
■ }
■ C:\>javac Example.java
■ C:\> java Example

11/13/2021 Internet Programming - SRIT 4


Data Types

■ Java is a strongly typed language.

11/13/2021 Internet Programming - SRIT 5


Variables

■ Unit of storage in any program.


■ Variables have a scope, visibility and a lifetime.
■ Variable Declaration:
– Type identifier [=value][,identifier[=value]…];
– Eg: int a=3;

11/13/2021 Internet Programming - SRIT 6


Arrays

■ Group of items that shares a common name.


■ One dimensional arrays
■ Multi dimensional arrays

11/13/2021 Internet Programming - SRIT 7


Declaring & Creating Arrays

■ dataType[] arrayRefVar;
– Eg: double[] myList;
■ Create the array using new operator
– arrayRefVar = new
dataType[arraySize];
■ It creates an array using new
dataType[arraySize].
■ It assigns the reference of the newly created
array to the variable arrayRefVar.

double[] myList = new double[10];

11/13/2021 Internet Programming - SRIT 8


Operators

■ Operators are special symbols that perform ■ Unary Operators


specific operations on one, two, or
– + Unary plus operator; indicates
three operands, and then return a result.
■ positive value (numbers are
■ Simple Assignment Operator
– = Simple assignment operator positive without this, however)
■ Arithmetic Operators ■ - Unary minus operator; negates
– + Additive operator (also used ■ an expression
for String concatenation) – ++ Increment operator; increments
– - Subtraction operator
a value by 1
– * Multiplication operator
– -- Decrement operator; decrements
– / Division operator
– % Remainder operator a value by 1

11/13/2021 Internet Programming - SRIT 9


Operators

■ ! Logical complement operator; ■ Conditional Operators


■ inverts the value of a Boolean – && Conditional-AND
■ Equality and Relational Operators – || Conditional-OR
– == – ?: Ternary (shorthand for
Equal to
– != Not equal to if-then-else statement)
– > Greater than ■ Type Comparison Operator
– >= Greater than or equal to ■ instanceof Compares an object to
– < Less than a specified type
– <= Less than or equal to

11/13/2021 Internet Programming - SRIT 10


Operators

■ Bitwise and Bit Shift Operators ■ Program Demo


■ ~ Unary bitwise complement
■ << Signed left shift
■ >> Signed right shift
■ >>> Unsigned right shift
■ & Bitwise AND
■ ^ Bitwise exclusive OR
■ | Bitwise inclusive OR

11/13/2021 Internet Programming - SRIT 11


Type Comparison Operator

■ The instanceof operator compares an object to a specified type.


■ You can use it to test if an object is an instance of a class, an instance of a subclass, or
an instance of a class that implements a particular interface.
■ When using the instanceof operator, null is not an instance of anything.

11/13/2021 Internet Programming - SRIT 12


Bitwise and Bit Shift Operators

■ The unary bitwise complement operator "~" inverts a bit pattern; it can be

applied to any of the integral types, making every "0" a "1" and every "1"

a "0". For example, a byte contains 8 bits; applying this operator to a value
■ The bitwise & operator performs a
whose bit pattern is "00000000" would change its pattern to "11111111".
bitwise AND operation.

■ The signed left shift operator "<<" shifts a bit pattern to the left, and the

signed right shift operator ">>" shifts a bit pattern to the right. The bit
■ The bitwise ^ operator performs a
pattern is given by the left-hand operand, and the number of positions to bitwise exclusive OR operation.
shift by the right-hand operand. The unsigned right shift operator ">>>"

shifts a zero into the leftmost position, while the leftmost position after

">>" depends on sign extension.


■ The bitwise | operator performs a
bitwise inclusive OR operation.

11/13/2021 Internet Programming - SRIT 13


Control Flow Statements

11/13/2021 Internet Programming - SRIT 14


Infinite Loop

■ // infinite loop
■ for ( ; ; ) {

■ // your code goes here
■ }

11/13/2021 Internet Programming - SRIT 15


■ The for statement also has another form designed for iteration through Collections and arrays.
■ class EnhancedForDemo {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}

11/13/2021 Internet Programming - SRIT 16


Classes

■ A class is a blueprint from which ■ A class can contain any of the following variable types.
individual objects are created. ■ Local variables − Variables defined inside methods,
constructors or blocks are called local variables. The
variable will be declared and initialized within the method
and the variable will be destroyed when the method has
completed.
■ Instance variables - A variable which is created inside
the class but outside the method is known as an instance
variable. Instance variable doesn't get memory at compile
time. It gets memory at runtime when an object or instance
is created. That is why it is known as an instance variable.
■ Class variables − Class variables are variables declared
within a class, outside any method, with the static
keyword.

11/13/2021 Internet Programming - SRIT 17


Class components

■ A class in Java can contain: ■ class <class_name>{  


■ Fields ■     field;  
■ Methods ■     method;  
■ Constructors ■ }  
■ Blocks
■ Nested class and interface

11/13/2021 Internet Programming - SRIT 18


Method in Java

■ In Java, a method is like a function which is used to expose the behavior of an object.
■ Advantage of Method
– Code Reusability
– Code Optimization
■ The new keyword is used to allocate memory at runtime. All objects get memory in
Heap memory area.

11/13/2021 Internet Programming - SRIT 19


Object

■ An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.
■ An object has three characteristics:
– State: represents the data (value) of an object.
– Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
– Identity: An object identity is typically implemented via a unique ID. The value of
the ID is not visible to the external user. However, it is used internally by the JVM
to identify each object uniquely.

11/13/2021 Internet Programming - SRIT 20


Object

■ For Example, Pen is an object. Its name is Reynolds; color is white, known as its state.
It is used to write, so writing is its behavior.
■ An object is an instance of a class. A class is a template or blueprint from which
objects are created. So, an object is the instance(result) of a class.

11/13/2021 Internet Programming - SRIT 21


Object and Class Example: main within the class

/Java Program to illustrate how to define //creating main method inside the Student class
a class and fields public static void main(String args[]){

//Defining a Student class. //Creating an object or instance


Student s1=new Student();//creating an object of
class Student{ Student
//defining fields //Printing values of the object
System.out.println(s1.id);//accessing member
int id;//field or data member or through reference variable
instance variable
System.out.println(s1.name);
String name; }
}

11/13/2021 Internet Programming - SRIT 22


Object and Class Example: main outside the class

■ We can have multiple classes in different Java files or //Creating another class TestStudent1 whi


single Java file. If you define multiple classes in a
single Java source file, it is a good idea to save the file ch contains the main method  
name with the class name which has main() method.
//Java Program to demonstrate having the main method in  
class TestStudent1{  
 
 public static void main(String args[]){  
//another class  
//Creating Student class.    Student s1=new Student();  
class Student{    System.out.println(s1.id);  
int id;  
String name;    System.out.println(s1.name);  
}   }  
}  

11/13/2021 Internet Programming - SRIT 23


3 Ways to initialize object

■ There are 3 ways to initialize object in Java.


– By reference variable
– By method
– By constructor

11/13/2021 Internet Programming - SRIT 24


Object and Class Example: Initialization through
reference

■ Initializing an object means storing data into the  s1.name="Sonoo";  


object. Let's see a simple example where we are
going to initialize the object through a reference  System.out.println(s1.id+" "+s1.name);//
variable.
printing members with a white space  
class Student{  
 int id;    }  
 String name;   }  
}  
class TestStudent2{  
 public static void main(String args[]){  
  Student s1=new Student();  
 s1.id=101;  

11/13/2021 Internet Programming - SRIT 25


Multiple Objects

■ We can also create multiple objects and


store information in it through //Initializing objects  
reference variable.  s1.id=101;  
class Student{   s1.name="Sonoo";  
int id;   s2.id=102;  
String name;   s2.name="Amit";  
}    //Printing data  
class TestStudent3{    System.out.println(s1.id+" "+s1.name);  
public static void main(String args[]){    System.out.println(s2.id+" "+s2.name);  
//Creating objects   }  
 Student s1=new Student();   } 
Student s2=new Student();  

11/13/2021 Internet Programming - SRIT 26


Object and Class Example: Initialization through
method

■ we are creating the two objects of Student class  void displayInformation()


and initializing the value to these objects by {System.out.println(rollno+" "+name);}  
invoking the insertRecord method. Here, we are }  
displaying the state (data) of the objects by class TestStudent4{  
invoking the displayInformation() method.
 public static void main(String args[]){  
class Student{     Student s1=new Student();  
 int rollno;     Student s2=new Student();  

 String name;     s1.insertRecord(111,"Karan");  
  s2.insertRecord(222,"Aryan");  
void insertRecord(int r, String n){  
  s1.displayInformation();  
  rollno=r;  
  s2.displayInformation();  
 name=n;    }  
 }   }  

11/13/2021 Internet Programming - SRIT 27


Object and Class Example: Initialization through a
constructor

class Employee{   public class TestEmployee {  
public static void main(String[] args) {  
int id;  
    Employee e1=new Employee();  
String name;  
    Employee e2=new Employee();  
 float salary;  
    Employee e3=new Employee();  
void insert(int i, String n, float s) {  
    e1.insert(101,"ajeet",45000);  
       id=i;  
    e2.insert(102,"irfan",25000);  
       name=n;       e3.insert(103,"nakul",55000);  
       salary=s;       e1.display();  
    }       e2.display();  
    void display(){System.out.println(id+" "+name     e3.display();  
+" "+salary);}   }  
}   }

11/13/2021 Internet Programming - SRIT 28


Different ways to create an object in Java

■ By new keyword
■ By newInstance() method
■ By clone() method
■ By deserialization
■ By factory method etc.

11/13/2021 Internet Programming - SRIT 29


Anonymous object

■ Anonymous simply means nameless. An object which has no reference is known as an


anonymous object. It can be used at the time of object creation only.

11/13/2021 Internet Programming - SRIT 30


Constructors

■ In Java, a constructor is a block of codes similar to the method. It is called when an


instance of the class is created. At the time of calling constructor, memory for the object
is allocated in the memory.
■ It is a special type of method which is used to initialize the object.
■ Every time an object is created using the new() keyword, at least one constructor is
called.
■ It calls a default constructor if there is no constructor available in the class. In such case,
Java compiler provides a default constructor by default.

11/13/2021 Internet Programming - SRIT 31


Rules for creating Java constructor

■ There are two rules defined for the constructor.


– Constructor name must be the same as its class name
– A Constructor must have no explicit return type
– A Java constructor cannot be abstract, static, final, and synchronized

11/13/2021 Internet Programming - SRIT 32


Types of Java constructors

■ There are two types of constructors in Java:


– Default constructor (no-arg constructor)
– Parameterized constructor

11/13/2021 Internet Programming - SRIT 33


Java Default Constructor

■ A constructor is called "Default ■ //Java Program to create and call a default construct


or  
Constructor" when it doesn't have
any parameter. ■ class Bike1{  
■ //creating a default constructor  
■ <class_name>(){}  
■ Bike1(){System.out.println("Bike is created");}  
■ we are creating the no-arg constructor ■ //main method  
in the Bike class. It will be invoked at ■ public static void main(String args[]){  
the time of object creation. ■ //calling a default constructor  
■ Bike1 b=new Bike1();  
■ }  
■ }  

11/13/2021 Internet Programming - SRIT 34


Java Parameterized Constructor

■ A constructor which has a specific number of parameters is called a parameterized


constructor.
■ The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.

11/13/2021 Internet Programming - SRIT 35


Constructor Overloading in Java

■ In Java, a constructor is just like a method but without return type. It can also be
overloaded like Java methods.
■ Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a
different task. They are differentiated by the compiler by the number of parameters in
the list and their types.

11/13/2021 Internet Programming - SRIT 36


Java Copy Constructor

■ There is no copy constructor in Java. However, we can copy the values from one object
to another like copy constructor in C++.
■ There are many ways to copy the values of one object into another in Java. They are:
■ By constructor
■ By assigning the values of one object into another
■ By clone() method of Object class

11/13/2021 Internet Programming - SRIT 37


Copying values without constructor

■ We can copy the values of one object into another by assigning the objects values to
another object. In this case, there is no need to create the constructor.

11/13/2021 Internet Programming - SRIT 38

You might also like