0% found this document useful (0 votes)
2 views27 pages

Week 1 concept of Java

Introduction for a basic course of JAVA

Uploaded by

Nilofar Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views27 pages

Week 1 concept of Java

Introduction for a basic course of JAVA

Uploaded by

Nilofar Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Computer

Applications
Grade IX
1. Primitive Data types
2. Operators in Java
Quick Recap
Accepting Input using “Scanner” Class
1. The Scanner class is used to get user input.
2. It is found in java.util package.
3. To use Scanner class, create an object of the class and call the
methods appropriate to your requirement.
4. Example methods of Scanner class:
ScannerObj.next() -> returns a String
ScannerObj.nextInt() -> returns a int value
ScannerObj.nextFloat() -> returns a float value
ScannerObj.nextDouble() -> returns a double value
5. To read a single character we can use, ScannerObj.next().charAt(0)
Data types in Java

Data types

Primitive Non-Primitive
1. Independent 1. Dependent on basic datatypes
2. Pre-defined or built-in 2. User defined
E.g.: int, float, long, double, E.g.: Classes, Arrays, interface
char
Primitive data types

Primitive

Numeric Non-Numeric
Integers Characters
Floating numbers Booleans
Integer type
• A variable of integer type can store a whole number.
• Either positive or negative, but without a decimal point.
• It has four types as shown below:
Data Data type Bit Size Format
Byte byte 8 bits(1 byte) byte b = 5;
Short short 16 bits(2 bytes) short a = 12;
Integer int 32 bits(4 bytes) int c = 214;
Long Integer long 64 bits(8 bytes) long d = 45687;
Floating point
• To store a fractional number i.e. a number with decimal points.
• It has two types as shown below:

Data Data type Bit Size Format


Small range of float 32 bits(4 bytes) float m = 32.14;
decimal values
Wide range of double 64 bits(8 bytes) double n = 12.1269387
decimal values
Characters
• A character type variable contains a single character.
• The declaration of a character can be given as:
• Syntax: <data-type> <variable> = <‘character literal’>;
• For e.g.: char ch = ‘a’;
• A character literal is always enclosed within single quotes.

• Similarly, a set of characters(String) can be declared as,


• Syntax: <data-type> <variable> = <“String constant”>;
• For e.g.: String p = “Computer Applications”;
• A string is always enclosed within double quotes.
Boolean
• Has only two values true or false
For example: boolean ans = true;
Operators in Java
• An operator is basically a symbol or token, which performs
arithmetical or logical operations to give meaningful results.
• Arithmetical Expression: An arithmetical expression contains
variables, constants and arithmetical operators together to produce a
meaningful result.
• For Example: i) x + y ii) m – 15 iii) a*a+2*a*b+b*b
• Invalid Arithmetical expressions in java: a2+b2, 2ab

• Arithmetical Statement: When an arithmetical expression is assigned


to a variable then it is known as an “Arithmetical statement”.
• For example: i) m = x + y ii) p = m – 15 iii) n = a*a+2*a*b+b*b
Forms of operators
There are three forms of operators:
1) Unary operator 2) Binary operator 3) Ternary operator

Unary operator: An arithmetical operator which is applied to a single


operand.
e.g.: unary(+), unary(-), ++, --, etc
• Unary(+) and unary(-) are used to specify the sign of the operand. It is
used before the operand.
• Example: if a = 8 then +a will be +8 and –a will be -8
• if a = -10 then +a will be -10 and –a will be +10
Unary increment and decrement operators
• Unary increment operator(++) increases the value of an operand by
one.
• Unary decrement operator(--) decreases the value of an operand by
one.
• Example:
• 1) x = x + 1 can be written as x++ or ++x
• 2) p = p – 1 can be written as p-- or --p
Prefix operators
• When increment or decrement operators are applied before the
operand, it is known as prefix operators.
• It works on the principle, “CHANGE BEFORE ACTION”
• It means the value of the variable changes before the operation takes
place.
Postfix Increment
• When increment or decrement operators are applied after the
operand, it is known as postfix operators.
• It works on the principle, “CHANGE AFTER THE ACTION”
• It means the value of the variable changes after performing the
operation.
Questions:
• 1) if p = 5, find d = ++p + 5;
• 2) if a = 48, find a = a++ + ++a;
• 3) if c = 2, find d = ++c + c++ + 4;
• 4) if m = 12, find n = m++ * 5 + --m;
• 5) if a = 4, b = 3; find the value of c = a++ * 6 + ++b*5 + 10
Binary Operators
There are three different types of operators to perform mathematical
tasks:
• Arithmetical Operators: The operators which are used to perform
arithmetic calculations in a program.
• Addition(+)
• Subtraction(-)
• Multiplication(*)
• Division(/)
• Modulus(%)
• Relational Operators: These operators are used to show relationship
between the operands. Relational operators compare the values of
the variables and result in terms of ‘True’ or ‘False’
• Logical Operators: These operators yield true or false depending upon
the outcome of different expressions. The different types of logical
operators along with there format are shown below.
• Logical OR(||): Used to combine 2 or more conditional expressions. It will
result in true if any one or more expressions are true otherwise false if
none of the conditions is true.
e.g.: 5>4 || 8>12 -> true
3>7 || 5<=4 -> false
• Logical AND(&&): Used to combine 2 or more conditional expressions. It
will result in true if all the expressions are true otherwise false.
e.g.: 5>3 && 3<5 -> true
6==6 && 3>5 -> false
• Logical NOT(!): Used when we want to reverse the result of an expression.
It is a unary operator.
e.g. !(8>3): false
!(3<0): true
Precedence of logical operators is NOT(!), AND(&&) and OR(||)
!(c<b) || (a>b) && (b>c)
Order of operation: 1 3 2

• Say for example, a = 10, b= 5 and c= 2


!(c<b) || (a>b) && (b>c)

• 1) !(c<b) is evaluated: i.e. !(2<5) -> !(true) -> false


• 2) (a>b) && (b>c) is evaluated: i.e. (10>5) && (5>2) -> true
• 3) false || true is evaluated: -> true
Examples:
Hierarchy of operations

You might also like