operators anjavascriptd operands
TRANSCRIPT
-
8/12/2019 Operators anjavascriptd Operands
1/33
Operators and Operandsmental Java Operators
uction
ration is an action performed on one or more values either to modify the value held by
both of the values, or to produce a new value by combining existing values. Therefore, an
on is performed using at least one symbol and at least one value. The symbol used in an
on is called an operator. A value involved in an operation is called an operand.
operator is an operator that performs its operation on only one operand. An operator is
to as binary if it operates on two operands.
Colon;
i-colon is used to indicate the end of an expression, a declaration, or a statement. As we
n in other sections, there are other uses of the semi-colon.
Brackets { }
ackets are used to create a section of code. As such they are required to delimit the
of classes and other statement, such as conditional statements. Curly brackets are also
create variable scope. We will see various examples.
heses ( )
st computer languages, Java uses parentheses to isolate a group of items that must beed as belonging to one entity. For example, parentheses are used to differentiate asuch as mainfrom a regular variable. Here is an example:
eses can also be used to isolate an operation or an expression with regard to another
n or expression.
e Brackets [ ]
brackets are mostly used to control the dimension or index of an array. We will learn how
hem when we study arrays. Here is an example:
lass Exercise {
ic static void main(String[]args) {
mma ,
mma is used to separate variables used in a group. For example, a comma can be used to
Operators/Ope
C#
C++
Visual Basic
http://www.csharpkey.com/csharp/Lesson04.aspxhttp://www.csharpkey.com/csharp/Lesson04.aspxhttp://www.functionx.com/cpp/Lesson04.htmhttp://www.functionx.com/cpp/Lesson04.htmhttp://www.functionx.com/visualbasic/Lesson02.htmhttp://www.functionx.com/visualbasic/Lesson02.htmhttp://www.functionx.com/visualbasic/Lesson02.htmhttp://www.functionx.com/cpp/Lesson04.htmhttp://www.csharpkey.com/csharp/Lesson04.aspx -
8/12/2019 Operators anjavascriptd Operands
2/33
he names of variables that are declared with the same data type. Here is an example:
lass Exercise {
ic static void main(String[] args)
String firstName,lastName,fullName;
mma can also be used to separate the members the arguments of a method, as we will
hem.
tical Learning: Introduction Operators
rt the NetBeans IDE
the main menu, click File -> New Project...
the first page of the wizard, click General. In the Projects list, make sure that Java
plication is selected and click Next
the Project Location, accept the suggested path or type a new one, such asPrograms\JavaLessons.
the Project Name text box, type Operations1
ck Finish
m what we have learned so far, change the file as follows:
ckage operations1;
blic class Main {
public static void main(String[] args) {
String customerName, homePhone;
int numberOfShirts, numberOfPants, numberOfDresses;double priceOneShirt, priceAPairOfPants, priceOneDress;
int orderMonth, orderDay, OrderYear;
double mondayDiscount;
}
ve the file
signment =
ou declare a variable, a memory space is reserved for it. That memory space is filled with
. To "put" a value in the memory space allocated to a variable, you can use the
ent operator represented as =. Based on this, the assignment operation gives a value to a
Its formula is:
Name= Value
ableNamefactor must be a valid variable name. It cannot be a value such as a number or
e-quoted) string. Here is an example that assigns a numeric value to a variable:
lass Exercise {
ic static void main(String[] args) {
double salary;
-
8/12/2019 Operators anjavascriptd Operands
3/33
/ Using the assignment operator
alary = 12.55;
variable has been declared and assigned a value, you can call print() or println() to
ts value. Here is an example:
lass Exercise {
ic static void main(String[] args) {
double salary;
/ Using the assignment operator
alary = 12.55;
System.out.print(salary);
uld produce:
ve code declares a variable before assigning it a value. You will usually perform this
ent when you want to change the value held by a variable. Providing a starting value to a
when the variable is declared is referred to as initializing the variable. Here is an:
lass Exercise {
ic static void main(String[] args) {
// Initializing a variable
double salary = 12.55;
System.out.print(salary);
that you could declare various variables at once by using the same data type but
ng their names with commas. When doing this, you can also initialize each variable byg it the desired value before the comma or the semi-colon. Here is an example:
lass Exercise {
ic static void main(String[] args) {
/ Initializing various variables when declaring them with the same data type
ouble value1 = 224.58, value2 = 1548.26;
ystem.out.println(value1);
ystem.out.println(value2);
uld produce:
tical Learning: Assigning Values to Variables
-
8/12/2019 Operators anjavascriptd Operands
4/33
use the assignment operator, change the file as follows:
ckage operations1;
blic class Main {
public static void main(String[] args) {String customerName, homePhone;
int numberOfShirts= 5, numberOfPants= 2, numberOfDresses= 8;
double priceOneShirt= 0.95D, priceAPairOfPants= 2.95D,
priceOneDress= 3.25D;
int orderMonth = 3, orderDay = 15, orderYear = 2002;
double mondayDiscount = 0.25; // 25%
}
ve the file
Quote '
gle quote is used to include one character to initialize, or assign a symbol to, a variable as char. A single qoute is usually combined with another single-quote and a character
ncluded between them. Here is an example:
lass Exercise {
ic static void main(String[] args) {
char gender;
gender = 'M';
System.out.print(gender);
include only one character between single-quotes except if the combination of symbolsvaluated to one character. This is the case for escape sequences. Here is an example:
lass Exercise {
ic static void main(String[] args) {
char gender;
gender = 'M';
System.out.print(gender);
System.out.print('\n');
Quotes "
ble quote " is used to delimit a string. As mentioned for the single-quote, the double-
usually combined with another double quote. Between the combination of double-quotes,include an empty space, a character, a word, or a group of words, making it a string.
an example:
lass Exercise {
ic static void main(String[] args) {
tring message;
-
8/12/2019 Operators anjavascriptd Operands
5/33
essage = "Welcome to the wonderful world of Java";
ystem.out.print(message);
e-quoted string can also be declared and then assigned to a variable.
tical Learning: Using Quotes
use single and double-quotes, change the file as follows:
ckage operations1;
blic class Main {
public static void main(String[] args) {
String customerName = "James Burreck",
homePhone = " (202) 301-7030 begin_of_the_skype_highlighting
EE (202) 301-7030 end_of_the_skype_highlighting";int numberOfShirts = 5, numberOfPants = 2, numberOfDresses = 8;
double priceOneShirt = 0.95D, priceAPairOfPants = 2.95D,
priceOneDress = 3.25D;
int orderMonth = 3, orderDay = 15, orderYear = 2002;
double mondayDiscount = 0.25; // 25%
System.out.println("-/- Georgetown Cleaning Services -/-");
System.out.println("========================");
System.out.print("Customer: ");
System.out.println(customerName);
System.out.print("Home Phone: ");
System.out.println(homePhone);
System.out.print("Order Date: ");
System.out.print(orderMonth);System.out.print('/');
System.out.print(orderDay);
System.out.print('/');
System.out.println(orderYear);
System.out.println("------------------------");
System.out.println("Item Type Qty Sub-Total");
System.out.println("------------------------");
System.out.print("Shirts ");
System.out.print(numberOfShirts);
System.out.print(" ");
System.out.println(priceOneShirt);
System.out.print("Pants ");
System.out.print(numberOfPants);
System.out.print(" ");
System.out.println(priceAPairOfPants);
System.out.print("Dresses ");
System.out.print(numberOfDresses);
System.out.print(" ");
System.out.println(priceOneDress);
System.out.println("------------------------");
System.out.print("Monday Discount: ");
System.out.print(mondayDiscount);
-
8/12/2019 Operators anjavascriptd Operands
6/33
System.out.println('%');
System.out.println("========================");
System.out.println();
}
ecute the application. This would produce:- Georgetown Cleaning Services -/-
======================
stomer: James Burreck
me Phone: (202) 301-7030 begin_of_the_skype_highlighting FREE (202)
1-7030 end_of_the_skype_highlightingder Date: 3/15/2002
----------------------
em Type Qty Sub-Total
----------------------
irts 5 0.95
nts 2 2.95
esses 8 3.25----------------------
nday Discount: 0.25%
======================
sitive Operator +
uses a type of ruler to classify numbers. This ruler has a middle position of zero. The
s on the left side of the 0 are referred to as negative while the numbers on the right sidelers are considered positive:
-6 -5 -4 -3 -2 -1 1 2 3 4 5 6 +
0
-6 -5 -4 -3 -2 -1 1 2 3 4 5 6 +
on the right side of 0 is considered positive. To express that a number is positive, you can
+ sign on its left. Examples are +4, +228, +90335. In this case the + symbol is called a
perator because it acts on only one operand. The positive unary operator, when used,positioned on the left side of its operand, never on the right side.
thematical convention, when a value is positive, you don't need to express it with the +
r. Just writing the number without any symbol signifies that the number is positive.e, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228,
Because the value doesn't display a sign, it is referred as unsigned.
ess a variable as positive or unsigned, you can just type it. here is an example:
lass Exercise {
ic static void main(String[] args) {
ystem.out.print("Number = ");
ystem.out.println(+802);
uld produce:
-
8/12/2019 Operators anjavascriptd Operands
7/33
802
gative Operator -
can see on the above ruler, in order to express any number on the left side of 0, it must
ended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value
anied by - is referred to as negative. The - sign must be typed on the left side of theit is used to negate. Remember that if a number does not have a sign, it is considered
Therefore, whenever a number is negative, it MUST have a - sign. In the same way, if
nt to change a value from positive to negative, you can just add a - sign to its left.
an example that uses two variables. One has a positive value while the other has a
value:
lass Exercise {
ic static void main(String[] args) {
ystem.out.print("First Number = ");
ystem.out.println(+802);
/ Displaying a negative number
ystem.out.print("Second Number = ");ystem.out.println(-802);
uld produce:
mber = 802
umber = -802
, as many astill the same,
ng the + sign.
of two valuese2, you wouldadd more than
ns that Value1
a + c + b the
Computer Languages
C#
C++
Visual Basic
-
8/12/2019 Operators anjavascriptd Operands
8/33
n also get the
med as if you also add add
mple:
ing
8;
-
8/12/2019 Operators anjavascriptd Operands
9/33
)
uch numbers,
The simplestor the variablestrated in the
-
8/12/2019 Operators anjavascriptd Operands
10/33
is called the
you can writefollows:
dify the value
iler takes the
ue:
-
8/12/2019 Operators anjavascriptd Operands
11/33
t is modifyingd position the
On the other
t to increment
iable:
-
8/12/2019 Operators anjavascriptd Operands
12/33
eclare another
alue");
advantage is
e depends onm you will not
tly modify the
directly on thetever value a
-
8/12/2019 Operators anjavascriptd Operands
13/33
n combine the=. Here is an
alue");
second value.e the variable
dded to itself,
itself 4 times,
n it comes to
apply to the
-
8/12/2019 Operators anjavascriptd Operands
14/33
_highlighting
8;
;
OfDresses;
resses;
-/-");
-
8/12/2019 Operators anjavascriptd Operands
15/33
t to the same
ple:
-
8/12/2019 Operators anjavascriptd Operands
16/33
on assignment
value to the
t is essentially
the same as c
-
8/12/2019 Operators anjavascriptd Operands
17/33
n section, the
g
resses;
-
8/12/2019 Operators anjavascriptd Operands
18/33
ts);
;
1 from a value
a value. This
-
8/12/2019 Operators anjavascriptd Operands
19/33
is done using
ft or the rightor, the above
t the variables is illustrated
-
8/12/2019 Operators anjavascriptd Operands
20/33
erator on the
om a variable,
or. Here is an
alue");
-
8/12/2019 Operators anjavascriptd Operands
21/33
en you cut an
ng pieces, you
e in 4 parts.The division is
Make sure that
-
8/12/2019 Operators anjavascriptd Operands
22/33
OfDresses;
alDresses;
ts);
;
-
8/12/2019 Operators anjavascriptd Operands
23/33
e result to the
ple:
an example of
-
8/12/2019 Operators anjavascriptd Operands
24/33
u type an odd
get the valueotball (soccer)o start. If the
pressing Shift
n");
and assign the
.\n");
-
8/12/2019 Operators anjavascriptd Operands
25/33
und remainder
mple:
.\n");
ata in memory
presented onlyf changing the
not just abouturrent position
r is decimal or
ou will hardlyular basis. The
that we were
erse it to 0. If provides the
to binary is
-
8/12/2019 Operators anjavascriptd Operands
26/33
xample:
it. To support
to binary first.
6 converted to
Based on the
-
8/12/2019 Operators anjavascriptd Operands
27/33
on, use the &
variable. Here
or consists of
-
8/12/2019 Operators anjavascriptd Operands
28/33
tion, the Java
rted to binary
305 converted
101. Based on
-
8/12/2019 Operators anjavascriptd Operands
29/33
riable. Here is
wise exclusion,
-
8/12/2019 Operators anjavascriptd Operands
30/33
number 618
to binary iss follows:
variable itself.
-
8/12/2019 Operators anjavascriptd Operands
31/33
mple:
one ore more
ry equivalent.
e represented
eft, depending
bit to the leftosition y. You
s a value of 0.
ically perform
-
8/12/2019 Operators anjavascriptd Operands
32/33
xample:
itself. Here is
-
8/12/2019 Operators anjavascriptd Operands
33/33
evious Copyright 2008-2012, FunctionX
http://www.functionx.com/java/Lesson02.htmhttp://www.functionx.com/java/Lesson02.htmhttp://www.functionx.com/java/index.htmhttp://www.functionx.com/java/index.htmhttp://www.functionx.com/java/index.htmhttp://www.functionx.com/java/Lesson02.htm