Unit 2 Programming Basics (Tapashi Final) Edited
Unit 2 Programming Basics (Tapashi Final) Edited
UNIT STRUCTURE
2.2 INTRODUCTION
You have learnt how to write, save, compile and execute programs in Java
from the previous unit.
In this unit we will discuss the basics of Java programming language
which include tokens, variables, data types, constants etc. This might be a
review for learners who have learnt the languages like C/C++ earlier. We
extend this discussion by adding some new concepts associated with Java.
Different control flow statements like if, if-else, while, for, break, continue
etc. will also be covered in this unit.
HelloJava
Employee
ComplexNumber
l Variables that represent constant values use all upper-case letters
and underscore between word if required. For example,
PI
RATE
MAX_VALUE
l Literals : Literals in Java are a sequence of characters such as
digits, letters and other characters that represent constant values to
be stored in a variable.
l Operators : An operator is a symbol that takes one or more
arguments and opeates on them to produce a result. We will explain
various operators in section 2.7.
l Separators : Separators are symbols used to indicate where
groups of code are arranged and divided. Java separators are as
follows:
{} Braces
() Parentheses
[] Brackets
; Semicolon
, Comma
. Period
2.4 VARIABLES
which are already discussed in the previous section. Blank space is not
allowed in a variable name. Like other language C, C++ , the general syntax
of the variable declaration in Java looks like this:
type name ;
2.5 CONSTANTS
Every variable must have a data type. A data type determines the
values that the variable can contain and the operations that can be performed
on it. A variable’s type also determined how its value is stored in the
computer’s memory. The JAVA programming language has the following
categories of data types as shown in figure 2.1.
Data Types in
JAVA
Primitive Non-Primitive
(Intrinsic) (Derived)
int and long. It does not support unsigned types and therefore all
Java values are signed types. This means that they can be positive
or negative. For example, the int value 1996 is actually stored as
the bit pattern 00000000000000000000011111001100 as the binary
equivalent of 1996 is 11111001100. Similary, we must use a byte
type variable for storing a number like 20 instead of an int type. It is
because that smaller data types require less time for manipulation.
We can specify a long integer by putting an ‘L’ or ‘l’ after the number.
‘L’ is preferred, as it cannot be confused with the digit ‘1’.
l Boolean type : Boolean type can take only two values: true or
false. It is used when we want to test a particular condition during
the execution of the program. It is denoted by the keyword boolean
and it uses 1 byte of storage.
The memory size and range of all eight primitive data types are
given in the following table 2.2 :
In the table, each operator takes two operands, one on either side
of the operator. Integer division results in an integer. Because integers do
not have decimal fractions, any remainder is ignored. The expression 14/ 3,
for example, results in 3. The remainder 2 is ignored in this case. Modulus
(%) gives the remainder once the operands have been evenly divided. For
}
}
variableName = value;
value. In postfix form, they produce their operand’s original value, but
modify the operand in the background. For example, let us take the following
two expressions:
y = x++;
y = ++x;
These two expressions give very different results because of the
difference between prefix and postfix. When we use postfix operators (x++
or x--), y gets the value of x before before x is incremented; using prefix, the
value of x is assigned to y after the increment has occurred.
Relational Operators : Java has several expressions for testing
equality and magnitude. All of these expressions return a boolean value
(that is, true or false). Table 2.4 shows the relational operators:
operands are different (one true and one false, or vice versa) and false
otherwise (even if both are true).
For NOT, the ! symbol with a single expression argument is used.
The value of the NOT expression is the negation of the expression; if x is
true, !x is false.
Bitwise Operators : Bitwise operators are used to perform
operations on individual bits in integers. Table 2.5 summarizes the bitwise
operators available in the JAVA programming language. When both
operands are boolean, the bitwise AND operator (&) performs the same
operation as logical AND (&&). However, & always evaluates both of its
operands and returns true if both are true. Likewise, when the operands
are boolean, the bitwise OR (|) performs the same operation as is similar to
logical OR (||). The | operator always evaluates both of its operands and
returns true if at least one of its operands is true. When their operands are
numbers, & and | perform bitwise manipulations.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift
>>> Zero fill right shift
~ Bitwise complement
<<= Left shift assignment (x = x << y)
>>= Right shift assignment (x = x >> y)
>>>= Zero fill right shift assignment (x = x >>> y)
x&=y AND assignment (x = x & y)
x|=y OR assignment (x + x | y)
x^=y NOT assignment (x = x ^ y)
A shift operator performs bit manipulation on data by shifting the bits of its
first operand right or left. For example if op1 and op2 are two operands, then
the statement
Operator Description
?: Conditional operator (a ternary operator)
[] Used to declare arrays, to create arrays, and to
access array elements
. Used to form qualified names
(params ) Delimits a comma-separated list of parameters
(type ) Casts (converts) a value to the specified type
new Creates a new object or array
instanceof Determines whether its first operand is an instance
of its second operand
Java is rich in method and class for input/output from the key board and to
the monitor. The Scanner class can be used for inputting integer data as
wel as string data from the keyboard. Program 2:2 is used to input two
integer data from the keyboard and find the sum of the numbers.
//Program 2:2 : Read two numbers from the keyboard and find the sum
import java.util.Scanner;
class Add_number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the first Number :”);
int a=sc.nextInt();
System.out.println(“Enter the Second Number :”);
int b=sc.nextInt();
int c=a+b;
System.out.println(“The result of addition is :”+c);
}
}
Here, Scanner is a class, sc is an object, a,b and c are three integer variables
and nextInt() is a predefined method of Scaner class. The following program
is used to read a string from the keyboard and display the content of the
string.
//Program 2:3 : Read a string from the keyboard and display it.
import java.util.Scanner;
class String_read{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the Name :”);
String name=sc.next();
System.out.println(“Name :”+name);
}
}
42 Programming in Java (Block 1)
Programming Basic Unit 2
l In this unit we have discussed all the basic data types and operators
in Java and their use in expressions.
l Java variables are categorized into three groups according to their
scope: local variable , instance variable and class variable.
l In Java, constants are declared in the manner similar to variables
but with additional reserved word final.