Tuesday December 6, 2011 16:55

Lesson no.5 (Operators in J2me)

Posted by pradeep as J2ME (Java)

Hi all,
I hope you are going well. Today we will learn something about operators.
Well, You already learnt how to declare and initialize variables. Now we can do something with before learnt lesson by the help of operators.

What is operators?

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. There are many types of operators. We will learn every type of operators one by one. Operators performs additive, logical, multiplicative, unary etc inside the programs.
Here is the list of operators used in java. We will learn this all type of operators one by one.

1. The Simple Assignment Operator


2. The Arithmetic Operators


3. The Unary Operators


4. The Equality and Relational Operators


5. The Conditional Operators

We will learn every operators. Lets go on no1

1. The Simple Assignment Operator

One of the most common operators that you’ll use is the simple assignment operator “=”. You have alreay used this operator while assigning variables. Its just state a value for a variable. Like in variable example app you did:

int a=3;

It suppose that value of a is equal to the given number. You are familier with this common operator that we have been using.
Another is :

2. The Arithmetic Operators

Java language provides operators like addition, subtraction, multiplication, and division. You all are familier with this kind of operators that you have been using in mathematics. Below are the arithmetic operators:

+ additive operator (can be used to add two strings also)
- subtraction operator
* multiplication operator
/ division operator
% remainder operator

Additive operator is used to add th values. Even you can add two string with this operator. Similarly subtraction is used to subract one value by another. likewise multiplication and dvision. The only operator that you are not familier with is remainder operator. This is used to find out the remainder. Example:

result = number % 7;

// result is now 3. It gives remainder 3 after you divide 10 by 7.
Basic outputs:
int output = 1 + 2; // output is now 3
result = output – 1; // output is now 2
result = output * 2; // output is now 4
result = output / 2; // output is now 2
result = output + 8; // output is now 10
result =output % 7; // output is now 3

Well, There are more operators and quite unexperienced like above. So understand this and we will disscuss other operators too within the same topic. Just make up mind :) no need to get into it just understand process and develop logics.

New day something new topic new spirits new codes :)
Well, Today we will know something about unary operators.

Pradeep wrote:3. The Unary Operators

The unary operators require only one operand. We can perform various task only with 1 value. they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean. Unary operators are:

+ Unary plus operator; indicates positive value
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
– Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean

Now you can suppose a variable with proper value and perform various tasks from above listed operators.

+ Unary plus operator; indicates positive value

This operator defines that the value passed to the variable is positive. However if you donot assign sign before value then also it is ok because the default sign is positive but you should be careful in negative case.

- Unary minus operator; negates an expression

Similarly, This operator show that the value is negative.

++ Increment operator; increments a value by 1

It is increment operator. It increase the value of variable by 1. like if value of variable is 5 then it increase by 1 and makes 6.

– Decrement operator; decrements a value by 1

Similarly, It make 1 value decrement. like 5 and use of this operator make value 4.

! Logical complement operator; inverts the value of a boolean

It is logical operator. it just make value of boolean opposite. If you made boolean ram=true; then this operator help to make the value false.
Well, Below sample defines more itself:
int result = 1; // result is now 1

result–; // result is now 0
result ++ ; // result is now 1
result = -result; // result is now -1
boolean success = false;
(success); // false
(!success); // true

If you append success then you get boolean value that you previously supposed but if you append !success then it inverts the supposed value to opposite.
The increment and decrement operators can be used in another way too. for simple value you can do result++ ; or ++result; Similarly in decrement result ; or result.
I hope this will help you. Next operator we will learn acordingly :) Build concepts.

Hi again,
Hope you are doing well.
Now today we will be learning about:

4. The Equality and Relational Operators

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use “==”, not “=”, when testing if two primitive values are equal.

Tags: , , , , , , , , , , , , , ,

Comment Form

You must be logged in to post a comment.

My Songs

My Music Video

Join Me In Facebook

Recent Activity

Most Recent Visitors

Recommended URLS

www.lovingnepal.com www.npmig33.com www.sudanbhandari.com.np www.sandipkc.com.np www.sprakash.com.np www.madabishek.blogspot.com www.educatesansar.com

Recent Searches