0% found this document useful (0 votes)
66 views

Java Programs On Operators, Arithmetic Promotion, Method Calling

Java practical engineering 1st batu university

Uploaded by

ajinkyamusale32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Java Programs On Operators, Arithmetic Promotion, Method Calling

Java practical engineering 1st batu university

Uploaded by

ajinkyamusale32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical Assignment No.

1
Aim: Programs on Operators, Arithmetic Promotion, Method Calling.
Theory:

Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in Java which are given below:

o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.

Java Unary Operator


The Java unary operators require only one operand. Unary operators are used to perform various
operations i.e.:

o Incrementing/decrementing a value by one


o negating an expression
o inverting the value of a Boolean

Java Unary Operator Example: ++ and --


public class OperatorExample
{
public static void main(String args[])
{
Int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}
}

Output:
10
12
12
10
-------------------------------------------------------------------------------------------------------------

Java Arithmetic Operators


Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They
act as basic mathematical operations.

Java Arithmetic Operator Example

public class OperatorExample


{
public static void main(String args[])
{
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}
}

Output:

15
5
50
2
0

What is Arithmetic Promotion in Java?


The arithmetic promotion in Java happens when you apply an arithmetic operation on two variables with
different data-types. In this case the compiler will convert the data type of one operand in the binary
arithmetic operation to the type of the other operand.

-------------------------------------------------------------------------------------------------------------

How to Call a Method in Java


In Java, the method is a collection of statements that performs a specific task or operation.

It is widely used because it provides reusability of code means that write once and use it many times.

It also provides easy modification. Each method has its own name by which it is called.
When the compiler reads the method name, the method is called and performs the specified task.

In this section, we will learn how to call pre-defined, user-defined, static, and abstract methods in Java.

public class MethodCallExample


{
static void showMessage()
{
System.out.println("The static method invoked.");
}

void displayMessage()
{
System.out.println("Non-static method invoked.");
}

public static void main(String[] args)


{
sowMessage();
MethodCallExample me=new MethodCallExample();
me.displayMessage(); //called method
}
}

Output:

The static method invoked.


Non-static method invoked.

You might also like