Java Chapter 3 - Variables & Operators
Java Chapter 3 - Variables & Operators
17UCTC31
Topic
Variables & Operators
K.A.Sheik Fareed
Assistant Professor of IT
Hajee Karutha Rowther Howdia College
Uthamapalayam-625533
Email Id: [email protected]
Blog Id : https//:sfnotesforit.blogspot.com
1
Variables
• What is a variable?
• The name of some location of memory used to hold a data value
• Different types of data require different amounts of memory. The compiler’s job is to
reserve sufficient memory
• Variables need to be declared once
• Variables are assigned values, and these values may be changed later
• Each variable has a type, and operations can only be performed between compatible
types
Example
int width = 3; 3 width
int height = 4; 4 height
int area = width * height; 12 area
width = 6;
area = width * height;
6 3 width
4 height
24 12 area
2
Variable Names
• Valid Variable Names: These rules apply to all Java names, or
identifiers, including methods and class names
• Starts with: a letter (a-z or A-Z), dollar sign ($), or underscore (_)
• Followed by: zero or more letters, dollar signs, underscores, or
digits (0-9).
• Uppercase and lowercase are different (total ≠ Total ≠ TOTAL)
• Cannot be any of the reserved names. These are special names
(keywords) reserved for the compiler. Examples:
class, float, int, if, then, else, do, public, private, void, …
3
Good Variable Names
• Choosing Good Names Not all valid variable names are good variable names
• Some guidelines:
• Do not use `$’ (it is reserved for special system names.)
• Avoid names that are identical other than differences in case (total, Total, and
TOTAL).
• Use meaningful names, but avoid excessive length
• crItm Too short
• theCurrentItemBeingProcessed Too long
• currentItem Just right
• Camel case capitalization style
• In Java we use camel case
• Variables and methods start with lower case
• dataList2 myFavoriteMartian showMeTheMoney
• Classes start with uppercase
• String JOptionPane MyFavoriteClass
4
Valid/Invalid Identifiers
Valid:
$$_
R2D2
INT okay. “int” is reserved, but case is different here
_dogma_95_
riteOnThru
SchultzieVonWienerschnitzelIII
Invalid:
30DayAbs starts with a digit
2 starts with a digit
pork&beans `&’ is illegal
private reserved name
C-3PO `-’ is illegal
5
Data Types and Variables
• Java Strongly-type language
• Strong Type Checking Java checks that all expressions involve compatible types
• int x, y; // x and y are integer variables
• double d; // d is a double variable
• String s; // s is a string variable
• boolean b; // b is a boolean variable
• char c; // c is a character variable
6
Operators
7
Assignment Operator (=)
lvalue = rvalue;
w = 10;
x = w;
z = (x - 2)/(2 + 2);
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
9
Simple Arithmetic
public class Example {
public static void main(String[] args) {
int j, k, p, q, r, s, t;
j = 5;
k = 2;
p = j + k;
q = j - k;
r = j * k;
s = j / k;
t = j % k;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
> java Example
} p = 7
q = 3
r = 10
s = 2
t = 1
>
10
Shorthand Operators
+=, -=, *=, /=, %=
Common Shorthand
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
a = a % b; a %= b;
11
Shorthand Operators
public class Example {
public static void main(String[] args) {
int j, p, q, r, s, t;
j = 5;
p = 1; q = 2; r = 3; s = 4; t = 5;
p += j;
q -= j;
r *= j;
s /= j;
t %= j;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
} > java Example
p = 6
q = -3
r = 15
s = 0
t = 0
> 12
Shorthand Increment and Decrement ++ and --
Common Shorthand
a = a + 1; a++; or ++a;
a = a - 1; a--; or --a;
13
Relational Operators
> < >= <= == !=
Primitives
• Greater Than >
• Less Than <
• Greater Than or Equal >=
• Less Than or Equal <=
14
Logical Operators (boolean)
&& || !
15
Logical (&&) Operator Examples
}
}
> java Example
f && f false
f && t false
t && f false
t && t true
>
16
Logical (||) Operator Examples
}
}
> java Example
f || f false
f || t true
t || f true
t || t true
>
17
Logical (!) Operator Examples
}
}
18
Shift Operators (Bit Level)
<< >> >>>
19