Tuesday, August 4, 2009

Operator

In Java, there are several types of service. There are arithmetic operators, customer service, service logic, and service conditions. Operator follows the various priorities that so compilernya akan know which service to run in the first some cases the service is shared in a statement.

Operator arithmetic
The following are the basic arithmetic operators that can be used to create a
Java programs:

+ : op1 + op2 Adding op2 with Op1
* : op1 * op2 op1 to op2 multiplying
/ : op1 / op2 Sharing op1 with op2
% : op1 % op2 Calculating the rest of the division of op1 with op2
- : op1 - op2 reduce op2 from op1

Here is an example program in the use of this operator-operator:

public class aritmatikaDemo
{
public static void main(String[] args)
{
//little number
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y); //Answer number
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//reduction in the number
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//digit multiplication
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//division number
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
//calculate the modulus results from the division of
System.out.println("Computing the remainder...");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));
//type of merger
System.out.println("Mixing tipes...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));
}
}

Below is the output of the program,

Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
i + j = 79
Adding...
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i / j = 0
x / y = 3.8054
Computing the remainder...
i % j = 37
x % y = 5.815
Mixing tipes...
j + y = 49.22
i * x = 1016.58


Operator Increment and Decrement

From the basic arithmetic operators, also consists of the Java unary increment operator (+ +)
and unary decrement operator (--). increment and decrement operators increase and reduce the value stored in the form of a variable number of value 1. For example, the statement,

count = count + 1; //count value increment with a value of 1

with the same statement,

count++;


OP++ :Adding value in the op 1; evaluate the value of op before diincrementasi / added
++OP :Adding value in the op 1; evaluate the value of op before diincrementasi / added
OP-- :Reduce the value 1 on the op; evaluate the value of op before diincrementasi / added
--OP :Reduce the value 1 on the op; evaluate the value of op before diincrementasi / added

Increment and decrement operators can be placed before or after the operand.

When used before the operand, will result in a variable or diincrement didecrement with a value of 1, and then a new value is used in the statement where he added. For example,

int i = 10,
int j = 3;
int k = 0;
k = ++j + i; //yield k = 4+10 = 14


When the increment and decrement operators are placed after the operand, the value of the variable old will be used first operated prior to the statement which he added. For example,

int i = 10,
int j = 3;
int k = 0;
k = j++ + i; //yield k = 3+10 = 13



Relations Operator

Relations operator to compare two values and determine the relationship between values it. Of the output is a boolean value that is true or false.

" > Op1> op2 op1 is greater than op2"
" > = Op1> = op2 op1 is greater than or equal to op2"
" < Op1 <= Op1 <= op2 op1 is less than or equal to op2 "
" == Op1 == op2 op1 together with op2 "
" ! = Op1! = Op2 op1 is not equal to op2 "

Here is an example program that uses the service relations,

public class RelasiDemo { public static void main(String[] args)

{ //some value int i = 37; int j = 42; int k = 42;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" k = " + k); //greater than System.out.println("Greater than..."); System.out.println(" i > j = " + (i > j)); //false
System.out.println(" j > i = " + (j > i)); //true
System.out.println(" k > j = " + (k > j)); //false
//greater or equal to
System.out.println("Greater or equal to...");
System.out.println(" i >= j = " + (i >= j)); //false
System.out.println(" j >= i = " + (j >= i)); //true
System.out.println(" k >= j = " + (k >= j)); //true
//less than
System.out.println("Less than...");
System.out.println(" i < j =" ">
System.out.println(" j < i =" ">
System.out.println(" k < j =" ">
//Less than or equal to
System.out.println("Less than or equal to...");
System.out.println(" i <= j = " + (i <= j)); //true
System.out.println(" j <= i = " + (j <= i)); //false
System.out.println(" k <= j = " + (k <= j)); //true //with the same
System.out.println("with the same...");
System.out.println(" i == j = " + (i == j)); //false
System.out.println(" k == j = " + (k == j)); //true //not the same as
System.out.println("Not the same as...");
System.out.println(" i != j = " + (i != j)); //true
System.out.println(" k != j = " + (k != j)); //false
}
}



Here is the result of the output from this program:

Variable values...
i = 37
j = 42
k = 42
Greater than...
i > j = false
j > i = true
k > j = false
Greater or equal to...
i >= j = false
j >= i = true
k >= j = true
Less than...
i < j =" true">
j < i =" false">
k < j =" false">
Less than or equal to...
i <= j = true
j <= i = false
k <= j = true with the same...
i == j = false
k == j = true
Not the same as...
i != j = true
k != j = false

Sunday, August 2, 2009

Variable

Variables are data items that are used to store the object statement. Variable has a data type and name. Type indicates the data type of value that can be formed by the variable itself. Variable name must follow the rules of the identifier.


Variable declaration and initialisation


For the variable declaration is as follows,
[= initial value];
Note: The value is between <> is a required value, while values in
marked [] are optional.
Here is an example program and declare that the several
variables,
public class VariableSamples { public static void main (String [] args) { / / data type declarations with the variable name / / Result and data type boolean boolean result; / / data type declarations with the variable name / / Option data type and char char option; option = 'C'; / / mark 'C' as an option / / data type declarations with the variable name / / grade, double data type and has been in the initialisation / / to 0.0 double grade = 0.0; } }


Displaying Variable Data
To remove the value of a variable that you want, we can use the command as follows,

System.out.println()
System.out.print()

Here is an example program,

public class OutputVariable { public static void main( String[] args ){ int value = 10; char x; x = ‘A’; System.out.println( value ); System.out.println( “The value of x=” + x ); } }

The program will remove the following text on the screen,

10 The value of x=A

System.out.println() vs. System.out.print()
What distinguishes between the System.out.println () and System.out.print ()?
The first to add a new line at the end of the data for the excluded, while
not next.
Note the statement,

System.out.print("Hello "); System.out.print("world!");
The statement will produce the following output on the screen,

Hello world!

Now see the following statement,

System.out.println("Hello "); System.out.printin("world!");
The statement will produce the following output on the screen,

Hello
world!


Reference Variables and Primitive Variables
Now we will distinguish two types of variables that are owned by the Java program. There reference variables and primitive variables.
Primitive variables are variables with primitive data types. They store data in the actual memory location where the variable is located.
Reference variables are variables that store addresses in a memory location. The point to a memory location where data is actually located. When you declare
variables in a particular class, you actually declare a variable in the reference classnya in the form of the object is.
For example, if we have two variables with data types int and String.
int num = 10; String name = "Hello"

eg the illustration shown below is the memory of the computer
You, where you have the address of each memory cell, the variable name and data
form as follows.

As you can see, the primitive variable for num, data in the location where the variables are. To reference a variable name, variable only refers to the address where the data is really there.