Site hosted by Angelfire.com: Build your free website today!

Variables

 

Rules for creating variables

  1. A variable may consist of one or more than one letters, numerals and underscore.

  2. The first letter of the variable name shouldn’t be any numeral. It can be an alphabet or an underscore.

  3. Try not to use meaningful variable names.

  4. No reserved of Java should be used.

  5. No blank spaces are allowed in a variable.

  6. Java is case sensitive.

 


 

Data Types:

            A programmer, while programming has to deal with various types of data. Hence, it becomes necessary that the programming language facilitates the programmer with all data types. Java provides all these.

 

Integers:

            These are whole numbers suck as 2, 4, 6, -7, -9 etc. no fractional part is an integer. Integers are divided into four categories.

  1. Byte – is that integer which has a very small value ranging from -128 to 127 bytes, i.e.8 bits.

  2. Integer – are those positive or negative digits whose values vary from -2147483648 to 2147483648 If we have to use the value of 32770, we have to use an integer because the max of a short integer is 32767.

  3. Short Integer – are those integers which have a short range of value-128 to 127 or 215 to 215-1.

  4. Long Integers – are those that have a wide range of value ranging from -9223372036854775807 to 9223372036854775808 or -264 to 264-1.

Data Type

Size Bits

Range

  1. Byte

8 Bits

-128 to 127

  1. Integer

32 Bits

2147483648 to 2147483648

  1. Short Integer

16 Bits

215 to 215-1

  1. Long Integer

64 Bits

9223372036854775807 to 9223372036854775808

-264 to 264-1

 


 

Example of Valid Integers

      int no1; int no1=0

      int sum; int sum = 0

      int rollno; int rollno = 0

Initialization of Variables

      int no1 = 5

      int rollno = 34

 


 

 

Character Types

             Character types are 2 bytes in size but can hold single characters. A character constant varies from 0 to 255. The characters are stored in the computer memory as integers in their binary form. Each character is assigned a particular value according to ASCII codes.

Character Type

Size

Rank

  1. char

2byte

-256 to 255

 

Example of a Valid Character

char reply = y

char ans = a

 

            String is a series of characters. These characters may be alphabets, digits, blank spaces etc. the string is enclosed within double quotes. The string is used to write or store messages.

 

Example:

“Hello”

“Hello” is a string and the characters of a string are stored in sequential locations.

String Variables:

String name;

String vh1;

String Address;

 

Floating point:

            Float data type: a fractional number is a floating point number. Decimal in a number refers that it is a floating number. Floating points are divided into two parts:

  1. float

  2. double

 

Example:


 

float average;

float balance;

double gross salary;

double percentage;


 

 


 

Constants:

            Constants are fixed values that do not change during the execution of the program. Constants are also called literals. There are numeric and character constants. Numeric constants are divided into Integer constants float. Character constants are divided into character and string.

 


 

Operators

 

Arithmetical Operators:

            These are used for mathematical calculations. The result of an arithmetical expression is a numerical value. An arithmetical expression may be a variable or a constant or a combination of both. These variables or functions are connected with arithmetical operators.

 

Symbols

Meaning

Example

+

Addition

A+B

-

Subtraction

A-B

x or ·

Multiplication

A·B

/

Division

A/B

%

Modulus or Remainder

A%B

 

Relational Operators

            These are used to determine the relationship between different operands. These are used in the work of comparison also; these are two mathematical expressions and are connected by relational operators. The relational expression returns 0 if the relation is false and returns 1 if the relation is true. The six relational operators are shown in the following table:

 

Symbol

Meaning

>

Greater than

<

Less than

>=

Greater than equal to

<=

Less than equal to

==

Equal (two equal signs)

!=

Not equal to

 

Logical Operator

            Logical operators combine the result of two or more than two expressions. They are made of connecting relationships in the expression refers as logic and the expressions are called logical expressions. The logical operator returns 0 if the relation is false and 1 if its true.

 

Symbol

Meaning

!

Logical ‘negation’

||

Logical ‘or’

&&

Logical ‘and’

 

Increment & Decrement Operators

            Java provided ++ and – which are not provided my many other languages. These operators are called increment and decrement operators. The increment operator adds 1 to the operand and decrement operator subtracts 1 from the operand.

 

Let the operand variable be i

++i / i++ means i=i+1

--i / i-- means i=i-1

 

The operators exist in two types

  1. Prefix – before the operand.

  2. Postfix – after the operand.

 

Example:

Prefix increment operator: consider the following program segment

 

class sample

    {

        public void prefixprog()

        {

            int i=20;

            int y=++i*8;

            System.out.println ("Answer is: "+y);

        }

    }

 

Output:

Answer is: 168

 

 

Postfix increment operator: consider the following program segment

 

class sample

    {

        public void postfixprog()

        {

            int i=20;

            int y=i++*8;

            System.out.println ("Answer is: "+y);

        }

    }

 

Output:

Answer is: 160