0% found this document useful (0 votes)
21 views16 pages

3 Structure

Uploaded by

viswadevadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

3 Structure

Uploaded by

viswadevadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Structure of a java program

A java program may contain one or more sections.

Documentation Section
Package Statement
Import Statement
Interface Statement
Class Defnition
Main method class
{
Main method defntion
}

Documentation Section:

It comprises of set of comment lines .

In java 3 types of comments are there.

1. Single line comment: // this is single line comment


2. Multi line comment :/* this is
Multi line comment */
3. Documentation comment : /** this is documentation comment*/ used for
generating documentation automatically.

Package Statement: this statement declares package name and informs the
compiler that the classes defined here belongs to this package.

Ex . package student;

Package statement is optional.

Import Statement : this is used to import a class or all the classes in the package.

Ex: import student.test;


Interface Statement : An interface is like class but includes a group of method
declarations.

Class Definitions: java program may contain multiple class definitions. These
classes are made up of instance variables and methods .The number of classes
used depend on the complexity of the program.

Main method class: java program execution always start from main method. The
main method creates objects of various classes and establishes communication
between them

…………………………………………….
Java Tokens
Smallest individual units in a program are known as tokens. A java program is a collection of
tokens, comments and white spaces.Java language includes five types of tokens. They are

 Keywords
 Identifiers
 Literals
 Operators
 Separators

Java character Set:

Smallest units of java language are the characters used to write java tokens.
These characters are defined by the Unicode

Keywords:
Java keywords are also known as reserved words. These are predefined words by Java
so it cannot be used as a variable or object name.

abstract continue for New switch


assert*** default goto* package synchronized
boolean do if private this
Break double implements protected throw
Byte else import public throws
Case enum instanceof return transient
Catch extends int short try
Char final interface static void
Class finally long strictfp volatile
const* float native super while

Identifiers:
Identifiers are the names of classes, methods, variables, objects, labels, packages and
interfaces in a program. Ex int marks=78;

Rules for java identifier:

1. They can be alphabets, digits, the underscore and dollar sign.


2. They must not contain any white spaces.
3. Identifiers are case sensitive.
4. They should not start with special characters like & (ampersand), $ (dollar), _
(underscore).
5. Upper case and lower case letters are distinct.
6. They can be of any length.
7. Keywords should not be used as identifier.
--------------------------------------------------------------------------------------------
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
primitive data types: The non-primitive data types include Classes, Interfaces,
2. Non-primitive
and Arrays.

Primitive Data Types

There are 8 types of primitive data types:

o boolean data type


o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
byte

 Byte data type is an 8-bit signed integer


 Minimum value is -128
 Maximum value is 127
 Default value is 0
 Example: byte a = 100, byte b = -50

short

 Short data type is a 16-bit signed integer


 Minimum value is -32,768
 Maximum value is 32,767
 Short data type can also be used to save memory as byte data type.
 Default value is 0.
 Example: short s = 10000, short r = -20000

int

 Int data type is a 32-bit signed integer.


 Minimum value is - 2,147,483,648
 Maximum value is 2,147,483,647
 Integer is generally used as the default data type for integral values unless there is a
concern about memory.
 The default value is 0
 Example: int a = 100000, int b = -200000

long

 Long data type is a 64-bit signed integer


 Minimum value is -9,223,372,036,854,775,808
 Maximum value is 9,223,372,036,854,775,807
 This type is used when a wider range than int is needed
 Default value is 0L
 Example: long a = 100000L, long b = -200000L
float

 Float data type is a single-precision floating point


 Float is mainly used to save memory in large arrays of floating point numbers
 Default value is 0.0f
 Example: float f1 = 234.5f

double

 double data type is a double-precision 64-bit floating point


 This data type is generally used as the default data type for decimal values,
generally the default choice
 Default value is 0.0d
 Example: double d1 = 123.4

boolean

 boolean data type represents one bit of information


 There are only two possible values: true and false
 This data type is used for simple flags that track true/false conditions
 Default value is false
 Example: boolean one = true

char

 char data type is a single 16-bit Unicode character


 Minimum value is '\u0000' (or 0)
 Maximum value is '\uffff' (or 65,535 inclusive)
 Char data type is used to store any character
 Example: char letterA = 'A'

Literals
A constant value which can be assign to the variable is called Literals.
Ex : int x=10;
Integer Literals:
Integral data types ( byte, short, int, long), we can specify literals in 4 ways.
1. Decimal literals (Base 10) : In this form the allowed digits are 0-9.
int x=101;
2. Octal literals (Base 8) : In this form the allowed digits are 0-7.
Octal numbers should be prefix with 0;
int x= 0146;

3. Hexa-decimal literals (Base 16) : In this form the allowed digits are 0-9 and
characters are a-f. We can use both uppercase and lowercase characters.
It should be prefix with 0X or ox
int x=OX123Face;

 By default, every literal is of int type, we can specify explicitly as long type by suffixed with l
or L

Floating-Point literal:
For Floating-point data types, we can specify literals in only decimal form .

double d=123.454;

 By default every floating point literal is of double type and hence we can’t assign directly to
float variable. But we can specify floating point literal as float type by suffixed with f or F.
We can specify explicitly floating point literal as double type by suffixed with d or D. Of
course this convention is not required.

Char literal
For char data types we can specify literals in 4 ways:
1. Single quote : We can specify literal to char data type as single character within
single quote.
Char ch=’a’;

2. Char literal as Integral literal : we can specify char literal as integral


literal which represents Unicode value of the character. The allowed
range is 0 to 65535.
char ch=062;

3. Unicode Representation : We can specify char literals in Unicode representation


‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.
char ch=’\u0061’;

4. Escape Sequence : Every escape character can be specify as char literals.


char ch=’\n’;

String literal
Any sequence of characters within double quotes is treated as String literals.
String s=”hello”;
Operators in java
Operators are used to perform operations on variables and values. Java provides a rich set of
operators to manipulate variables. We can divide all the Java operators into the following
groups −

 Arithmetic Operators
 Relational Operators
 Bitwise Operators
 Logical Operators
 Assignment Operators
 Misc Operators
Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are
used in algebra. The following table lists the arithmetic operators −
Assume integer variable A=10 and variable B = 20, then

Operator Description Example

Adds values on either side of the


+ (Addition) A + B will give 30
operator.

Subtracts right-hand operand from left-


- (Subtraction) A - B will give -10
hand operand.

* Multiplies values on either side of the


A * B will give 200
(Multiplication) operator.

Divides left-hand operand by right-hand


/ (Division) B / A will give 2
operand.

Divides left-hand operand by right-hand


% (Modulus) B % A will give 0
operand and returns remainder.

++ (Increment) Increases the value of operand by 1. B++ gives 21


-- (Decrement) Decreases the value of operand by 1. B-- gives 19

exampleLive Demo
public class Test {

public static void main(String args[]) {


int a = 10;
int b = 20;
int c = 25;
int d = 25;

System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("b / a = " + (b / a) );
System.out.println("b % a = " + (b % a) );
System.out.println("c % a = " + (c % a) );
System.out.println("a++ = " + (a++) );
System.out.println("b-- = " + (a--) );

// Check the difference in d++ and ++d


System.out.println("d++ = " + (d++) );
System.out.println("++d = " + (++d) );
}
}
Ouput
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++ = 10
b-- = 11
d++ = 25
++d = 27
Relational Operators:
Relational operators are used to compare two values:

Operator Description Example


Checks if the values of two operands
(A == B) is not
== (equal to) are equal or not, if yes then condition
true.
becomes true.

Checks if the values of two operands


!= (not equal to) are equal or not, if values are not (A != B) is true.
equal then condition becomes true.

Checks if the value of left operand is


greater than the value of right (A > B) is not
> (greater than)
operand, if yes then condition true.
becomes true.

Checks if the value of left operand is


< (less than) less than the value of right operand, (A < B) is true.
if yes then condition becomes true.

Checks if the value of left operand is


>= (greater than or greater than or equal to the value of (A >= B) is not
equal to) right operand, if yes then condition true.
becomes true.

Checks if the value of left operand is


<= (less than or less than or equal to the value of
(A <= B) is true
equal to) right operand, if yes then condition
becomes true.

exampleLive Demo
public class Test {

public static void main(String args[]) {


int a = 10;
int b = 20;

System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}

This will produce the following result −


a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

Bitwise Operators
Java defines several bitwise operators, which can be applied to the integer types, long, int,
short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and
b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101

Operator Description Example

(A & B) will
Binary AND Operator copies a bit to the result if it
& (bitwise and) give 12 which
exists in both operands.
is 0000 1100

(A | B) will
Binary OR Operator copies a bit if it exists in either
| (bitwise or) give 61 which
operand.
is 0011 1101

(A ^ B) will
Binary XOR Operator copies the bit if it is set in one
^ (bitwise XOR) give 49 which
operand but not both.
is 0011 0001

~ (bitwise Binary Ones Complement Operator is unary and has (~A ) will give
compliment) the effect of 'flipping' bits. -61 which is
1100 0011 in
2's
complement
form due to a
signed binary
number.

A << 2 will
Binary Left Shift Operator. The left operands value is
give 240
<< (left shift) moved left by the number of bits specified by the right
which is 1111
operand.
0000

Binary Right Shift Operator. The left operands value is A >> 2 will
>> (right shift) moved right by the number of bits specified by the give 15 which
right operand. is 1111

Shift right zero fill operator. The left operands value is


A >>>2 will
>>> (zero fill moved right by the number of bits specified by the
give 15 which
right shift) right operand and shifted values are filled up with
is 00001111
zeros.

Logical Operators
The following table lists the logical operators −
Assume Boolean variables A holds true and variable B holds false, then −

Operator Description Example

Called Logical AND operator. If both the


&& (logical
operands are non-zero, then the condition (A && B) is false
and)
becomes true.

Called Logical OR Operator. If any of the


|| (logical or) two operands are non-zero, then the (A || B) is true
condition becomes true.
Called Logical NOT Operator. Use to
reverses the logical state of its operand. If a
! (logical not) !(A && B) is true
condition is true then Logical NOT
operator will make false.

Assignment Operators
Following are the assignment operators supported by Java language −

Operator Description Example

Simple assignment operator. Assigns values from C = A + B will assign value


=
right side operands to left side operand. of A + B into C

Add AND assignment operator. It adds right operand


C += A is equivalent to C = C
+= to the left operand and assign the result to left
+A
operand.

Subtract AND assignment operator. It subtracts right


C -= A is equivalent to C =
-= operand from the left operand and assign the result
C–A
to left operand.

Multiply AND assignment operator. It multiplies


C *= A is equivalent to C = C
*= right operand with the left operand and assign the
*A
result to left operand.

/= Divide AND assignment operator. It divides left


C /= A is equivalent to C = C
operand with the right operand and assign the result
/A
to left operand.

Modulus AND assignment operator. It takes modulus


C %= A is equivalent to C =
%= using two operands and assign the result to left
C%A
operand.
Miscellaneous Operators
There are few other operators supported by Java Language.

Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator. This operator consists of
three operands and is used to evaluate Boolean expressions.
variable x = (expression) ? value if true : value if false

example:
public class Test {

public static void main(String args[]) {


int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );

b = (a == 10) ? 20: 30;


System.out.println( "Value of b is : " + b );
}
}

Output:
Value of b=30
Value of b=20

Instanceof Operator:

This operator is used only for object reference variables. The operator checks whether the
object is of a particular type (class type or interface type). instanceof operator is written
as
( Object reference variable ) instanceof(class/interface type)
Live Demo
public class Test {

public static void main(String args[]) {

String name = "James";

// following will return true since name is type of String


boolean result = name instanceof String;
System.out.println( result );
}
}

Output:
True

Precedence of Java Operators


Operator precedence determines the grouping of terms in an expression. This affects how
an expression is evaluated.
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3 * 2 and then adds into 7.

Category Operator Associativity

Postfix expression++ expression-- Left to right

Unary ++expression –-expression Right to left


+expression –expression ~ !

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> >>> Left to right

Relational < > <= >= instanceof Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right


Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %= ^= |= <<= >>= Right to left


>>>=

You might also like