Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: variables,constants and operators in java
Description: class notes which can be understood by anyone. point to point and user friendly

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


VARIABLES
In Java, every variable has a type
...
Here are some examples:
double salary;

//declaring a single variable salary of type double

int vacationDays;
long earthPopulation;
boolean done;
int i,j;

//declaring multiple variables

Rules for Naming Variable
A variable name must begin with a letter and must be a sequence of letters or
Digits
Java is case sensitive
...

You also cannot use a Java reserved word(keyword) as a variable name
...


Initializing Variables
After you declare a variable, you must explicitly initialize it by means of an
assignment statement
For Example: If variable is not initialized you get the error as below
int vacationDays;
System
...
println(vacationDays); // ERROR--variable not initialized
Example :
int vacationDays; //declaring a variable called vacationDays
VacationDays = 12; // Initializing vacationDays to 12
You can both declare and initialize a variable on the same line
...


Types of Variables
There are three types of variables in Java
1
...
instance variable
3
...
You can use
this variable only within that method and the other methods in the class aren't even aware that the
variable exists
...

2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance variable
...

3) Static variable
A variable that is declared as static is called a static variable
...
You can create a single
copy of the static variable and share it among all the instances of the class
...


Constants
In Java, you use the keyword final to denote a constant
...
Once the value is assigned, it cannot be reassigned
...
14; //PI is real constant
int radius =10;
double area;
area= PI*radius*radius;

System
...
println("The Area of Circle is " + area);
}
It is probably more common in Java to create a constant so it’s available to multiple methods inside a
single class
...

Set up a class constant with the keywords static final
...
14;
public static void main(String args[])
{
int radius =10;
double area;
area= PI*radius*radius;
System
...
println("The Area of Circle is " + area);
}
}

Operators in Java
Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data
types
...
They are used to increment, decrement or
negate a value
...


+ :Unary plus, indicates positive value
++ :Increment operator, used for incrementing the value by 1
...

Post-Increment : Value is first used for computing the result and then incremented
...

-- : Decrement operator, used for decrementing the value by 1
...

Post-decrement : Value is first used for computing the result and then decremented
...

! : Logical not operator, used for inverting a boolean value
...

+=, for adding left operand with right operand and then assigning it to variable on the left
...

*=, for multiplying left operand with right operand and then assigning it to variable on the left
...

%=, for assigning modulo of left operand with right operand and then assigning it to variable on the left
...
They return
boolean result after the comparison
...

!=, Not Equal to : returns true if left hand side is not equal to right hand side
...

<=, less than or equal to : returns true if left hand side is less than or equal to right hand side
...

>=, Greater than or equal to: returns true if left hand side is greater than or equal to right hand side
...
&& are also called short circuit operators because they are
optimized
...

||, Logical OR : returns true if at least one condition is true
...
It has three operands and hence the name
ternary
...


Bitwise Operators
 The bitwise operators are &(bitwise AND), |(bitwise OR), ^(bitwise exclusive OR), <<(shift left)
and >>(shift right)
...

 Bitwise OR (|)
 This operator is a binary operator, denoted by ‘|’
...
e
...

 Bitwise AND (&)
 This operator is a binary operator, denoted by ‘&’
...
e, if
both bits are 1, it gives 1, else it gives 0
...

 For example : a<<2 means multiply a by 2^2(i
...
4) which yields 8
...

 For example b>>2 means divide b by 2^2(i
...
4) which yields 1
Title: variables,constants and operators in java
Description: class notes which can be understood by anyone. point to point and user friendly