Java Keywords Expression
Java Keywords Expression
Engineering
Course Code: E1UA307C Course Name:Java Programming
1 Java Programming(E1UA402C)
Faculty Name: Sharmistha Dey(GUSCSE202333045) Programe Name: BCA Semester
Prerequisites, Objectives and Outcomes
• Prerequisite of topic: Basic concepts related to java programming
• Objective: To make students aware about the different types of exceptions
along with numerous handling mechanisms available.
• Outcome : 1. Student will be able to know about types of exceptions in
Java.
2. Students will be able to understand various exception handling
mechanisms.
3. Students will be able to implement in practical applications.
2 Java Programming(E1UA402C)
Java Operator
3 Java Programming(E1UA402C)
List of Topics to be covered
▪ Operator in Java
▪ Keywords in java
▪ Java Typecasting
▪ Methods in Java Programming Language
▪ Method definition
▪ Method Calling
▪ method returns nothing
4 Java Programming(E1UA402C)
Operators in Java
⚫ Java provides a rich operator environment.
⚫ An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.
⚫ Operators are used in the program to manipulate data and variables. They usually form a part of the
mathematical or logical expression.
⚫ Java operators can be divided into following categories:
⚫ Arithmetic Operators
⚫ Relational Operators
⚫ Bitwise Operators
⚫ Logical Operators
⚫ Assignment Operators
⚫ conditional operator
⚫ Shift Operators
5 Java Programming(E1UA402C)
Java Operator Precedence
Operator Type Category Precedence
Unary Postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
6 Java Programming(E1UA402C)
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Arithmetic Operator
⚫ The basic arithmetic operations in Java Programming are addition,
subtraction, multiplication, and division.
8 Java Programming(E1UA402C)
Relational Operator
⚫Relational Operators are used to checking relation between two
variables or numbers.
⚫Relational Operators are Binary Operators.
⚫Most of the relational operators are used in “If statement” and inside
Looping statement in order to check truthiness or falseness of
condition.
9 Java Programming(E1UA402C)
Relational Operators
Operators Descriptions Examples
== (equal to) This operator checks the value of two operands, if (2 == 3) is not true.
both are equal, then it returns true otherwise false.
!= (not equal to) This operator checks the value of two operands, if (4 != 5) is true.
both are not equal, then it returns true otherwise
false.
> (greater than) This operator checks the value of two operands, if (5 > 56) is not true.
the left side of the operator is greater, then it returns
true otherwise false.
< (less than) This operator checks the value of two operands if the (2 < 5) is true.
left side of the operator is less, then it returns true
otherwise false.
>= (greater than This operator checks the value of two operands if the (12 >= 45) is not true.
or equal to) left side of the operator is greater or equal, then it
returns true otherwise false.
<= (less than or This operator checks the value of two operands if the (43 <= 43) is true.
equal to) left side of the operator is less or equal, then it
10 Java Programming(E1UA402C)
returns true otherwise false.
Program: Relational Operator
⚫ public static void main(String args[]) {
⚫ int p = 5; Output
⚫ int q = 10; p == q = false
p != q = true
p > q = false
⚫ System.out.println("p == q = " + (p == q) ); p < q = true
q >= p = true
⚫ System.out.println("p != q = " + (p != q) ); q <= p = false
⚫ System.out.println("p > q = " + (p > q) );
⚫ System.out.println("p < q = " + (p < q) );
⚫ System.out.println("q >= p = " + (q >= p) );
⚫ System.out.println("q <= p = " + (q <= p) );
⚫ }
⚫}
11 Java Programming(E1UA402C)
Bitwise Operator examples
⚫ 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
12 Java Programming(E1UA402C)
Bitwise OR Operator examples
⚫ class BitwiseOrOperator {
⚫ public static void main(String[] args){
⚫ }
⚫}
⚫ Output
⚫ 11
13 Java Programming(E1UA402C)
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
14 Java Programming(E1UA402C)
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
⚫ }
15 Java Programming(E1UA402C)
Some interview question related to Java Operator
1. Q. what are the different priority level of
operator?
⚫High priority ⇒ * / %
⚫Low priority ⇒ + –
2. Output of following
public class Test { program? 3. Output of the following?
public class Test {
public static void main(String[] args) {
public static void main(String[] args) {
int x = 2, y = 5;
int x = 9, y = 12; int a = 2, b = 4;
int exp1 = (x * y / x);
boolean exp = 4/3 * (x + 34) < 9 * (3 + y * (2 + a)) / (a + b*y);
int exp2 = (x * (y / x));
System.out.println(exp); }
System.out.println(exp1);
}
System.out.println(exp2); } }
16 Java Programming(E1UA402C)
Some interview question related to Java Operator
What will be output?
class j1{
public static void main(String args[])
{
byte a = 56;
int i;
byte b;
i = a<< 2;
b = (byte) (a << 2);
System.out.println(i + " " + a +b);
}
}
17 Java Programming(E1UA402C)
Comparison of ++I and i++
⚫ class PrePostDemo {
⚫ public static void main(String[] args){ The increment/decrement operators can be
⚫ int i = 3; applied before (prefix) or after (postfix) the
operand. The code result++; and ++result; will
⚫ i++;
both end in result being incremented by one.
⚫ System.out.println(i); // prints 4 The only difference is that the prefix version (+
⚫ ++i; +result) evaluates to the incremented value,
⚫ whereas the postfix version (result++)
⚫ System.out.println(i); // prints 5 evaluates to the original value.
If you are just performing a simple
⚫
increment/decrement, it doesn't really matter
⚫ System.out.println(++i); // prints 6 which version you choose.
⚫ System.out.println(i++); // prints 6 But if you use this operator in part of a larger
⚫ System.out.println(i); // prints 7 expression, the one that you choose may make
a significant difference.
⚫ }
⚫ }
18 Java Programming(E1UA402C)
Java Reserved Keywords
19 Java Programming(E1UA402C)
Java Reserved Keywords
20 Java Programming(E1UA402C)
Java Reserved Keywords
Keyword Description
abstract A non-access modifier. Used for classes and methods: An abstract class
cannot be used to create objects (to access it, it must be inherited from
another class). An abstract method can only be used in an abstract class, and
it does not have a body. The body is provided by the subclass (inherited
from)
23 Java Programming(E1UA402C)
Java Reserved Keywords(static keyword)
public class Static1{
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating
objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating
objects");
}
// Main method
public static void main(String[ ] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would output an error
24 Java Programming(E1UA402C)
Static1 myObj = new Static1(); // Create an object of Static
Java Reserved Keywords(instanceof keyword)
25 Java Programming(E1UA402C)
Java Reserved Keywords(final keyword)
public class Final {
final int x = 10;
26 Java Programming(E1UA402C)
Java Type Casting
27 Java Programming(E1UA402C)
28 Java Programming(E1UA402C)
Java Type Casting
Type casting is when you assign a value of one primitive data type to
another type.
In Java, there are two types of casting:
•Widening Casting (automatically) - converting a smaller type to a
larger type size
byte -> short -> char -> int -> long -> float -> double
29 Java Programming(E1UA402C)
Java Type Casting
public class TypeCast1 {
public static void main(String[]
args) {
int myInt = 9;
double myDouble = myInt; //
Automatic casting: int to double
System.out.println(myInt); //
Outputs 9
System.out.println(myDouble); //
Outputs 9.0
}
}
30 Java Programming(E1UA402C)
Java Type Casting
public class TypeCast2{
public static void main(String[] args) {
double myDouble = 11.55d;
int myInt = (int) myDouble; // Manual
casting: double to int
System.out.println(myDouble); //
Outputs 9.78
System.out.println(myInt); //
Outputs 9
}
}
31 Java Programming(E1UA402C)
Branching Statements
⚫ 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");
⚫ } }}
32 Java Programming(E1UA402C)
The Continue Statements
⚫ 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;
33 Java Programming(E1UA402C)
return statement
⚫ The last of the branching statements is the return statement.
⚫ The return statement exits from the current method, and control flow returns to
where the method was invoked.
⚫ The return statement has two forms: one that returns a value, and one that doesn't.
To return a value, simply put the value (or an expression that calculates the value)
after the return keyword.
⚫ return ++count;
⚫ The data type of the returned value must match the type of the method's declared
return value. When a method is declared void, use the form of return that doesn't
return a value.
⚫ return;
34 Java Programming(E1UA402C)
⚫Methods in Java programming
Language
35 Java Programming(E1UA402C)
Method & Function in Java
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.
36 Java Programming(E1UA402C)
Functions are called independently. Methods are called using instance or object.
Method in Java
1.A method is a set of code which is referred to by name and can be
called (invoked) using method's name.
▰A method in Java is a collection of instructions that performs a
specific task. It provides the reusability of code.
▰We can also easily modify code using methods.
▰A method acts on data and often returns a value.
⚫The use of methods will be our first step in the direction of modular
programming.
37 Java Programming(E1UA402C)
Method in Java
⚫ A method is a block of code or collection of statements or a set of code grouped
together to perform a certain task or operation. It is used to achieve the reusability of
code.
⚫ We write a method once and use it many times.
⚫ It also provides the easy modification and readability of code, just by adding or
removing a chunk of code.
⚫ The method is executed only when we call or invoke it.
38 Java Programming(E1UA402C)
Method Definitions
⚫Method definitions have four basic parts:
39 Java Programming(E1UA402C)
Method Definition
40 Java Programming(E1UA402C)
Method Signature & Access Secifier
⚫ 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.
41 Java Programming(E1UA402C)
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.
42 Java Programming(E1UA402C)
Types of Method
⚫There are two types of methods in Java:
⚫Predefined Method
⚫User-defined Method
43 Java Programming(E1UA402C)
Predefined Method
⚫ In Java, predefined methods are the method that is already defined in the Java
class libraries is known as predefined methods.
⚫ It is also known as the standard library method or built-in method.
⚫ We can directly use these methods just by calling them in the program at any
point.
⚫ Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
⚫ Each and every predefined method is defined inside a class. Such as print()
method is defined in the java.io.PrintStream class. It prints the statement that
we write inside the method. For example, print("Java"), it prints Java on the
console.
44 Java Programming(E1UA402C)
Predefined method Program
⚫ // Java.lang.Math Class need not to import
46 Java Programming(E1UA402C)
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.
47 Java Programming(E1UA402C)
Method Call
48 Java Programming(E1UA402C)
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;
⚫ }}
49 Java Programming(E1UA402C)
Method Calling
⚫ In a method definition, you define what the method is to do.
⚫ To execute the method, you have to call or invoke it.
⚫ There are two ways in which a method is called depending on whether the method
returns a value or not.
50 Java Programming(E1UA402C)
Method Calling
51 Java Programming(E1UA402C)
class MethodsExample{Example
⚫ public static void main(String args[])
⚫ {
⚫ int a = 15;
⚫ int b = 8;
⚫ System.out.println("The largest number is " +max(a, b));
⚫ }
⚫ public static int max(int numA, int numB) {
⚫ int result;
⚫ if (numA > numB){
⚫ result = numA;
⚫ }
⚫ else{
⚫ result = numB;
⚫ }
⚫ return result; } }
52 Java Programming(E1UA402C)
Method returns nothing
⚫ 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);
⚫ }}
53 Java Programming(E1UA402C)
Void Method
⚫ public class VoidMethodExample {
⚫ public static void main(String[] args) {
⚫ System.out.print("You are ");
⚫ printAge(78);
⚫ System.out.print("You are ");
⚫ printAge(15);
⚫ }
⚫ public static void printAge(double age) {
⚫ if (age >= 90.0) { System.out.println("lagend Old"); }
⚫ else if (age >= 60.0) { System.out.println("old"); }
⚫ else if (age >= 40.0) { System.out.println("adult"); }
⚫ else if (age >= 20.0) { System.out.println("young"); }
⚫ else {
⚫ System.out.println("boy");
⚫ } }}
54 Java Programming(E1UA402C)
Passing Parameter by Value
⚫When you invoke a method with an argument, the value of the
argument is passed to the parameter.
55 Java Programming(E1UA402C)
Parameter Passing
⚫ public class SwappingExample {
⚫ public static void main(String[] args) {
⚫ int a = 3;
⚫ int b = 4;
⚫ System.out.println("Before swapping, a = " + a + " and b = " + b);
⚫ swapFunction(a, b); // Invoke the swap method
⚫ System.out.println("\n Now, Before and After swapping values will be same here :");
⚫ System.out.println("After swapping, a = " + a + " and b is " + b);
⚫ }
⚫ public static void swapFunction(int a, int b) {
⚫ System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
⚫ // Swap a with b
⚫ int c = a;
⚫ a = b;
⚫ b = c;
⚫ System.out.println("After swapping(Inside), a = " + a + " b = " + b);
⚫ }}
56 Java Programming(E1UA402C)
Parameter Passing
⚫ public class Mathmax
⚫ {
⚫ int a, b;
⚫ void sum1(int x, int y= 6) // The syntax of Java language doesn't allow you to //declare a method
with a predefined value for a parameter
⚫ {
⚫ a=x+y;
⚫ b=x-y;
⚫ System.out.println(a+" "+b);
⚫ return;
⚫ }
⚫ public static void main(String[] args)
⚫ {
⚫ // using the max() method of Math class
⚫ System.out.print("The maximum number is: " + Math.max(9,7));
⚫ Mathmax m=new Mathmax();
⚫ m.sum1(4,9); } }
57 Java Programming(E1UA402C)
References:
• https://www.geeksforgeeks.org/
• https://www.javatpoint.com/exception-handling-in-java
• https://www.tutorialspoint.com/java/java_exceptions.htm
• The complete reference, eleventh edition, available at:
https://gfgc.kar.nic.in/sirmv-science/GenericDocHandler/1
38-a2973dc6-c024-4d81-be6d-5c3344f232ce.pdf
58 Java Programming(E1UA402C)
Thank you
59 Java Programming(E1UA402C)