Java Environment:
JDK (Java Development Kit)
Basic Tools: These tools are the foundation of the JDK. They are the tools you
use to create and build applications.
Tool Name Brief Description Links to Reference Pages
Javac The compiler for the Java
programming language.
[Solaris and Linux] [Windows]
Java The launcher for Java applications. In
this release, a single launcher is used
both for development and
deployment.
The old deployment launcher, jre, is
no longer provided.
[Solaris and Linux] [Windows]
javadoc API documentation generator.
See Javadoc Tool page for doclet and
taglet APIs.
[Solaris and Linux] [Windows]
Apt Annotation processing tool.
See Annotation Processing Tool for
program annotation processing.
[Solaris, Linux, and Windows]
appletviewer Run and debug applets without a web
browser.
[Solaris and Linux] [Windows]
Jar Create and manage Java Archive
(JAR) files.
[Solaris and Linux] [Windows]
See Java Archive Files page for the
JAR specification.
jdb The Java Debugger.
See JPDA for the debugger
architecture specifications.
[Solaris and Linux] [Windows]
Javah C header and stub generator. Used to
write native methods.
[Solaris and Linux] [Windows]
Javap Class file disassembler [Solaris and Linux] [Windows]
extcheck Utility to detect Jar conflicts. [Solaris and Linux] [Windows]
JVM (Java Virtual Machine): All the language compiler converts the source
code into machine code and this machine code in java is known as byte code, which is
also known as intermediate language. After the compilation Java will generate the
byte code for that machine, which does not exist, known as JVM. It exists inside the
memory. It is a simulated computer within the computer, which can do all major work
as the real computer. Here the java interpreter acts as an intermediate between virtual
machine and real machine.
Java Program:
Documentation Section
Package Statement
Import Statement
Interface Statement
Class Definition
main method
{
main method definition
}
Suggested
Optional
Optional
Optional
Essential
EssentialSimple Java Program: The best way to learn a new language is to write a
simple small program. Here we are going to begin with a very simple program that
print a message as an output.
/* Write a java program to print the message- Welcome to the world of java!!!*/
class Message
{
public static void main(String ar[])
{
[Link](“Welcome to the world of Java!!!”);
}
}
The above program is the simplest of all java programs. Let’s therefore discuss the
program line by line and understand the unique feature that constitutes a java
program.
Class Declaration: The first line-
class Message- It declares a class, which is an object oriented construct. As stated
earlier, Java is a true object programming language and therefore everything(code)
must be placed inside the class. ‘class’ is a keyword and declares that a new class
definition follows ‘Message’ is a java identifier that specifies the name of the class to
be defined. The rules of declaration of an identifier (Interface, package, class, object,
function, label, variable and constant name) must be followed.
Opening Braces: The second line-
Every class definition in java begins with an opening braces ( { ) and ends with a
matching closing braces ( } ) appearing in the last line in the above program.
The main line: public static void main(String ar[])
It defines a method named main. Conceptually, this is similar to the main() in C/C++.
Every java application program must include the main() method. This is the starting
point for the interpreter to begin the execution of the program. A java application can
have any number of classes but only one of them must include the main method to
initiate the execution.
Note: java applet will not use the main method at all. The main line contains a number
of keywords, public, static and [Link]: The keyword public is an access specifier that
declares the main method as
unprotected and therefore making it accessible to other classes.
static: It declares the method as one that belongs to the entire class and not a part of
any object of the class. The main() must always be declared as static since the
interpreter uses this method before any objects are created.
void: The type modifier ‘void’ states that the main() does not return any value.
The output line: The only executable statement in the program is-
[Link](“Welcome to the world of Java!!!”);
The println() method is a member of ‘out’ object, which is a static data member of
’System’ class. This line prints the string-
Welcome to the world of Java!!!
to the screen. The method println() always appends a new line character to the end of
the string. This means that any subsequent output will start on a new line.
Note: In java each and every statement must be ended with semicolon(;). Semicolon
must not be put in the statement just before the opening braces.
Comments: The purpose of comment in the java program is to describe the logic
of the program and why the program is all about, so that the new programmer can
easily understand the logic of the program and why the program is all about.
Comment lines are ignored by the compiler. Java supports the following types of
comments-
1. Single line comment(//………..): It starts with double forward slash (//) and
ends with the end of the line.
2. Multiline comment(/*………*/): It starts with forward slash asterisk(/*) and
ends with asterisk forward slash(*/).
3. Documentation comment (/**………*/): It starts with forward slash double
asterisk and ends with the end of the line.
Tokens: The smallest individual part of the java program is known as token. Java
program consists of tokens, white space and comment. Java language includes five
types of tokens. These are given below-
1. Reserve word or Keyword.
2. Identifiers.
3. Literals or Constant.
4. Operator.
5. Separator/Punctuator.
Reserve Word/Keyword: The predefined word in java is known as keyword or
reserve word, which has a special meaning and function in the java program. There
are 60 keywords available in java. All the keywords are in lower case.
Identifiers: The word which are defined by the programmer or the programmer
defined tokens are known as identifiers. It consists of set of characters. It includesInterface
name, package name, class name, object name, function name, label name,
variable name and constant name. The rules of declaration of identifiers-
1. It must not be a keyword.
2. It can’t contain any special symbol except underscore (_) or dollar sign ($).
3. It must not start with digit. Digit may contain in between or at last.
4. It can be of any length.
Literals/Constant: It is a sequence of characters (identifier) that represents the
constant value to be stored in a variable. Java language specifies five major types of
literals. They are- Integer, Floating, Character, String, Boolean and Null constant.
Operators: Operators are the symbols which perform the specific task.
Separator/Punctuator: Separator is a symbol to indicate group of code how they are
divided and arranged. Eg:- [],{},(),.,:,; and ,
.
Java Statements: Statement is the collection of tokens, white space ending with
semicolon (;). It is of following types-
1. Empty Statement: These do nothing and are used during program
development as a place holder.
2. Labeled Statement: Any statement may begin with a label such label must not
be a keywords, already declared variables or previously used labels in the
current module. Labels in java are used as the argument of jump statement.
3. Expression Statements: Most statements are expression statements. Java has
seven types of expression statements-
a) Assignment
b) Pre-Increment
c) Pre-Decrement
d) Post-Increment
e) Post-Decrement
f) Method Calls
g) Allocation Expression
4. Control Statement: Various control statements are there-
1) Selection Statements:
a) Bi-directional Statements:
a. Simple if statements
b. if-else statements
c. Nested of if statements
d. else-if ladder statements
b) Multi-branching Statements:
a. Switch case statement
2) Iteration Statements:
a) for loop
b) while loop
c) do-while loop
3) Jump Statements:
a) break statement
b) continue statementc) return statement
5. Synchronized statement: This associated with the synchronized keyword.
6. Guarding statement: Guard statement is used for guarding the exception
whether it is occurred or not and it is controlled by the exception handler
keyword i.e. ‘try’ and ‘catch’.
Implementing a Java Program: Implementation of a Java application
program involve the following steps-
1. Creating the program.
2. Compiling the program.
3. Running the program.
It must be remembered that, before we begin creating the program, The JDK (Java
Development Kit) must be properly installed on your computer system.
Operators in Java
Operators: Operators are the symbol, which performs the specific task. Operators
can be categorized into the following categories-
1. On the basis of their operands:
(a) Unary Operator: The operator, which needs single operand to perform their
task called unary operator.
(b) Binary Operator: The operator, which needs two operands to perform their
task called binary operator.
(c) Ternary Operator: The operator, which needs three operands to perform their
task called ternary operator.
2. On the basis of their function.
(a) Arithmetic Operator:
(b) Relational Operator:
(c) Logical Operator.
(d) Assignment Operator:
(e) Bitwise Operator:
(f) Conditional Operator:
(g) Special Operator:
(h) Increment/Decrement Operator.
(i) Unary Plus Operator:
(j) Unary Minus Operator.
(k) Concatenation Operator.
Arithmetic Operator: Java arithmetic operators are used to perform addition,
subtraction, multiplication, and division. They act as basic mathematical
operations. Arithmetic operators are applied on numeric as well as character data
type and always return numeric value.
[Link]. SYMBOL MEANING EXAMPLE
(x=10 and y=5)
RESULT
1. + Addition x+y 15
2. - Subtraction x-y 5
3. * Multiplication x*y 50
4. / Division x/y 2
5. % Remainder/Modulo Division x%y 0
Relational Operator: The purpose of relational operator is to check the relation
between two variables (operands) and returns the boolean value either true or
false. It is applied on numeric, character as well as boolean data type.
[Link]. SYMBOL MEANING EXAMPLE
(x=5 and y=10) RESULT
1. > Greater than x>y false
2. < Less than x<y true
3. >= Greater than or equals to x>=y false
4. <= Less than or equals to x<=y true
5. == Equals to x==y false
6. != Not equals to x!=y true
Note: boolean type operands are applicable for equlas(==) and not equals(!=)
operator.
Logical Operator: The purpose of logical operator is to combine two or more
relations and returns a boolean value either true or false.
[Link]. SYMBOL MEANING EXAMPLE (X=1) RESULT
1 && Logical AND x < 5 && x < 10 true
2 || Logical OR x < 5 || x < 4 true
3 ! Logical NOT !(x < 5 && x < 10) false
Logical AND(&&) Operator: When we combine two or more relation using
logical AND (&&) operator, it returns true if both or all the relation (condition)
becomes true and if any one of the combine relation (condition) is false it
returns false.
Logical OR(||) Operator: When we combine two or more relation using
logical OR (||) operator, it returns true if anyone of the combine relation
(condition) becomes true and it returns false when all the combined relation
(condition) becomes false.
Logical NOT(!) Operator: Logical NOT(!) is a unary operator. It returns true if
the relation is false and it returns false if the relation is true.
Assignment Operator: The purpose of assignment operator is to assignment
operator is to assign a new value to the variable.
Syntax- variable=value;
Short hand assignment operator: The purpose of short hand assignment
operator is to update the value of the variable by something.
Syntax- variable<operator>=value;
Eg:- a+=5; => a=a+5;
Bit-wise Operator: Bit-wise operator performs the operator on bit level.
[Link]. SYMBOL MEANING EXAMPLE
(a=10,b=12)
RESULT
1
&
bitwise and a&b 8
2
|
bitwise or a|b 14
3
^
bitwise XOR a^b 6
4
~
bitwise compliment ~a -11
5
<< left shift a<<2 40
6
>> right shift a>>2 2
7
>>> zero fill right shift a>>>2 2
Conditional Operator(?:): Conditional operator is a ternary operator. The
purpose of conditional operator is to check the provided condition. Condition
either may true or false. If the condition becomes true expression 1 will be
evaluated else expression 2 will be evaluated.
Syntax- variable=(condition)?expression1:expression2;
[Link]((condition)?Statement 1: Statement 2);
Increment(++)/Decrement(--): Both increment and decrement operators are
the unary operator. Increment/Decrement operator increases/decreases the value
of the variable by 1. Unary operator usually takes place before the operand but
increment/decrement operator takes place either before the operand called prefix
increment/decrement operator or after the operand called postfix
increment/decrement operator.
Eg:- int a=5;
a++; => a=a+1;
++a; => a=a+1;
a--; => a=a-1;
--a; => a=a-1;
Unary Plus(+): The unary plus operator(+) is written before the operand. This
operator results in the same value as the operand.
Unary Minus(+): The unary minus operator(-) is also written before the
operand. This operator negates the value of an operand.
String Concatenation Operaotr(+): The purpose of this operator is to join
two strings.
Eg:- String s1=”Aman”, s2=”Verma”,s3;
s3=s1+s2;
Java Question:
1. W.A.P to enter two numbers and print their sum.
2. W.A.P to enter two numbers and print their difference.
3. W.A.P to enter two numbers and print their product.
4. W.A.P to enter two numbers and print their quotient.
5. W.A.P to enter two numbers and print the remainder.
6. W.A.P. to enter two numbers and print their sum, difference, product, division and remainder.
7. W.A.P. to enter three different numbers and print their average.
8. W.A.P. to enter the rate and quantity of an article and print the amount to be paid.
9. W.A.P. to enter the rate and quantity of an article and print the amount to be paid after giving
10% as discount.
10. W.A.P. to enter the rate, quantity and discount percent of an article and print the amount to
be paid after discount.
11. W.A.P. to enter the rate and quantity of an article and print the amount to be paid including
GST 18%.
12. W.A.P to enter two numbers and swap both the numbers.
13. W.A.P to enter two numbers and swap both the numbers without using any extra variable.
14. W.A.P to enter two numbers and swap both the numbers without using any extra variable
and any arithmetic operators.
15. W.A.P. to enter all the possible attributes of the following geometrical figure and print their
respective area, perimeter and etc.
a) Triangle
b) Rectangle
c) Square
d) Parallelogram
e) Trapezium
f) Rhombus
g) Circle
h) Cube
i) Cuboid
j) Sphere
k) Cylinder
l) Cone
m) Prism
16. W.A.P. to enter the temperature in degree Fahrenheit and print it into degree Celsius.
17. W.A.P. to enter the temperature in degree Celsius and print it into degree Fahrenheit.
18. W.A.P. to enter the length in any one unit and print in rest another unit.
millimetre centimetre decimetre meter decametre hectometre kilometre
19. W.A.P. to enter the speed in km/s and convert it into m/s.
20. W.A.P. to enter the speed in m/s and convert it into km/h.
21. W.A.P. to enter the no. of days and print the no. of months.
22. W.A.P. to enter the no. of days and print the no. of years.
23. W.A.P. to enter the no. of days and print the no. of years, months and remaining days.
24. W.A.P. to enter the time in second and print it into minutes.
25. W.A.P. to enter the time in second and print it into hours.
26. W.A.P. to enter the time in minute and print it into seconds.
27. W.A.P. to enter the time in minute and print it into hours.
28. W.A.P. to enter the time in hour and print it into seconds.
29. W.A.P. to enter the time in hour and print it into minutes.
30. W.A. P. to enter the time in second and print it into hours, minute and remaining seconds.
31. W.A.P. to enter the three numbers and print the mid numbers.
32. W.A.P. to enter the three/four/five digit numbers and print the last digit.
33. W.A.P. to enter the three/four/five digit numbers and print the first digit.
34. W.A.P. to enter the three digit number and print the mid digit.
35. W.A.P. to enter three/four/five digit number and print the sum of the digit.
36. W.A.P. to enter the three/four/five digit number and print the sum of first and last digit.
37. W.A.P. to enter the two/three/four different numbers and print the maximum.
38. W.A.P. to enter the two/three/four different numbers and print the minimum.
39. W.A.P. to enter the three different numbers and print the mid value.
40. W.A.P. to enter the three/four/five digit number and print the maximum digit.
41. W.A.P. to enter the three/four/five digit number and print the minimum digit.
42. W.A.P. to enter the three digit number and print the mid digit value.