Professional Documents
Culture Documents
Learning JAVA
Learning JAVA
Learning JAVA
Tutorial
on JAVA
Dheeraj Mehrotra
City Montessori School & Degree College
Lucknow (India)
attention@computerscienceexpertise.com
www.computerscienceexpertise.com
Towards QUALITY IT LITERACY FOR ALL
attention@computerscienceexpertise.com
o Class :
o animal dog;
attention@computerscienceexpertise.com
n Abstraction
n Encapsulation
n Inheritance
n Polymorphism
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
o Keywords
o Identifiers
o Literals
o Separators
o Operators
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
This is followed by brace {, which indicates the beginning of the block of executable statements.
defines the function main( ). The declaration of the main() function contains the following
keywords:
The Public keyword acts as an access specifier which declares the main function to be publically
accessible for other classes.
The Static definition declares that the very function or method is independent of the entire class
and does not figure as a part of any objects of the class. In the above program, the class
exampleprogram is unchanging or static, as a state. The term static means unchanging and
implicitly final.
The Void is a data type modifier which defines the main() function of no return type.
The argument of the main() function lies within braces eg. (String args[ ]), here String args[ ]
declares an argument parameter asattention@computerscienceexpertise.com
args containing an array of objects of the class type String.
It specifies that the main method takes an argument that is an object of string.
attention@computerscienceexpertise.com
Output Statements:
The println( ) method or the function is used to display information on
the screen. It also provides a line feed i.e. transfers the control to the
next line of the output.
class output
{ public void display()
{int a=10;
int b=20;
int c=30;
int d=40;
System.out.println("The usage of println( ) method");
System.out.println("The value of a is "+a);
System.out.println("The value of b is "+b);
System.out.println("The value of c is "+c);
System.out.println("The value of d is "+d);
}}
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
eg.
int age;
String name;
boolean isAbsent;
attention@computerscienceexpertise.com
+ Addition a+b
- Substraction a-b
* Product a*b
/ Division a/b
% Modulus a%b
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
eg. ++marks;
increments the value of the variable marks by 1.
attention@computerscienceexpertise.com
which indicates that if the test expression is true then the transfer value
for storage into the value variable is expression1 and if the test
expression is false the transfer value becomes the expression2.
for example:
grade = (total > 40) ? ‘P’ : ‘F’;
In the above example grade refers to the value variable, total>40 is the
test expression, ‘P’ is the expression1 and ‘F’ is the expression2. The ?
character follows the true condition and : follows the false condition.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
if (expression)
statement1;
attention@computerscienceexpertise.com
if (expression)
statement 1;
else
statement 2;
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
(ii). The IF-ELSE statement uses both integer as well as floating type values
whereas the SWITCH statement only accepts integer values.
attention@computerscienceexpertise.com
Initialization;
while (test expression)
{
body of the loop
}
where the test expression is the conditional state which executes the
body of the loop if it returns the true value and terminates once the
value returned for the test condition is false. It acts as an entry
controlled loop and the given test expression is evaluated at every
execution of the body of the loop.
attention@computerscienceexpertise.com
do
{
body of the loop
}
while (test expression);
attention@computerscienceexpertise.com
The above segment assigns the loop variable s by 1, the test condition
is j<11 and the increment expression is j++. The loop executes for 10
times, with the value of s ranging from 1 to 10.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
b. Default Constructors:
attention@computerscienceexpertise.com
String var-name;
var-name = new String(“value”);
attention@computerscienceexpertise.com
(xi). indexOf();
FORMAT: a1.indexOf(‘d’);
USAGE:
This returns the position of the first occurrence of ‘d’ in the string a1.
(xii). indexOf(); [another format]
FORMAT: a1.indexOf(‘d’,n);
USAGE:
This format gives the position of ‘d’ starting from the nth position in the
string variable a1.
(xiii). compareTo()
FORMAT: a1.compareTo(a2);
USAGE:
This function returns negative if a1 is less than a2, positive if a1 is greater
than a2 and zero if a1 is equal to a2.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
n Thank you
§ Send your queries at
§ attention@computerscienceexpertise.com
§ Visit: www.computerscienceexpertise.com
§ Towards Quality IT Literacy for All.
attention@computerscienceexpertise.com