Chapter 2 Basics of Java
Chapter 2 Basics of Java
1) $amount 5) score
2) 6tally 6) first Name
3) my*Name 7) total#
4) salary 8) this
V. Boolean Literals
7 automatically initialized
Preparedarrays and to define a
by: Melkamu D.
Contd.
III. Brackets [ ] :- are used to declare array types
and for dereferencing array values.
int x = 3;
int y = 5;
boolean result;
3) result = (x != x*y);
now result is assigned the value true because the
product of
x and y (15) is not equal to x (3)
13 Prepared by: Melkamu D.
C. Logical Operators
Symbol Name
&& Logical AND
|| Logical OR
! Logical NOT
(x || y) evaluates to true
(true && x) evaluates to true
count = count - 1;
can be written as:
--count; or count--;
int a=110;
float b=23.5;
(type-name)expression;
where type-name is one of the standard data
types.
Example :
a=(int)21.3/(int)3.5; a will be 7
23 Prepared by: Melkamu D.
Operator Precedence and Associativity.
Operator Description Associativity Rank
. Member Selection
() Function call
Left to right 1
[] Array elements
reference
- Unary Minus
++ Increment
-- Decrement
! Logical negation Right to left 2
~ One’s complement
(type) Casting
* Multiplication
Left to right 3
/ Division
% Modulus
24 Prepared by: Melkamu D.
Contd.
Operator Description Associativity Rank
+ Addition
Left to right 4
- Subtraction
<< Left Shift
Left to right 5
>> Right Shift
>>> Right shift with zero fill
< Less than
<= Less than or equal to
> Greater than Left to right 6
>= Greater than or equal
to
instanceof Type comparison
== Equality
Left to right 7
!= Inequality
25 Prepared by: Melkamu D.
Operator Precedence and Associativity.
Operato Description Associativity Rank
r
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
//Order of Evaluation
int answer1=a*b+c/++d;
int answer2=--a*(b+++c)/d++;
//Type Conversion
float answer3=a/c;
float answer4=(float)a/c;
float answer5=a/y;
27 Prepared by: Melkamu D.
//Modulo Operations
int answer6=a%c;
float answer7=x%y;
//Logical Operations
boolean bool1=a>b && c>d;
boolean bool2=a<b && c>d;
boolean bool3=a<b || c>d;
boolean bool4=!(a-b==c);
System.out.println("Order of Evaluation");
System.out.println("a*b+c/++d + "+answer1);
System.out.println("--a*(b+++c)/d++ = " +answer2);
System.out.println("================");
System.out.println("Type Conversion");
System.out.println(" a/c = "+answer3);
System.out.println("(float)a/c = " + answer4);
System.out.println(" a/y = " + answer5);
28 Prepared by: Melkamu D.
System.out.println("================");
System.out.println("Modulo Operations");
System.out.println(" a%c = "+answer6);
System.out.println(" x%y = "+answer7);
System.out.println("================");
System.out.println("Logical Operations");
System.out.println(" a>b && c>d = "+bool1);
System.out.println(" a<b && c>d = "+bool2);
System.out.println(" a<b || c>d = "+bool3);
System.out.println(" !(a-b==c) = "+bool4);
System.out.println("================");
System.out.println("Bitwise Operations");
Non-
Primitive
Primitive
(Intrinsic)
(Derived)
Interface
Floating- Charact s
Integer Boolean
Point er
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
3. The place of declaration (in the program) declares the scope of the variable.
Example:
int count, x,y; //Declaration
char
43 firstLetterOfName = 'e' Prepared
; //by:Declaration
Melkamu D. & initialization
Assigning Values to Variables
A variable must be given a value after it has been declared
Assignment Statement
A simple method of giving value to a variable is
through the assignment statement as follows:
variableName = value;
Example: x = 123, y = -34;
It is possible to assign a value to a variable at the
time of declaration as: type variableName =
value; Prepared by: Melkamu D. 44
Read Statement
It is also to assign value for variables interactively through
the keyboard using the readLine() method which belongs to
the DataInputStream class.
The readLine() method reads the input from the keyboard as
a string which is then converted into the corresponding data
type using the data type wrapper classes.
The wrapper classes are contained in the java.lang package.
Wrapper classes wrap a value of the primitive types into an
object.
The keywords try and catch are used to handle any errors
that might occur during the reading process.
45 Prepared by: Melkamu D.
// A program to read data from the Keyboard
import java.io.DataInputStream;
public class Reading{
public static void main(String[] args) {
DataInputStream in = new DataInputStream(System.in);
int intNumber=0;
float floatNumber=0.0f;
double doubleNumber=0.0;
String Name=null;
try{
System.out.println("Enter your Name: ");
Name=in.readLine();
System.out.println("Enter an Integer Number: ");
intNumber=Integer.parseInt(in.readLine());
System.out.println("Enter a float Number: ");
floatNumber=Float.parseFloat(in.readLine());
System.out.println("Enter a Double Number Number: ");
doubleNumber=Double.parseDouble(in.readLine());
}
catch(Exception e){}
System.out.println("Hello : "+Name);
System.out.println("The Integer Number is : "+intNumber);
System.out.println("The Float Number is : "+floatNumber);
System.out.println("The Double Number is :
"+doubleNumber);
}
}
46 Prepared by: Melkamu D.
Scope of Variables
1. Instance Variables: are declared in a class, but outside a
method, constructor or any block.
• are created when an object is created with the use of the key word 'new' and
destroyed when the object is destroyed.