Anda di halaman 1dari 33

Operators and Operands

Operators/Operands
C# C++ Visual Basic

ndamental Java Operators

roduction

operation is an action performed on one or more values either to modify the value held by e or both of the values, or to produce a new value by combining existing values. Therefore, an eration is performed using at least one symbol and at least one value. The symbol used in an eration is called an operator. A value involved in an operation is called an operand.

nary operator is an operator that performs its operation on only one operand. An operator is erred to as binary if it operates on two operands.

mi-Colon;
semi-colon is used to indicate the end of an expression, a declaration, or a statement. As we learn in other sections, there are other uses of the semi-colon.

ly Brackets { }

ly brackets are used to create a section of code. As such they are required to delimit the ies of classes and other statement, such as conditional statements. Curly brackets are also d to create variable scope. We will see various examples.

entheses ( )

e most computer languages, Java uses parentheses to isolate a group of items that must be sidered as belonging to one entity. For example, parentheses are used to differentiate a thod such as main from a regular variable. Here is an example:

()

entheses can also be used to isolate an operation or an expression with regard to another ration or expression.

are Brackets [ ]

uare brackets are mostly used to control the dimension or index of an array. We will learn how use them when we study arrays. Here is an example:

ic class Exercise { public static void main(String[] args) { }

Comma ,
comma is used to separate variables used in a group. For example, a comma can be used to

mit the names of variables that are declared with the same data type. Here is an example:

ic class Exercise { public static void main(String[] args) { String firstName, lastName, fullName; }

comma can also be used to separate the members the arguments of a method, as we will ew them.

ractical Learning: Introduction Operators


Start the NetBeans IDE On the main menu, click File -> New Project... In the first page of the wizard, click General. In the Projects list, make sure that Java Application is selected and click Next In the Project Location, accept the suggested path or type a new one, such as C:\Programs\JavaLessons. In the Project Name text box, type Operations1 Click Finish From what we have learned so far, change the file as follows: package operations1; public 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; } } Save the file

Assignment =

en you declare a variable, a memory space is reserved for it. That memory space is filled with bage. To "put" a value in the memory space allocated to a variable, you can use the gnment operator represented as =. Based on this, the assignment operation gives a value to a able. Its formula is:

ableName = Value

VariableName factor must be a valid variable name. It cannot be a value such as a number or double-quoted) string. Here is an example that assigns a numeric value to a variable:

ic class Exercise { public static void main(String[] args) { double salary;

// Using the assignment operator salary = 12.55;

ce a variable has been declared and assigned a value, you can call print() or println() to play its value. Here is an example:

ic class Exercise { public static void main(String[] args) { double salary; // Using the assignment operator salary = 12.55; System.out.print(salary);

s would produce:

above code declares a variable before assigning it a value. You will usually perform this gnment when you want to change the value held by a variable. Providing a starting value to a able when the variable is declared is referred to as initializing the variable. Here is an mple:

ic class Exercise { public static void main(String[] args) { // Initializing a variable double salary = 12.55; System.out.print(salary);

saw that you could declare various variables at once by using the same data type but arating their names with commas. When doing this, you can also initialize each variable by gning it the desired value before the comma or the semi-colon. Here is an example:

ic class Exercise { public static void main(String[] args) { // Initializing various variables when declaring them with the same data type double value1 = 224.58, value2 = 1548.26; System.out.println(value1); System.out.println(value2);

s would produce:

58 .26

ractical Learning: Assigning Values to Variables

To use the assignment operator, change the file as follows: package operations1; public 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% } } Save the file

gle-Quote '

single quote is used to include one character to initialize, or assign a symbol to, a variable lared as char. A single qoute is usually combined with another single-quote and a character be included between them. Here is an example:

ic class Exercise { public static void main(String[] args) { char gender; gender = 'M'; System.out.print(gender);

} can include only one character between single-quotes except if the combination of symbols be evaluated to one character. This is the case for escape sequences. Here is an example:

ic class Exercise { public static void main(String[] args) { char gender; gender = 'M'; System.out.print(gender); System.out.print('\n');

uble Quotes "

double quote " is used to delimit a string. As mentioned for the single-quote, the doublete is usually combined with another double quote. Between the combination of double-quotes, can include an empty space, a character, a word, or a group of words, making it a string. e is an example:

ic class Exercise { public static void main(String[] args) { String message;

message = "Welcome to the wonderful world of Java"; System.out.print(message);

ouble-quoted string can also be declared and then assigned to a variable.

ractical Learning: Using Quotes


To use single and double-quotes, change the file as follows: package operations1; public class Main { public static void main(String[] args) { String customerName = "James Burreck", homePhone = " (202) 301-7030 begin_of_the_skype_highlighting FREE (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);

System.out.println('%'); System.out.println("========================"); System.out.println(); } } Execute the application. This would produce: -/- Georgetown Cleaning Services -/======================== Customer: James Burreck Home Phone: (202) 301-7030 begin_of_the_skype_highlighting 301-7030 end_of_the_skype_highlighting Order Date: 3/15/2002 -----------------------Item Type Qty Sub-Total -----------------------Shirts 5 0.95 Pants 2 2.95 Dresses 8 3.25 -----------------------Monday Discount: 0.25% ======================== FREE (202)

Positive Operator +

ebra uses a type of ruler to classify numbers. This ruler has a middle position of zero. The mbers on the left side of the 0 are referred to as negative while the numbers on the right side he rulers are considered positive: - - -6 -6 -5 -5 -4 -4 -3 -3 -2 -2 -1 0 -1 1 2 3 4 5 6 + 1 2 3 4 5 6 +

alue on the right side of 0 is considered positive. To express that a number is positive, you can e a + sign on its left. Examples are +4, +228, +90335. In this case the + symbol is called a ry operator because it acts on only one operand. The positive unary operator, when used, st be positioned on the left side of its operand, never on the right side.

a mathematical convention, when a value is positive, you don't need to express it with the + rator. Just writing the number without any symbol signifies that the number is positive. refore, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228, 35. Because the value doesn't display a sign, it is referred as unsigned.

express a variable as positive or unsigned, you can just type it. here is an example:

ic class Exercise { public static void main(String[] args) { System.out.print("Number = "); System.out.println(+802); }

s would produce:

er = 802

e Negative Operator -

you can see on the above ruler, in order to express any number on the left side of 0, it must appended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value ompanied by - is referred to as negative. The - sign must be typed on the left side of the mber it is used to negate. Remember that if a number does not have a sign, it is considered itive. Therefore, whenever a number is negative, it MUST have a - sign. In the same way, if want to change a value from positive to negative, you can just add a - sign to its left.

e is an example that uses two variables. One has a positive value while the other has a ative value:

ic class Exercise { public static void main(String[] args) { System.out.print("First Number = "); System.out.println(+802); // Displaying a negative number System.out.print("Second Number = "); System.out.println(-802); }

s would produce:

t Number = 802 nd Number = -802

Computer Languages

other, as many as C# t is still the same, C++ s using the + sign. Visual Basic

ition of two values <SCRIPT language="JavaScript1.1" SRC="http://bid.g.double Value2, you would smidnHzwoBkmvU_HbuYX_YLHrr5TVcnfmiGnUbZtHxQSYocA7vWAJBhR8R_N978bwbiT7HgtT706u0sJvs also add more than aaW2FXbsTtrC9rt608eeEkKOs8e6kCvp4LApnbQqCY36WFUwg1-QUvDSqEjJeGK0uPHV6z1CAT3eOdMR4 GcjV76dxNCrOq331U7A39b6Tm-NWccHqf2PfO0fa7x1G-2RAa-ffIyo5GO2x6 HREF="http://bid.g.doubleclick.net/xbbe/cre means that Value1 smidnHzwoBkmvU_HbuYX_YLHrr5TVcnfmiGnUbZtHxQSYocA7vWAJBhR8R_N978bwbiT7HgtT706u0sJvs e as a + c + b the aaW2FXbsTtrC9rt608eeEkKOs8e6kCvp4LApnbQqCY36WFUwg1-QUvDSqEjJeGK0uPHV6z1CAT3eOdMR4 GcjV76dxNCrOq331U7A39b6Tm-NWccHqf2PfO0fa7x1G-2RAa-ffIyo5GO2x6WQ7kLOjVKDM"> smidnHzwoBkmvU_HbuYX_YLHrr5TVcnfmiGnUbZtHxQSYocA7vWAJBhR8R_N978bwbiT7HgtT706u0sJvs aaW2FXbsTtrC9rt608eeEkKOs8e6kCvp4LApnbQqCY36WFUwg1-QUvDSqEjJeGK0uPHV6z1CAT3eOdMR4 GcjV76dxNCrOq331U7A39b6Tm-NWccHqf2PfO0fa7x1G-2RAa-ffIyo5GO2x6WQ7kLOjVKDM&pr=0.04"

ou can also get the

erformed as if you u can also add add example:

hlighting

es = 8;

");

(202)

ing such numbers, ange. The simplest alue or the variable illustrated in the

ator is called the + 1, you can write n as follows:

o modify the value ompiler takes the d value:

able it is modifying should position the

alled. On the other want to increment e variable:

to declare another

a value");

. The advantage is riable depends on ogram you will not anently modify the ion directly on the whatever value a

u can combine the s +=. Here is an

a value");

by a second value. since the variable is added to itself, a to itself 4 times,

When it comes to lso apply to the

kype_highlighting

es = 8;

sses;

02;

mberOfDresses;

talDresses;

ces -/-"); ");

"); ); ");

");

");

202)

esult to the same xample:

ication assignment sired value to the

ue. It is essentially

not the same as c

action section, the

ghting

talDresses;

");

");

"); ); "); "); rts); "); lPants); "); sses); "); ems);

0);

");

202)

ing 1 from a value ting a value. This

This is done using he left or the right erator, the above

ement the variable . This is illustrated

he operator on the

ue from a variable, erator. Here is an

a value");

e, when you cut an sulting pieces, you apple in 4 parts. her. The division is

0). Make sure that

mberOfDresses;

bTotalDresses;

"); ");

"); ); "); "); rts); "); lPants); "); sses); "); ems);

0);

");

n the result to the xample:

e is an example of

if you type an odd nt to get the value a football (soccer) am to start. If the

rom pressing Shift

ts.\n");

able and assign the

tarts.\n");

mpound remainder example:

tarts.\n");

ts data in memory e represented only sts of changing the t is not just about its current position

mber is decimal or

w. You will hardly regular basis. The much that we were OR.

n reverse it to 0. If uage provides the

erted to binary is

an example:

her bit. To support

rted to binary first.

r 286 converted to 1011. Based on the

unction, use the &

ame variable. Here

perator consists of

operation, the Java

onverted to binary

ber 305 converted 0100101. Based on

it0

e variable. Here is

mple:

bitwise exclusion,

imal number 618 ted to binary is ers as follows:

Bit0 1 0 0

the variable itself.

example:

s by one ore more binary equivalent. <<.

an be represented

the left, depending each bit to the left at position y. You eives a value of 0.

t0

mmatically perform

an example:

able itself. Here is

the <<= operator.

hift but in reverse

Previous

Copyright 2008-2012, FunctionX

Nex

Anda mungkin juga menyukai