Expressions, Statements and Displaying Data in Java (ICSE Computer Applications Notes)
Expressions, Statements and Displaying Data in Java
(ICSE Computer Applications Notes)
Expression:
Java is one of the most powerful and widely used programming languages.
While writing programs in Java, we frequently use expressions to
perform calculations and operations. An expression in Java is a combination
of variables, constants, operators, and method calls that
produces a single value when evaluated. In simple words, an
expression is a statement that calculates a value.
Components of a Java Expression:
1. Variables
Variables store values used in expressions.
Example:
int a = 5;
int b = 10;
int c = a + b;
Here a + b is an expression.
2. Constants
Constants are fixed values used in expressions.
Example
int result = 5 * 6;
Here 5 * 6 is
an expression.
3. Operators
Operators perform
operations on variables and constants.
Example:
Types of Expressions in Java
1. Arithmetic Expression
An arithmetic expression in Java is a
combination of variables, constants, and arithmetic
operators that performs a mathematical
calculation and produces a numeric result.
In simple words, an
arithmetic expression is used to perform mathematical operations in a
Java program.
Operators Used
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
Example
int a = 10;
int b = 5;
int result = a * b + 20;
Expression:
70
2. Relational Expression
A relational expression in Java is an
expression that compares two values using relational operators and
returns a boolean result (true or false).
In simple words, a
relational expression checks the relationship between two operands.
Example
int a = 15;
int b = 10;
boolean result = a > b;
Output: true
3. Logical Expression
A logical expression in Java is an expression
that combines two or more relational expressions using logical operators
and produces a boolean result (true or false).
In simple terms,
logical expressions are used to check multiple conditions at the same time.
Logical Operators
&& Logical
AND
|| Logical OR
! Logical NOT
Example
int age = 20;
boolean result = age > 18) && (age < 60);
Output: true
4. Assignment
Expression
An Assignment Expression in Java is an
expression where a value or result of another expression is assigned to a
variable using the assignment operator (=).
It stores the result of the expression on the right side into the
variable on the left side.
Example:
int x;
x = 50;
Here
x = 50
is an assignment expression.
5. Increment
and Decrement Expression
An Increment Expression increases the value
of a variable by 1 using the increment operator (++).
A Decrement Expression decreases the value
of a variable by 1 using the decrement operator (--).
Operators
++ Increment
-- Decrement
Example
int a = 10;
a++;
Value of a becomes 11.
Another example:
int b = 20;
b--;
Value of b becomes 19.
Example Program
Using Expression
public class
ExpressionExample
{
public static void main(String args[])
{
int a = 10;
int b = 5;
int sum = a + b;
int product = a * b;
boolean check = a > b;
System.out.println("Sum = " +
sum);
System.out.println("Product =
" + product);
System.out.println("Is a greater
than b? " + check);
}
}
Output:
Sum = 15
Product = 50
Is a greater than b? true
Statements in Java:
A Java statement is a complete command that
performs an action during program execution. Most Java statements end with a semicolon
( ; ).
In Java
programming, a statement is a single instruction that tells the
computer to perform a specific task. Statements are the basic building blocks
of a Java program. Every Java program is made up of one or more statements that
are executed by the Java Virtual Machine (JVM).
Example
int a = 10;
Explanation:
·
int → data type
·
a → variable
·
= 10 → value assigned
·
; → terminates the statement
Types of Statements
In Java, statements
are generally classified into the following types:
1. Assignment Statement
2. Expression Statement
3. Selection (Decision
Making) Statement
4. Iteration (Looping)
Statement
5. Jump Statement
Important Points About Java Statements
·
A Java statement usually ends with a semicolon ( ; ).
·
Statements are executed one by one in a program.
·
Statements help in controlling the flow of a program.
·
Different types of statements are used for calculation, decision
making, and looping.
Displaying Data in Java:
In Java
programming, displaying data means showing information or
output on the screen (console). This is an important concept for ICSE
Computer Applications students because every Java program needs to display
results, messages, or calculated values.
Methods Used to Display Data in Java
Java mainly uses the
following methods to display data:
1. print()
2. println()
1. print() Method
The print() method
displays the output but does not move the cursor to the next line.
Example:
System.out.print("Java
");
System.out.print("Programming");
Output
Java Programming
Both words appear on
the same line.
2. println() Method
The println()
method displays the output and moves the cursor to the next line.
Example
System.out.println("Java");
System.out.println("Programming");
Output
Java
Programming
Each statement
appears on a new line.
Important Points:
·
System is a predefined class in Java.
·
out is an object used to display output.
·
println() prints data and moves to the next line.
·
print() prints data on the same line.
·
+ operator is used to combine text and variables.

Comments
Post a Comment