0% found this document useful (0 votes)
11 views30 pages

Lecture 3 Operators in JAVA

Uploaded by

Khunsha farooq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
11 views30 pages

Lecture 3 Operators in JAVA

Uploaded by

Khunsha farooq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

JAVA Programming (CST015)

Lecture 3: Operators in JAVA

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
March 10, 2023
Identify Errors in the Given Program
class PracticeQuestions
{
public static void main(int args[])
{
char c=‘k’; //line 1
c=k; //line 2
char c1=“k”; //line 3
char c2=‘9’; //line 4
byte b=128; //line 5
long l=99999999999; //line 6
bool ready=False; //line 7
float f=2.3; //line 8
}
}
What will be the output?
int x=1_00_000_00_00;
System.out.println(x) //output?
Output will be a normal number excluding all the
underscores.
int x=1_00_000_00_00;
System.out.println(x)

Output will be a normal number excluding all the underscores.


This can be used for counting the number of zeros in the given literal.
What will be the output?
double f=54;
System.out.println(f) //output?
What will be the output?
double f=54;
System.out.println(f)

The program will compile successfully with no errors.


Output will be 54.0
The value 54 which is of int type will be converted to double
• In Java, everything goes in Class only. The source code (actual java code)
is compiled and converted to the bytecode (.class file).
• Running a Java program means to tell the JVM to load the “.class” file
and start executing the code in the main() method.
• Main method is where your program starts running.
• No matter how big your program is in term of number of classes, there
will be one main() method in them to execute the program.
• Inside the main() method, you write the code.
Whitespace doesn't matter in Java
int x = 3;
Is same as
int x=3;
Keywords in JAVA
• Keywords are the reserved words whose meaning is already defined in
the JAVA Compiler.
• We can`t use keywords for variable and other personal use.
Output?
Keywords are case-sensitive.
• This code will execute successfully with no errors because JAVA has no
keyword “CHAR”
Keywords in JAVA
Keywords in JAVA
• null, true, false are also keywords which can be used as literals only.
Operators in JAVA
• Operators are used to perform operations on variables and values.
int x = 100 + 50;
In the example above, we use the + operator to add together two values.
(Here, 100 and 50 are operand and + is the operator and 150 is the result)

Although the + operator is often used to add together two values, like in the
example above, it can also be used to add together a variable and a value, or a
variable and another variable:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum1; // 800 (400 + 400)
Types of Operators in Java

• Arithmetic Operators: [+, -, *, /, %, ++, --]


• Assignment Operators: [=, +=]
• Comparison Operators: [==, >=, <=]
• Logical Operators: [&&, ||, !]
• Bitwise Operators: [&, |] (operates bitwise)
Arithmetic Operators
➢Arithmetic operators are used to perform common mathematical
operations.
➢Arithmetic Operators: [+, -, *, /, %, ++, --]
++ increments value of a variable by 1
-- decrements value of a variable by 1

➢Arithmetic operators cannot work with Booleans.


➢% operator can work on floats and doubles.
% operator can work on floats and doubles.
System.out.println(12.5 % 3.5); //here both the operands are of
type Double and result will also be
in double as the default data-type of
fractional values are double in java.
System.out.println(12.5f % 3.5f) //here both the operands are of
type float as explicitly defined, and
the output of the operation will also
be float.
System.out.println(12.5f % 3.5d) //here one operand is float and other is
double, so output will be Double
(output will be of bigger datatype)
What will be the Output?
System.out.println(10 % 2.5);
// It is possible to perform arithmetic operations between compatible
datatypes.
//here 10 is of type int by default and 2.5 will be of type double.

Output will be 0 or 0.0 ?


What will be the Output?
System.out.println(1 * 2.5);
// It is possible to perform arithmetic operations between compatible
datatypes.
//here 1 is of type int by default and 2.5 will be of type double.

Output will be 2 or 2.5 ?


Output?
Output?
Can we do this? Output
No, Because increment operator works on variables and
not on constants
What will be the output?
char c=‘a’;
c++; // ++ is the increment operator
System.out.println(c);
Answer:
char c=‘a’;
c++; // ++ is the increment operator
System.out.println(c);

Output will be: b


Increment operator works on char datatypes also.
Increment Operators
int operand = 1;
++operand; // operand = 2
int number = ++operand; // operand = 3, number = 3
% works with decimal numbers also
Java Assignment Operators
Assignment operators are used to assign values to variables.
Assignment Operators: [ =, +=, -=, *=, /=, %= ]
int x = 20;

The addition assignment operator (+=) adds a value to a variable:


int x = 10;
x += 5; x=x+5 equivalent to x = x + 5
x *= 3 x=x*3 equivalent to x = x * 3
Comparison Operators
• Comparison operators are used to compare two values
• Comparison Operators: [==, >=, <=, >, <]
Questions
1. Do we have to put a main() method in every class we write?
2. Why does everything have to be in a class in JAVA?
3. Write a JAVA Program to calculate the area of a rectangle.
4. Write a Program in JAVA to print your names and enrollment nos.
5. Program to calculate the remainder of dividing two numbers and
print the output as "42 divided by 2 gives remainder 0"

You might also like