By S.Asha Assistant Professor Dept of Information Technology S.A.Engineering College OBJECT ORIENTED PROGRAMMING
INTRODUCTION TO OOPAND JAVA FUNDAMENTALS Object Oriented Programming - Abstraction – objects and classes – Encapsulation-Inheritance - Polymorphism- OOP in Java – Characteristics of Java – Fundamental Programming Structures in Java – Defining classes in Java – constructors, methods -access specifiers - static members -Comments, Data Types, Variables, Operators, Control Flow, Arrays, Packages.
INTRODUCTION TO JAVA JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code. JAVA is a object-oriented programming language. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java.
CHARACTERISTICS OF JAVA:
1.Object Oriented Programming: Java supports object oriented programming concepts such as Encapsulation Abstraction Inheritance Polymorphism 2. Simple: Java is very easy to learn, and its syntax is simple and easy to understand. 3.Secured: Java is secured because java programs run inside a virtual machine sandbox. JVM consist of class loader and byte-code verifier. 4.Platform Independent: Java is a write once, run anywhere language.
5.Robust: Java is robust because: It uses strong memory management. Java provides automatic garbage collection for collecting the unused objects. There is a lack of pointers that avoids security problems. 6. Portable: Java is portable because It provides the facility to execute the Java byte code in any OS making it platform independent. 7. Architecture-neutral: Java is architecture neutral because there is no implementation dependent features.
8. Dynamic: Java is a dynamic language because It supports the dynamic loading of classes i.e classes are loaded on demand. 9. Interpreted: Java is an interpreted programming language because in high level programming languages like Java , the programs are converted into byte codes when they are compiled. At runtime java bytecode is interpreted(converted) into machine code using java interpreter(part of JVM) in order to run the program. 10. High-performance: Since JAVA supports features like just-in- time compiler, multithreading, garbage collection which increases the performance of java.
 11. Distributed: Here the applications running on different machines which are connected via internet/network can interact/access the resources with each other using java's pre- built libraries like RMI, EJB etc.
12. Multi-threaded: Java is a multithreaded programming language because In JAVA a single program can perform two or more than two tasks simultaneously.
FUNDAMENTAL PROGRAMMING STRUCTURS IN JAVA
DEFINING CLASSES IN JAVA A class is a user defined data type from which objects are created. It is a template or blueprint from which objects are created. A class in Java can contain: Fields Methods Constructors Blocks
VARIABLES  A variable is a container which stores the value while the Java program is executed.  Variable is a name of memory location.  A variable is assigned with a data type.
TYPES OF VARIABLES There are three types of variables supported in JAVA. 1. Local Variable 2. Instance Variable 3. Static Variable
1) Local Variable  A variable declared inside the body of the method is called local variable. 2) Instance Variable:  A variable declared inside the class but outside the body of the method, is called an instance variable. 3) Static variable:  A variable that is declared as static is called a static variable. Memory allocation for static variables happens only once when the class is loaded in the memory.
DATA TYPES IN JAVA:
Data Type Default Value Default size boolean false 1 bit char 'u0000' 2 byte byte 0 1 byte short 0 2 byte int 0 4 byte long 0L 8 byte float 0.0f 4 byte double 0.0d 8 byte
COMMENTS Comments are the statements in a program that are not executed by the compiler and interpreter. Advantages:  Comments are used to make the program more readable by adding the details of the code.  It makes easy to maintain the code and to find the errors easily.  The comments can be used to provide information or explanation about the variable, method, class, or any statement.
TYPES OF JAVA COMMENTS  There are three types of comments in Java.  Single Line Comment  Multi Line Comment  Documentation Comment
PROGRAM public class JavaPrograms { public static void main(String[] args) { int id=10; System.out.println("id="+id); } }
Methods in JAVA:  A method in Java is a collection of statements that perform some specific task and return the result to the caller.
ACCESS SPECIFIERS The access modifiers in Java specify the accessibility or scope of a field, method, constructor, or class.  There are four types of Java access modifiers:  Default  Private  Public  Protected
Access Modifier within class within package outside package by subclass only outside package Private Y N N N Default Y Y N N Protected Y Y Y N Public Y Y Y Y
class Student { int id=1; String name=“XXX”; int age=15; void display() { System.out.println(id+" "+name+" "+age); } public static void main(String args[]) { } }
OBJECTS  It is a basic unit of Object-Oriented Programming and represents real life entities. An object consists of :  State: It is represented by attributes of an object.  Behavior: It is represented by methods of an object.  Identity: It gives a unique name to an object and enables one object to interact with other objects.  Example of an object: dog
Syntax for creating an object: classname objectname=new classname(); Example: Student s=new Student(); Where Student – class name S-object name
class Student { int id=1; String name=“XXX”; int age=15; void display() { System.out.println(id+" "+name+" "+age); } public static void main(String args[]) { Student s = new student(); s.display(); } } Note: If we are using methods inside a class, then we must create an object inside the main method()to invoke the method
CONSTRUCTORS  It is a special type of method which is used to initialize an object.  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  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
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
TYPES OF JAVA CONSTRUCTORS There are two types of constructors in Java:  Default constructor (no-arg constructor)  Parameterized constructor
STATIC MEMBERS  The static keyword in Java is used mainly for memory management.  In Java, static members are those which belongs to the class and we can access these members without instantiating the class.  The static keyword can be used for variables, methods, blocks, class etc.,
STATIC FIELDS  We can create a static field by using the keyword static.  The static fields can be accessed both by the static as well as non-static methods.  We can access static fields using the class name (without creating an object).
STATIC METHODS  We can create a static method by using the keyword static.  Static methods can access only static fields.  To access static methods there is no need to instantiate the class, we can do it just using the class name.
STATIC BLOCKS  These are a block of codes with a static keyword.  In general, these are used to initialize the static members.  JVM executes static blocks before the main method at the time of class loading.
public class statmem_example { static int data; void display() { int data=15; System.out.println("data="+data); } static void print() { System.out.println("The value of data="+data); }
static { data=10; System.out.println("The static method is invoked"); } public static void main(String args[]) { statmem_example ex=new statmem_example(); statmem_example.print(); ex.display(); } } } }
OPERATORS Operator is a symbol that is used to perform mathematical and logical operations. For example: +, -, *, / etc. There are many types of operators in Java which are given below:  Unary Operator  Binary operator  Arithmetic Operator  Shift Operator  Relational Operator  Bitwise Operator  Logical Operator
Unary Operators:  Operations are performed on single operands.  They are used to increment, decrement or negate a value.
public class Unary_Example { public static void main(String args[]) { int a=10; System.out.println("Value of a=“+(-a)); System.out.println("Value of a=“+(a++)); System.out.println("Value of a="+(--a)); } }
OUTPUT: Value of a=-10 Value of a=10 Value of a=10
BINARY OPERATORS  Operation is performed on two operands.  Binary operators that are supported in JAVA are  Arithmetic Operator  Shift Operator  Relational Operator  Bitwise Operator  Logical Operator
ARITHMETIC OPERATORS  Addition  Subtraction  Multiplication  Division  Modulus Example:  int A=10,B=20;
RELATIONAL OPERATORS  A relational operator checks the relationship between two operands.  If the relation is true, it returns 1;  If the relation is false, it returns value 0.  Relational operators are used in decision making and loops.
RELATIONAL OPERATORS  equal to  not equal to  greater than  less than  greater than or equal to  less than or equal to  instance of
PROGRAM public class instanceof_example { public static void main(String args[]){ instanceof_example s=new instanceof_example(); System.out.println(s instanceof instanceof_example);//true } }
BITWISE OPERATORS:  Bitwise operator works on bits and performs bit-by-bit operation.  Example:  int a = 60, b = 13; a b a&b a|b a^b ~a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0
LOGICAL OPERATORS  Logical operators are used to check whether an expression is true or false .  They are used in decision making.  The logical operators that are supported in JAVA are  &&(logical and)  || (logical or)  ! (logical not)  Example:  A=true; B=false;
SHIFT OPERATORS  The shift operator is a java operator that is used to shift bit patterns right or left.  There are two types of shift operators. They are  Left shift(<<) and  Right shift(>>)  EXAMPLE: 8<<2=0  8>>2=2
ASSIGNMENT OPERATOR = (Simple assignment operator) += (Addition assignment operator) -= (Subtraction assignment operator) *= (Multiplication assignment operator) /=(Division Assignment operator) %=(Modulo division assignment operator)
public class AssignmentOp_Example { public static void main(String[] args) { int a= 20, b = 15; System.out.println("a+=b="+(a+=b)); System.out.println("a-=b="+(a-=b)); System.out.println("a*=b="+(a*=b)); System.out.println("a/=b="+(a/=b)); System.out.println("a%=b="+(a%=b)); } }
OUTPUT: a+=b=35 a-=b=20 a*=b=300 a/=b=20 a%=b=5
TERNARY OPERATOR Conditional Operator ( ? : ) SYNTAX: variable x = (expression) ? value if true : value if false Example: int a=10,b=5; b=a>b?a:b;
PRECEDENCE OF JAVA OPERATORS Precedence operator tells us how an expression is evaluated. Certain operators have higher precedence than others; For example, the multiplication operator has higher precedence than the addition operator.  For example, consider the following expression,  x = 10 + 5 * 2;  So, the output is 20, not 30.  Because operator * has higher precedence than +.
Category Operator Associativity Postfix >() [] . (dot operator) Left to right Unary >++ - - ! ~ Right to left Multiplicative >* / Left to right Additive >+ - Left to right Shift >>> >>> << Left to right Relational >> >= < <= Left to right Equality >== != Left to right Bitwise AND >& Left to right Bitwise XOR >^ Left to right Bitwise OR >| Left to right Logical AND >&& Left to right Logical OR >|| Left to right Conditional ?: Right to left Assignment >= += -= *= /= %= >>= <<= &= ^= |= Right to left
CONTROL FLOW Java Control statements control the flow of execution based on data values and conditional logic used in a program.  There are three main categories of control flow statements;  Selection statements: The selection statements checks the condition only once for the program execution. Example: if, if-else and switch.  Iteration statements: Iteration statements execute a block of code for several numbers of times until the condition is true. Example: while, do-while and for.  Transfer statements: Transfer statements are used to transfer the flow of execution from one statement to another. break, continue, return.
SWITCH CASE STATEMENT It is known as multi-way branching statement. The switch statement begins with a keyword switch, followed by an expression. When the switch statement executes, it compares the value of the expression to the values of each case label. SYNTAX: switch (<expression>) { case label 1: <statement1> case label 2: <statement2> … case label n: <statement> default: <statement> }
 Flow chart for Execution of FOR statements
TRANSFER STATEMENTS  Transfer statements are used to transfer the flow of execution from one statement to another. Continue Statement:  The continue statement in JAVA programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. Break Statement:  When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
public class ProgramContinue { public static void main(String[] args) { System.out.println(“Odd Numbers”); for (int i = 1; i <= 10; ++i) { if (i % 2 == 0) continue; System.out.println(i + “t”); } } }
OUTPUT: Odd Numbers 1 3 5 7 9
PROGRAM FOR BREAK STATEMENTS public class ProgramBreak { public static void main(String[] args) { System.out.println(“Numbers 1 - 10”); for (int i = 1;; ++i) { if (i == 11) break; System.out.println(i + “t”); } } }
OUTPUT: Numbers 1 - 10 1 2 3 4 5 6 7 8 9 10
ARRAYS  Array is a collection of elements of similar data types stored in contiguous memory location.  The array size is fixed.  It is index based and the first element is stored at 0th index.
Advantages of Array:  Code Optimization  Random access Disadvantages of Array:  Inefficient memory usage Types of Array There are two types of array.  One Dimensional Array  Multidimensional Array
One Dimensional Array: One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array. Syntax: dataType[] arrayName=new datatype[size]; Or dataType arrayName []=new datatype[size]; Example: int[] a=new int[5];
DECLARING AN ARRAY  Array can be created using the new keyword.  To allocate memory for array elements we must mention the array size.  The size of an array must be specified by an int value and not long or short.  The default initial value of elements of an array is 0 for numeric types and false for boolean.
Syntax: dataType arrayName[]=new datatype[size]; Or dataType[] arrayName=new datatype[size]; Example: int[] a=new int[5]; OR int a[]=new int[5];
INITIALIZING AN ARRAY: SYNTAX: dataType[] arrayName=new datatype[]{list of values separated by comma}; Example: int[] a=new int[]{12,13,14};
import java.util.*; public class Array_Example { public static void main(String args[]) { int number[]=new int[3]; System.out.println("Enter the array elements"); Scanner s=new Scanner(System.in); number[0]=s.nextInt(); number[1]=s.nextInt(); number[2]=s.nextInt(); System.out.println("The numbers entered are"+number[0]+" t"+number[1]+"t"+number[2]); } }
MULTIDIMENSIONALARRAYS:  A multidimensional array is an array with more than two dimensions.  In multi dimensional array the elements are stored in the form of rows and columns.  Syntax: dataType[][] arrayName=new datatype[rowsize][columnnsize];  Example: int[][] a=new int[3][4];
JAGGED ARRAY  Jagged array is an array of arrays with different row size i.e. with different dimensions. Example:  int[][] a = { {11, 3, 43}, {3, 5, 8, 1}, {9}, };
JAVA PACKAGE  A java package is a group of similar types of classes, interfaces and sub-packages.  Package in java can be categorized in two form, built- in package and user-defined package.  There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.  Note: The package keyword is used to create a package in java.
How to access package from another package? There are two ways to access the package from outside the package. import package.*; import package.classname;
PROGRAM: package Package_Example; public class Hello { public String display() { return "Hello"; } }
import java.util.*; import Package_Example.*; public class Package_Example { public static void main(String args[]) { System.out.println("Enter the student name"); Scanner in=new Scanner(System.in); String name=in.next(); Hello h=new Hello(); System.out.println(h.display()+"t"+name); } }
OBJECT ORIENTED PROGRAMMING  Definition: Object-oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules.  The main aim of OOP is to bind together the data and the functions that operate on these data
OOPs Concepts:  Class  Objects  Data Abstraction  Encapsulation  Inheritance  Polymorphism  Dynamic Binding  Message Passing

Object Oriented Programming unit 1 content for students

  • 1.
    By S.Asha Assistant Professor Dept ofInformation Technology S.A.Engineering College OBJECT ORIENTED PROGRAMMING
  • 2.
    INTRODUCTION TO OOPANDJAVA FUNDAMENTALS Object Oriented Programming - Abstraction – objects and classes – Encapsulation-Inheritance - Polymorphism- OOP in Java – Characteristics of Java – Fundamental Programming Structures in Java – Defining classes in Java – constructors, methods -access specifiers - static members -Comments, Data Types, Variables, Operators, Control Flow, Arrays, Packages.
  • 3.
    INTRODUCTION TO JAVA JAVAwas developed by James Gosling at Sun Microsystems Inc in the year 1995, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code. JAVA is a object-oriented programming language. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java.
  • 4.
  • 5.
    1.Object Oriented Programming:Java supports object oriented programming concepts such as Encapsulation Abstraction Inheritance Polymorphism 2. Simple: Java is very easy to learn, and its syntax is simple and easy to understand. 3.Secured: Java is secured because java programs run inside a virtual machine sandbox. JVM consist of class loader and byte-code verifier. 4.Platform Independent: Java is a write once, run anywhere language.
  • 6.
    5.Robust: Java isrobust because: It uses strong memory management. Java provides automatic garbage collection for collecting the unused objects. There is a lack of pointers that avoids security problems. 6. Portable: Java is portable because It provides the facility to execute the Java byte code in any OS making it platform independent. 7. Architecture-neutral: Java is architecture neutral because there is no implementation dependent features.
  • 7.
    8. Dynamic: Javais a dynamic language because It supports the dynamic loading of classes i.e classes are loaded on demand. 9. Interpreted: Java is an interpreted programming language because in high level programming languages like Java , the programs are converted into byte codes when they are compiled. At runtime java bytecode is interpreted(converted) into machine code using java interpreter(part of JVM) in order to run the program. 10. High-performance: Since JAVA supports features like just-in- time compiler, multithreading, garbage collection which increases the performance of java.
  • 8.
     11. Distributed:Here the applications running on different machines which are connected via internet/network can interact/access the resources with each other using java's pre- built libraries like RMI, EJB etc.
  • 9.
    12. Multi-threaded: Javais a multithreaded programming language because In JAVA a single program can perform two or more than two tasks simultaneously.
  • 10.
  • 11.
    DEFINING CLASSES INJAVA A class is a user defined data type from which objects are created. It is a template or blueprint from which objects are created. A class in Java can contain: Fields Methods Constructors Blocks
  • 12.
    VARIABLES  A variableis a container which stores the value while the Java program is executed.  Variable is a name of memory location.  A variable is assigned with a data type.
  • 13.
    TYPES OF VARIABLES Thereare three types of variables supported in JAVA. 1. Local Variable 2. Instance Variable 3. Static Variable
  • 14.
    1) Local Variable A variable declared inside the body of the method is called local variable. 2) Instance Variable:  A variable declared inside the class but outside the body of the method, is called an instance variable. 3) Static variable:  A variable that is declared as static is called a static variable. Memory allocation for static variables happens only once when the class is loaded in the memory.
  • 15.
  • 16.
    Data Type DefaultValue Default size boolean false 1 bit char 'u0000' 2 byte byte 0 1 byte short 0 2 byte int 0 4 byte long 0L 8 byte float 0.0f 4 byte double 0.0d 8 byte
  • 17.
    COMMENTS Comments are thestatements in a program that are not executed by the compiler and interpreter. Advantages:  Comments are used to make the program more readable by adding the details of the code.  It makes easy to maintain the code and to find the errors easily.  The comments can be used to provide information or explanation about the variable, method, class, or any statement.
  • 18.
    TYPES OF JAVACOMMENTS  There are three types of comments in Java.  Single Line Comment  Multi Line Comment  Documentation Comment
  • 19.
    PROGRAM public class JavaPrograms { publicstatic void main(String[] args) { int id=10; System.out.println("id="+id); } }
  • 20.
    Methods in JAVA: A method in Java is a collection of statements that perform some specific task and return the result to the caller.
  • 21.
    ACCESS SPECIFIERS The accessmodifiers in Java specify the accessibility or scope of a field, method, constructor, or class.  There are four types of Java access modifiers:  Default  Private  Public  Protected
  • 22.
    Access Modifier within class withinpackage outside package by subclass only outside package Private Y N N N Default Y Y N N Protected Y Y Y N Public Y Y Y Y
  • 23.
    class Student { int id=1; Stringname=“XXX”; int age=15; void display() { System.out.println(id+" "+name+" "+age); } public static void main(String args[]) { } }
  • 24.
    OBJECTS  It isa basic unit of Object-Oriented Programming and represents real life entities. An object consists of :  State: It is represented by attributes of an object.  Behavior: It is represented by methods of an object.  Identity: It gives a unique name to an object and enables one object to interact with other objects.  Example of an object: dog
  • 25.
    Syntax for creatingan object: classname objectname=new classname(); Example: Student s=new Student(); Where Student – class name S-object name
  • 26.
    class Student { int id=1; Stringname=“XXX”; int age=15; void display() { System.out.println(id+" "+name+" "+age); } public static void main(String args[]) { Student s = new student(); s.display(); } } Note: If we are using methods inside a class, then we must create an object inside the main method()to invoke the method
  • 27.
    CONSTRUCTORS  It isa special type of method which is used to initialize an object.  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  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
  • 28.
    RULES FOR CREATINGJAVA 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
  • 29.
    TYPES OF JAVACONSTRUCTORS There are two types of constructors in Java:  Default constructor (no-arg constructor)  Parameterized constructor
  • 30.
    STATIC MEMBERS  Thestatic keyword in Java is used mainly for memory management.  In Java, static members are those which belongs to the class and we can access these members without instantiating the class.  The static keyword can be used for variables, methods, blocks, class etc.,
  • 31.
    STATIC FIELDS  Wecan create a static field by using the keyword static.  The static fields can be accessed both by the static as well as non-static methods.  We can access static fields using the class name (without creating an object).
  • 32.
    STATIC METHODS  Wecan create a static method by using the keyword static.  Static methods can access only static fields.  To access static methods there is no need to instantiate the class, we can do it just using the class name.
  • 33.
    STATIC BLOCKS  Theseare a block of codes with a static keyword.  In general, these are used to initialize the static members.  JVM executes static blocks before the main method at the time of class loading.
  • 34.
    public class statmem_example{ static int data; void display() { int data=15; System.out.println("data="+data); } static void print() { System.out.println("The value of data="+data); }
  • 35.
    static { data=10; System.out.println("The static methodis invoked"); } public static void main(String args[]) { statmem_example ex=new statmem_example(); statmem_example.print(); ex.display(); } } } }
  • 36.
    OPERATORS Operator is asymbol that is used to perform mathematical and logical operations. For example: +, -, *, / etc. There are many types of operators in Java which are given below:  Unary Operator  Binary operator  Arithmetic Operator  Shift Operator  Relational Operator  Bitwise Operator  Logical Operator
  • 38.
    Unary Operators:  Operationsare performed on single operands.  They are used to increment, decrement or negate a value.
  • 39.
    public class Unary_Example { publicstatic void main(String args[]) { int a=10; System.out.println("Value of a=“+(-a)); System.out.println("Value of a=“+(a++)); System.out.println("Value of a="+(--a)); } }
  • 40.
    OUTPUT: Value of a=-10 Valueof a=10 Value of a=10
  • 41.
    BINARY OPERATORS  Operationis performed on two operands.  Binary operators that are supported in JAVA are  Arithmetic Operator  Shift Operator  Relational Operator  Bitwise Operator  Logical Operator
  • 42.
    ARITHMETIC OPERATORS  Addition Subtraction  Multiplication  Division  Modulus Example:  int A=10,B=20;
  • 43.
    RELATIONAL OPERATORS  Arelational operator checks the relationship between two operands.  If the relation is true, it returns 1;  If the relation is false, it returns value 0.  Relational operators are used in decision making and loops.
  • 44.
    RELATIONAL OPERATORS  equalto  not equal to  greater than  less than  greater than or equal to  less than or equal to  instance of
  • 45.
    PROGRAM public class instanceof_example { publicstatic void main(String args[]){ instanceof_example s=new instanceof_example(); System.out.println(s instanceof instanceof_example);//true } }
  • 46.
    BITWISE OPERATORS:  Bitwiseoperator works on bits and performs bit-by-bit operation.  Example:  int a = 60, b = 13; a b a&b a|b a^b ~a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0
  • 47.
    LOGICAL OPERATORS  Logicaloperators are used to check whether an expression is true or false .  They are used in decision making.  The logical operators that are supported in JAVA are  &&(logical and)  || (logical or)  ! (logical not)  Example:  A=true; B=false;
  • 48.
    SHIFT OPERATORS  Theshift operator is a java operator that is used to shift bit patterns right or left.  There are two types of shift operators. They are  Left shift(<<) and  Right shift(>>)  EXAMPLE: 8<<2=0  8>>2=2
  • 49.
    ASSIGNMENT OPERATOR = (Simpleassignment operator) += (Addition assignment operator) -= (Subtraction assignment operator) *= (Multiplication assignment operator) /=(Division Assignment operator) %=(Modulo division assignment operator)
  • 50.
    public class AssignmentOp_Example{ public static void main(String[] args) { int a= 20, b = 15; System.out.println("a+=b="+(a+=b)); System.out.println("a-=b="+(a-=b)); System.out.println("a*=b="+(a*=b)); System.out.println("a/=b="+(a/=b)); System.out.println("a%=b="+(a%=b)); } }
  • 51.
  • 52.
    TERNARY OPERATOR Conditional Operator( ? : ) SYNTAX: variable x = (expression) ? value if true : value if false Example: int a=10,b=5; b=a>b?a:b;
  • 53.
    PRECEDENCE OF JAVAOPERATORS Precedence operator tells us how an expression is evaluated. Certain operators have higher precedence than others; For example, the multiplication operator has higher precedence than the addition operator.  For example, consider the following expression,  x = 10 + 5 * 2;  So, the output is 20, not 30.  Because operator * has higher precedence than +.
  • 54.
    Category Operator Associativity Postfix>() [] . (dot operator) Left to right Unary >++ - - ! ~ Right to left Multiplicative >* / Left to right Additive >+ - Left to right Shift >>> >>> << Left to right Relational >> >= < <= Left to right Equality >== != Left to right Bitwise AND >& Left to right Bitwise XOR >^ Left to right Bitwise OR >| Left to right Logical AND >&& Left to right Logical OR >|| Left to right Conditional ?: Right to left Assignment >= += -= *= /= %= >>= <<= &= ^= |= Right to left
  • 55.
    CONTROL FLOW Java Controlstatements control the flow of execution based on data values and conditional logic used in a program.  There are three main categories of control flow statements;  Selection statements: The selection statements checks the condition only once for the program execution. Example: if, if-else and switch.  Iteration statements: Iteration statements execute a block of code for several numbers of times until the condition is true. Example: while, do-while and for.  Transfer statements: Transfer statements are used to transfer the flow of execution from one statement to another. break, continue, return.
  • 56.
    SWITCH CASE STATEMENT Itis known as multi-way branching statement. The switch statement begins with a keyword switch, followed by an expression. When the switch statement executes, it compares the value of the expression to the values of each case label. SYNTAX: switch (<expression>) { case label 1: <statement1> case label 2: <statement2> … case label n: <statement> default: <statement> }
  • 57.
     Flow chartfor Execution of FOR statements
  • 58.
    TRANSFER STATEMENTS  Transferstatements are used to transfer the flow of execution from one statement to another. Continue Statement:  The continue statement in JAVA programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. Break Statement:  When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
  • 59.
    public class ProgramContinue { publicstatic void main(String[] args) { System.out.println(“Odd Numbers”); for (int i = 1; i <= 10; ++i) { if (i % 2 == 0) continue; System.out.println(i + “t”); } } }
  • 60.
  • 61.
    PROGRAM FOR BREAKSTATEMENTS public class ProgramBreak { public static void main(String[] args) { System.out.println(“Numbers 1 - 10”); for (int i = 1;; ++i) { if (i == 11) break; System.out.println(i + “t”); } } }
  • 62.
    OUTPUT: Numbers 1 -10 1 2 3 4 5 6 7 8 9 10
  • 63.
    ARRAYS  Array isa collection of elements of similar data types stored in contiguous memory location.  The array size is fixed.  It is index based and the first element is stored at 0th index.
  • 64.
    Advantages of Array: Code Optimization  Random access Disadvantages of Array:  Inefficient memory usage Types of Array There are two types of array.  One Dimensional Array  Multidimensional Array
  • 65.
    One Dimensional Array:One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array. Syntax: dataType[] arrayName=new datatype[size]; Or dataType arrayName []=new datatype[size]; Example: int[] a=new int[5];
  • 66.
    DECLARING AN ARRAY Array can be created using the new keyword.  To allocate memory for array elements we must mention the array size.  The size of an array must be specified by an int value and not long or short.  The default initial value of elements of an array is 0 for numeric types and false for boolean.
  • 67.
    Syntax: dataType arrayName[]=new datatype[size]; Or dataType[]arrayName=new datatype[size]; Example: int[] a=new int[5]; OR int a[]=new int[5];
  • 68.
    INITIALIZING AN ARRAY: SYNTAX: dataType[]arrayName=new datatype[]{list of values separated by comma}; Example: int[] a=new int[]{12,13,14};
  • 69.
    import java.util.*; public classArray_Example { public static void main(String args[]) { int number[]=new int[3]; System.out.println("Enter the array elements"); Scanner s=new Scanner(System.in); number[0]=s.nextInt(); number[1]=s.nextInt(); number[2]=s.nextInt(); System.out.println("The numbers entered are"+number[0]+" t"+number[1]+"t"+number[2]); } }
  • 70.
    MULTIDIMENSIONALARRAYS:  A multidimensionalarray is an array with more than two dimensions.  In multi dimensional array the elements are stored in the form of rows and columns.  Syntax: dataType[][] arrayName=new datatype[rowsize][columnnsize];  Example: int[][] a=new int[3][4];
  • 71.
    JAGGED ARRAY  Jaggedarray is an array of arrays with different row size i.e. with different dimensions. Example:  int[][] a = { {11, 3, 43}, {3, 5, 8, 1}, {9}, };
  • 72.
    JAVA PACKAGE  Ajava package is a group of similar types of classes, interfaces and sub-packages.  Package in java can be categorized in two form, built- in package and user-defined package.  There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.  Note: The package keyword is used to create a package in java.
  • 74.
    How to accesspackage from another package? There are two ways to access the package from outside the package. import package.*; import package.classname;
  • 75.
    PROGRAM: package Package_Example; public classHello { public String display() { return "Hello"; } }
  • 76.
    import java.util.*; import Package_Example.*; publicclass Package_Example { public static void main(String args[]) { System.out.println("Enter the student name"); Scanner in=new Scanner(System.in); String name=in.next(); Hello h=new Hello(); System.out.println(h.display()+"t"+name); } }
  • 77.
    OBJECT ORIENTED PROGRAMMING Definition: Object-oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules.  The main aim of OOP is to bind together the data and the functions that operate on these data
  • 78.
    OOPs Concepts:  Class Objects  Data Abstraction  Encapsulation  Inheritance  Polymorphism  Dynamic Binding  Message Passing