Monday, June 2, 2008
Java Variables are used to store data. Variables have type, name, and value. Variable names begin with a character, such as x, D, Y, z. Other examples are xy1, abc2, Count, N, sum, Sum, product etc. These are all variable names.
Different variable types are int, char, double. A variable type tells you what kind of data can be stored in that variable.
The syntax of assignment statements is easy. Assignment statements look like this:
variableName = expression ;
For example:
int x; // This means variable x can store numbers such as 2, 10, -5.
char y; // This means variable y can store single characters 'a', 'A'.
double z; // This means variable z can store real numbers such as
10.45, 3.13411.
The above are declarations for variables x, y and z.
Important points:
1. Note that a variable has to be declared before being used.
2. The values assigned to a variable correspond to its type. Statements below represent assignment of values to a variable.
x = 100; // x is an integer variable.
y = 'A'; // y is a character variable.
abc = 10.45; // abc is of type double (real numbers).
3. Both variable declaration and assignment of values can be done in same statement. For example,
int x;
x = 100; is same as int x = 100;
4. A variable is declared only once.
int x; // Declaration for x.
x = 100; // Initialization.
x = x + 12; // Using x in an assignment statement.
Often in a program you want to give a variable, a constant value. This can be done:
class ConstDemo
{
public static void main ( String[] arg )
{
final double PI = 3.14;
final double CONSTANT2 = 100;
. . . . . .
}
}
The reserved word final tells the compiler that the value will not change. The names of constants follow the same rules as the names for variables. (Programmers sometimes use all capital letters for constants; but that is a matter of personal style, not part of the language.)
2. Arithmetic Expressions
--------------------------
An assignment statement or expression changes the value that is held in a variable. Here is a program that uses an assignment statement:
class example
{
public static void main ( String[] args )
{
long x ; //a declaration without an initial value
x = 123; //an assignment statement
System.out.println("The variable x contains: " + x );
}
}
Java Arithmetic expressions use arithmetic operators such as +, -, /, *, and %. The % operator is the remainder or modulo operator. Arithmetic expressions are used to assign arithmetic values to variables. An expression is a combination of literals, operators, variables, and parentheses used to calculate a value.
The following code describes the use of different arithmetic expressions.
int x, y, z; // Three integer variables declared at the same time.
x = 10;
y = 12;
z = y / x; // z is assigned the value of y divided by x.
// Here z will have value 1.
z = x + y; // z is assigned the value of x+y // Here z will have value 22.
z = y % x // z is assigned the value of remainder when y // is divided by x. Here z will have value 2.
Java Boolean expressions are expressions which are either true or false. The different boolean operators are < (less than), > (greater than),
== (equal to), >= (greater or equal to), <= (less or equal), != (not equal to).
Example:
int x = 10;
int y = 4;
int z = 5;
(x < 10) // This expression checks if x is less than 10.
(y > 1) // This expression checks if y is greater than 1.
((x - y) == (z + 1)); // This expression checks
// if (x - y) equals (z + 1).
A boolean expression can also be a combination of other boolean expressions. Two or more boolean expressions can be connected using &&
(logical AND) and || (logical OR) operators.
The && operator represents logical AND. The expression is true only if both boolean expressions are true. The || operator represents logical
OR. This expression would be true if any one of the associated expressions is true.
Example:
int x = 10; int y = 4; int z = 5;
(x <= 10) && (y > 1) // This expression checks if x is less
// than 10 AND y is greater than 1.
// This expression is TRUE.
(x*y == 41) || (z == 5) // This expression checks if x*y is equal
// to 40 OR if z is equal to 5.
// This expression is FALSE
Different variable types are int, char, double. A variable type tells you what kind of data can be stored in that variable.
The syntax of assignment statements is easy. Assignment statements look like this:
variableName = expression ;
For example:
int x; // This means variable x can store numbers such as 2, 10, -5.
char y; // This means variable y can store single characters 'a', 'A'.
double z; // This means variable z can store real numbers such as
10.45, 3.13411.
The above are declarations for variables x, y and z.
Important points:
1. Note that a variable has to be declared before being used.
2. The values assigned to a variable correspond to its type. Statements below represent assignment of values to a variable.
x = 100; // x is an integer variable.
y = 'A'; // y is a character variable.
abc = 10.45; // abc is of type double (real numbers).
3. Both variable declaration and assignment of values can be done in same statement. For example,
int x;
x = 100; is same as int x = 100;
4. A variable is declared only once.
int x; // Declaration for x.
x = 100; // Initialization.
x = x + 12; // Using x in an assignment statement.
Often in a program you want to give a variable, a constant value. This can be done:
class ConstDemo
{
public static void main ( String[] arg )
{
final double PI = 3.14;
final double CONSTANT2 = 100;
. . . . . .
}
}
The reserved word final tells the compiler that the value will not change. The names of constants follow the same rules as the names for variables. (Programmers sometimes use all capital letters for constants; but that is a matter of personal style, not part of the language.)
2. Arithmetic Expressions
--------------------------
An assignment statement or expression changes the value that is held in a variable. Here is a program that uses an assignment statement:
class example
{
public static void main ( String[] args )
{
long x ; //a declaration without an initial value
x = 123; //an assignment statement
System.out.println("The variable x contains: " + x );
}
}
Java Arithmetic expressions use arithmetic operators such as +, -, /, *, and %. The % operator is the remainder or modulo operator. Arithmetic expressions are used to assign arithmetic values to variables. An expression is a combination of literals, operators, variables, and parentheses used to calculate a value.
The following code describes the use of different arithmetic expressions.
int x, y, z; // Three integer variables declared at the same time.
x = 10;
y = 12;
z = y / x; // z is assigned the value of y divided by x.
// Here z will have value 1.
z = x + y; // z is assigned the value of x+y // Here z will have value 22.
z = y % x // z is assigned the value of remainder when y // is divided by x. Here z will have value 2.
Java Boolean expressions are expressions which are either true or false. The different boolean operators are < (less than), > (greater than),
== (equal to), >= (greater or equal to), <= (less or equal), != (not equal to).
Example:
int x = 10;
int y = 4;
int z = 5;
(x < 10) // This expression checks if x is less than 10.
(y > 1) // This expression checks if y is greater than 1.
((x - y) == (z + 1)); // This expression checks
// if (x - y) equals (z + 1).
A boolean expression can also be a combination of other boolean expressions. Two or more boolean expressions can be connected using &&
(logical AND) and || (logical OR) operators.
The && operator represents logical AND. The expression is true only if both boolean expressions are true. The || operator represents logical
OR. This expression would be true if any one of the associated expressions is true.
Example:
int x = 10; int y = 4; int z = 5;
(x <= 10) && (y > 1) // This expression checks if x is less
// than 10 AND y is greater than 1.
// This expression is TRUE.
(x*y == 41) || (z == 5) // This expression checks if x*y is equal
// to 40 OR if z is equal to 5.
// This expression is FALSE
Subscribe to:
Post Comments (Atom)
SUBSCRIBE VIA eMAIL
Recent Posts
Archives
- December 2008 (5)
- November 2008 (15)
- October 2008 (17)
- September 2008 (9)
- August 2008 (12)
- July 2008 (19)
- June 2008 (22)
- May 2008 (17)
- April 2008 (2)
Categories
- java.sql (19)
- Examples (18)
- INTRODUCTION (9)
- JAVA APPLET (9)
- java.awt (8)
- java.net (8)
- java.beans (7)
- JAVA String Utility (6)
- Arrays (4)
- java.math (3)
- java.util.regex (3)
- Sort (2)
- Swing (2)
- java.security (2)
- java.util.zip (2)
- Catching Exceptions (1)
- Classes and Objects (1)
- Core Java Programs-part 1 (1)
- Core Java Programs-part 2 (1)
- Core Java Programs-part3 (1)
- Criticism of Java programming language (1)
- Falling Letters (1)
- File I/O and Streams (1)
- Fun With Letters and Words (1)
- Get current working directory (1)
- How do I convert String to Date object? (1)
- How do I convert string into InputStream? (1)
- How do i calculate directory size? (1)
- How to make executable jar files in JDK1.3.1? (1)
- Interfaces (1)
- JAVA Date Utility (1)
- Java Arithmetic Operators (1)
- Java Assignment Operators (1)
- Java Boolean Operators (1)
- Java Command Line Arguments (1)
- Java Comments (1)
- Java Conditional Operators (1)
- Java Data and Variables (1)
- Java Hello World Program (1)
- Java If-Else Statement (1)
- Java Increment and Decrement Operators (1)
- Java Loops (while (1)
- Java Relational Operators (1)
- Java Variables and Arithmetic Expressions (1)
- Java Virtual Machine (1)
- Know the current position of cursor (1)
- Letters (1)
- Methods (Includes Recursive Methods) (1)
- Rotating Lines (1)
- Send an email with attachment (1)
- Use for..each in Java (1)
- What is Autoboxing? (1)
- Write text file (1)
- connection to database (1)
- do-while and for loops) (1)


0 comments:
Post a Comment