Subject Name Java Programming (CSE III Year) Department of Computer Science & Engineering Created By: Dr. Avinash Dwivedi
Subject Name Java Programming (CSE III Year) Department of Computer Science & Engineering Created By: Dr. Avinash Dwivedi
Operator in Java
Java Operator Precedence
Arithmetic Operator
Relational Operators
Bitwise Operator examples
Methods in Java Programming Language
Method definition
Method Calling
method returns nothing
Operator in Java
Arithmetic Operator
▰ class BitwiseAndOperator {
▰ public static void main(String[] args){
▰ int A = 10; // 1010
▰ int B = 3; // 0011
▰ int Y;
▰ Y = A & B; // 0010 =2 (decimal)
▰ System.out.println(Y);
▰ }
▰ }
▰ Output 2
Bitwise OR Operator examples
▰ class BitwiseOrOperator {
▰ public static void main(String[] args){
▰ }
▰ }
▰ Output
▰ 11
Unary Operators
▰ The unary operators require only one operand; they perform various operations
such as incrementing/decrementing a value by one, negating an expression, or
inverting the value of a boolean.
Operator Description
Unary plus operator; indicates positive value (numbers are
+
positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean
Program: Unary Operator
▰ class UnaryDemo {
▰ public static void main(String[] args) {
▰ int result = +1; // result is now 1
▰ System.out.println(result);
▰ result--; // result is now 0
▰ System.out.println(result);
▰ result++;
▰ System.out.println(result); // result is now 1
▰ result = -result;
▰ System.out.println(result); // result is now -1
▰ boolean success = false;
▰ System.out.println(success); // false
▰ System.out.println(!success); // true
▰ }
Comparison of ++I and i++
▰ class BreakDemo {
▰ public static void main(String[] args) {
▰ int[] arrayOfInts = { 32, 87, 3, 589,12, 1076, 2000,8, 622, 127 };
▰ int searchfor = 3;
▰ int i;
▰ boolean foundIt = false;
▰ for (i = 0; i < arrayOfInts.length; i++) {
▰ if (arrayOfInts[i] == searchfor) {
▰ foundIt = true;
▰ break;
▰ }
▰ }
▰ if (foundIt) {
▰ System.out.println("Found " + searchfor + " at index " + i);
▰ } else {
▰ System.out.println(searchfor + " not in the array");
▰ } }}
The continue Statement
▰ class ContinueS{
▰ public static void main(String[] args) {
▰ String searchMe = "peter piper picked a " + "peck of pickled peppers";
▰ int max = searchMe.length();
▰ int numPs = 0;
▰ class ReturnArray {
▰ // Returns an array such that first element of array is a+b, and second element is a-b
▰ static int[] getSumAndSub(int a, int b)
▰ {
▰ int[] ans = new int[2];
▰ ans[0] = a + b;
▰ ans[1] = a - b;
▰ return ans; // returning array of elements
▰ }
▰ public static void main(String[] args)
▰ {
▰ int[] ans = getSumAndSub(100, 50);
▰ System.out.println("Sum = " + ans[0]);
▰ System.out.println("Sub = " + ans[1]);
▰ }}
Methods in Java Programming Language
Methods & Function in Java Programming
Function Method
a set of instructions that perform a task. a set of instructions that are associated with
an object.
Functions have independent existence. You Methods do not have independent
can define them outside of the class. existence. They are always defined within a
class, struct, or enum.
Functions are the properties of structured Methods are the properties of Object-
languages like C, C++, Pascal and object oriented language like C#, Java, Swift etc.
based language like JavaScript.
Note: There is no concept of function in
Java.
Functions don't have any reference variables. Methods are called using reference
variables.
Functions are a self describing piece of code. Methods are used to manipulate instance
variable of a class.
Functions are called independently. Methods are called using instance or object.
Methods in Java Programming Language
▰ The use of methods will be our first step in the direction of modular
programming.
▰
Method in Java
▰ Method Signature: Every method has a method signature. It is a part of the method declaration. It includes
the method name and parameter list.
▰ Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of
the method. Java provides four types of access specifier:
▰ Public: The method is accessible by all classes when we use public specifier in our application.
▰ Private: When we use a private access specifier, the method is accessible only in the classes in which it is
defined.
▰ Protected: When we use protected access specifier, the method is accessible within the same package or
subclasses in a different package.
▰ Default: When we do not use any access specifier in the method declaration, Java uses default access
specifier by default. It is visible only from the same package only.
Method related terms
▰ Return Type: Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
▰ Method Name: It is a unique name that is used to define the name of a method. It must be corresponding
to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers,
the method name must be subtraction(). A method is invoked by its name.
▰ Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses.
It contains the data type and variable name. If the method has no parameter, left the parentheses blank.
▰ Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is
enclosed within the pair of curly braces.
Types of Method
▰ Predefined Method
▰ User-defined Method
Predefined Method
▰ public class Demo
▰ {
▰ public static void main(String[] args)
▰ {
▰ // using the max() method of Math class
▰ System.out.print("The maximum number is: " + Math.max(9,7));
▰ }
▰ }
Method Header
Explanation
▰ modifier ? It defines the access type of the method and it is optional to use.
▰ return type ? Method may return a value. It can be any data type.
▰ name of method ? This is the method name. The method signature consists of the
method name and the parameter list.
▰ Parameter List ? Parameter list may two type 1. formal parameter 2. actual
parameter. The list of parameters, it is the type, order, and a number of parameters
of a method. These are optional, the method may contain zero parameters.
▰ formal parameter - The variables defined in the method header are known as formal
parameters or simply parameters. A parameter is like a placeholder.
▰ actual parameter - When a method is invoked, you pass a value to the parameter.
This value is referred to as an actual parameter or argument.
▰ method body ? The method body defines what the method does with the
statements.
Method Call
Complete programming to calculate
largest number among two number
▰ class MethodsExample{
▰ public static void main(String args[])
▰ {
▰ int a = 15;
▰ int b = 8;
▰ int c = max(a, b);
▰ System.out.println("Largest Value = " + c);
▰ }
▰ public static int max(int numA, int numB) {
▰ int result;
▰ if (numA > numB){
▰ result = numA;
▰ }
▰ else{ result = numB; }
▰ return result;
▰ }}
Method Calling
▰ class Methodscall{
▰ public static void main(String args[])
▰ {
▰ int a = 15;
▰ int b = 8;
▰ max(a, b); // function calling
▰ }
▰ public static void max(int x, int y) {
▰ int result;
▰ if (x > y){
▰ result = x;
▰ }
▰ else{ result = y; }
▰ System.out.println("The largest number is " +result);
▰ }}
Void Method