0% found this document useful (0 votes)
6 views19 pages

Java Chapter 3 - Variables & Operators

The document provides an overview of variables and operators in Java programming, detailing the definition, types, and naming conventions for variables. It also explains various operators including assignment, arithmetic, relational, logical, and shift operators, along with examples of their usage. The content is aimed at helping learners understand how to effectively use variables and operators in Java code.

Uploaded by

Sheik Fareed
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)
6 views19 pages

Java Chapter 3 - Variables & Operators

The document provides an overview of variables and operators in Java programming, detailing the definition, types, and naming conventions for variables. It also explains various operators including assignment, arithmetic, relational, logical, and shift operators, along with examples of their usage. The content is aimed at helping learners understand how to effectively use variables and operators in Java code.

Uploaded by

Sheik Fareed
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/ 19

Java Programming

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

• x = 7; // legal (assigns the value 7 to x)


• b = true; // legal (assigns the value true to b)
• c = ‘#’; // legal (assigns character # to c)
• s = “cat” + “bert”; // legal (assigns the value “catbert” to s)
• d = x – 3; // legal (assigns the integer value 7 – 3 = 4 to double d)

• b = 5; // illegal! (cannot assign int to boolean)


• y = x + b; // illegal! (cannot add int and boolean)
• c = x; // illegal! (cannot assign int to char)

6
Operators

7
Assignment Operator (=)
lvalue = rvalue;
w = 10;
x = w;
z = (x - 2)/(2 + 2);

• Take the value of the rvalue and store


it in the lvalue.
• The rvalue is any constant, variable or
expression.
• The lvalue is named variable.
8
Arithmatic Operators

 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 <=

Primitives or Object References


• Equal (Equivalent) ==
• Not Equal !=

The Result is Always true or false

14
Logical Operators (boolean)
&& || !

• Logical AND &&


• Logical OR ||
• Logical NOT !

15
Logical (&&) Operator Examples

public class Example {


public static void main(String[] args) {
boolean t = true;
boolean f = false;

System.out.println("f && f " + (f && f));


System.out.println("f && t " + (f && t));
System.out.println("t && f " + (t && f));
System.out.println("t && t " + (t && t));

}
}
> java Example
f && f false
f && t false
t && f false
t && t true
>

16
Logical (||) Operator Examples

public class Example {


public static void main(String[] args) {
boolean t = true;
boolean f = false;

System.out.println("f || f " + (f || f));


System.out.println("f || t " + (f || t));
System.out.println("t || f " + (t || f));
System.out.println("t || t " + (t || t));

}
}
> java Example
f || f false
f || t true
t || f true
t || t true
>

17
Logical (!) Operator Examples

public class Example {


public static void main(String[] args) {
boolean t = true;
boolean f = false;

System.out.println("!f " + !f);


System.out.println("!t " + !t);

}
}

> java Example


!f true
!t false
>

18
Shift Operators (Bit Level)
<< >> >>>

• Shift Left << Fill with Zeros

• Shift Right >> Based on Sign

• Shift Right >>> Fill with Zeros

19

You might also like