The following steps to be taken when you writing a java program:-
---------------------------------------------------------
Comment:-
It represents description about all the features of a program. (or)
Un executable lines in a program.
There are 3 types of comments in Java.
1. //single line comment
2. /* multiple line--
---comments------
------------------*/
3./**--java documentation
----comments-----------
---------------------*/ --> they are useful to create API document from a .java program.
API:- (application programming interface)
IIQ) What is API document?
It is a .html file that contains description of all the features of a software,a product or a technology.
Note:- javadoc compiler is used to create API document
importing classes:-
javalibrary-->packages-->classes/interfaces-->methods
package:-
It is a sub directory which contains classes and interfaces.
ex:- import java.lang.System;
import java.lang.String;
-->If you want to import several classes of same package then we write import java.lang.*;
IIQ) what is the difference between #include and import?
#include makes a c (or) c++ compiler to copy entire header file code into the program.Thus the program size increases unneccessarily and it wastes memory and processor time.
Import statement makes JVM to go to the Java library,and execute the code there and copy the result into the java program.
So import is better mechanism than #include.
IIQ)what is JRE (Java Runtime Environment)?
jre = jvm + java library.
ex:-
class First{
public static void main(String args[]){
System.out.print("Welcome to java");
}
}
==>Jvm starts execution of a java program from main() method.
==>Values passed to method are called arguments.
Calling a method in java in 2 ways.
1.create an object to the class to which the method belongs.
classname objectname = new classname();
calling the method like this:
objectname.methodname();
2. classname.methodname();
The second method for static methods only.
static method:- Static method is a method that can be called and executed without using any object.
==> print method belongs to PrintStream class.
There is a shortcut available to create the object of PrintStream class.That is System.out (i.e: monitor).
Here out is a variable (or) field in System class.
field is nothing but static variable.
How to compile and execute a java program:-
-------------------------------------------
d:\>javac First.java === This is saved file.
d:\>java First === this is .class file.
IIQ) what is the difference between print() and println()?
print() method displays the result and keep the cursor in the same line.
println() method displays the result and then throws the cursor to the next line.
Backslash code meaning
============== ========
\n next line
\t Horizontal tab space
\r enter key
\b Back space
\\ displays \
\" displays "
\' displays '
ex:-
System.out.println("Hello");
System.out.println("\\Hello");
System.out.println("\"Hello\"");
o/p:- Hello
\Hello
"Hello"
important points:-
=================
-> Java is case sensitive language.
-> Every statement should end with semicolen.
In java we should follow naming convensions(rules):
Naming Convensions
------------------
1)Package names in java are written in all small letters.
ex:- java.awt
java.io
java.swing
2) Each word of class names and interfaces start with a capital.
ex:- String,DataInputStream,ActionListener
3) Method names start with small letter,then second word onwords start with a capital.
ex:- println(),readLine(),getNumberInstance(),etc..
4)Variable names also follow the above rule.
ex:- age , empName, employee_Net_Sal
5)Constants should be written using all capital letters.
ex:- PI , MAX_VALUE , Font.BOLD (Here Font is class)
6)All keywords should be written in all small letters.
ex:- public , void , import
java.lang is the default package that is imported in to every program
Important packages of core java:
1.java.lang :-
This package got primary classes and interfaces essential for java program it consists of wrapper classes (useful to convert ordinary data in to objects),strings,threads.
2.java.util:-
This package contains useful classes and interfaces
like Stacks,LinkedList,HashTable,Arrays
3.java.io:-
This package handles files and input output related tasks.
4.java.awt(abstract window tool kit):-
this package helps to develop GUI.it consists of an important sub package is java.awt.event.
java.awt.event is providing actions to components.
5.javax.swing(extended package):-
This is also helps to develop GUI like java.awt
==>difference between 4 and 5 is a lot of new feauters are available in swing
6.java.net:-
client server programing can be done using this package
7.java.applet:-
applet means programs .They are stored in server on internet
Applets are programes which come from a server in to client and get executed on the client machine
8.java.sql:-
This package helps to connect to data bases like oracle and utilize them in java.
Data type:-
It represents the type of data stored into variable(or)memory.
Datatypes and literals:-
1.Integer datatypes:-
These data types store integer numbers like 125,100000,-10,...
int is divided into 4 types.
Datatype Memory size Range
----------- ------------ ---------
byte 8 bits(1 byte) -128 to +127
short 2 bytes -32,768 to +32,767
int 4 bytes -2147483648 to +2147483647
long 8 bytes -9223372036854775808 to
+9223372036854775807
=>Literal:- It is the value stored into variable directly in the program.
ex:- byte rno=10;
here 10 is literal.
long x = 150L;
Here 8 bytes memory will be alloted.If there is no L then jvm allocates memory 2 bytes only.
2.float datatypes:-(single precision floating number)
These data types represents numbers with decimal points like 125.36,0.0034,-1.0,etc....
These are again classified into float and double.
double means double precision floating point.
precision ==> accuracy.
Data type Memory size range
----------- ------------- ---------
float 4 bytes 1.4*10p-45 to 3.4*10p38
double 8 bytes 4.9*10p-324 to 1.8*10p308
ex:- float PI=3.142F;
here 4 bytes memory alloted by JVM.
Here there is no F,Jvm alloted 8 bytes.
IIQ) What is the difference between float and double?
Float can represent upto 7 digits after decimal point accurately.
double can represent upto 15 digits after decimal point.
3)character datatype:-
This datatype represents a single character like A,a,9,*,etc..,
datatype memory size range
-------- ----------- --------
char 2 bytes 0 to 65,535
ex:- char ch='x';
IIQ)what is UNICODE system?
It is an encoding standard to include the alphabet from all human languages into the character set of java.
UNICODE system used 2 bytes to represent each character.
4)String datatype:-
It represents a group of characters.
ex:- 1.String name="Gouse";
2.String str = new String("Md.Gouse");
5)boolean datatype:-
boolean means two.These can handle either true (or) false.Jvm uses 1 bit to represent a boolean value internally.
ex:- boolean response = true;
OPERATORS
---------
operator:- it is a symbol that performs an operation.
==> An operator acts on some variables called operands to get the desired resuls.
ex:- a + b
here a,b are operands and + is operator.
Unary operator:-
if an operator acts on a single variable it is called unary operator.
Binary operator:-
If an operator acts on two variables it is called Binary operator.
Ternary operator:-
If an operator acts on three variables it is called Ternary operator.
Various Types of Operators used in JAVA:-
------------------------------------------
1) Arithmatical operators:- ( +,-,*,/,% )
2) Unary operators:-
a) unary minus operator(-)
b) Increment operator ( ++ )
c) Decrement operator ( -- )
3) Assignment operator:- ( = , += , -= , *= , %= , /= )
4) Relational operators:-
a)greater than ( > )
b)greater than or equal to ( >= )
c)less than ( < )
d)less than or equal to ( <= )
e)equal to ( == )
f)not equal to ( != )
5) Logical operators:-
a) Logical AND ( && )
b) Logical OR ( || )
c) Logical NOT ( ! )
6) Boolean operators:-
a) Boolean AND ( & )
b) Boolean OR ( | )
c) Boolean NOT ( ! )
7) Bitwise operators:-
a) bit-wise complement operator ( ~ )
a) bit-wise AND operator ( & )
a) bit-wise OR operator ( | )
a) bit-wise XOR operator ( ^ )
a) bit-wise Left shift operator ( << )
a) bit-wise Right shift operator ( >> )
a) bit-wise zero fill right shit operator ( >>> )
8) Ternary operator (or) Condition operator ( ?: )
9) Member operator ( . ) (or) dot operator
10) Instanceof operator
11) new operator
12) cast operator ( () )
Bit-wise zero fill right shift operator( >>> ):-
------------------------------------------------
This operator also shift the bits towards right.But it does not protect the sign bit.
'>>>' operator always fills the sign bit with '0'.
IIQ)what is the difference between >> and >>> ?
Both bitwise right shift(>>) and bitwise zero fill right shift(>>>) operators are used to shift the bits towards right.The only difference is that >> will protect the sign bit whereas the >>> operator will not protect the sign bit.It always fills 0 in the sign bit.
Priority of operators:-
----------------------
Certain rules of operator precedence are followed:
1)the content inside the braces: () and [] will be executed.
2)Next ++ and --
3)Next *,/,and % will execute.
4)+ and - will come next.
5)Relational operators are executed next.
6)Boolean operators and bitwise operators.
7)Logical operators will come afterwords.
8)then ternary operator.
9)Assignment operators are executed at the last.
sequential execution:-
---------------------
Executing the statements one by one sequentially is called sequential execution. It is useful to write simple programs.
Random execution:-
------------------
Executing the statements randomly and repeatedly is called Random execution.
The following are the statements which change the flow of execution.
The following are control statements in JAVA.
1) if --- else statement
2) do --- while loop
3) while loop
4) for loop
5) foreach loop
6) switch statement
7) break statement
8) continue statement
9) return statement
if-else statement:-
-------------------
This statement executes a task depending upon a condition is true (or) not.
Syn:-
if(condition)
Statements-1;
[else statements-2;]
do-while loop:-
---------------
This loop executes a group of statements repeatedly as long as a condition is true.
syn:-
do
{
statements;
}while(condition);
while loop:-
-----------
It is same as do while.It is executes a group of statements repeatedly as long as a condition is true.
syn:-
while(condition)
{
statements;
}
for loop:-
---------
This loop executes a group of statements repeatedly as long as a condition is true.
Syn:-
for(exp1;exp2;exp3)
{
statements;
}
here exp1 = initialization
exp2 = condition
exp3 = updation
=>we can write a for loop without using exp1 (or) exp2 (or) exp3 (or) any two expressions (or) all the three expressions.
=> A loop that executes forever is called Infinite loop.
=> Infinite loops are drawbacks(mistakes) in ur program.
ctrl+c to terminate from program.
Note:- we can write loop inside another loop.They are called nested loops.
Infinite loops :-
==============
for(;;)
{
statments;
}
while(true)
{
statements;
}
do
{
statements;
}while(true);
foreach loop:-
-------------
this loop executes a group of statements for each element of a collection(group of elements).
collection:- any array,any class of java.util
syn:-
for(var:collection)
{
statements;
}
ex:- (program)
class Demo{
p s v m(String args[]){
int arr[] = {10,20,40,50};
for(int x:arr)
{
System.out.print(x + "\t");
//System.out.println(x);
}
}
}
IIQ)Why goto statement is not available in java?
1.goto statements lead to confusion for a programmer.
2.goto's form infinite loops easily.
3.goto's make documentation difficult.
4.goto statements are not part of structural programming.
break:-
-------
It is used in 3 ways.
1.It is used to comeout of loop.
2.It is used to comeout from switch statement
3.It is used in nested blocks to goto end of a block.
return:-
---------
There are 2 uses.
1.return statement is used to comeout of a method block to the calling method.
2.return statement can be used to return some value to the calling method.
Note:-
return statement in main() will terminate the program(or) application.
IIQ)what is the difference between System.exit(0) and System.exit(1)?
Sys.exit(0) represents normal termination.
sys.exit(1) represents termination due to error.
switch statement:-
----------------
It is useful to execute a particular task from available tasks depending upon the value of variable.
syn:-
switch(option)
{
case value-1:
statements-1;
case value-2:
statements-2;
--------------
--------------
case value-n:
statements-n;
[default:
default statements;]
}
H.W:
1)print odd numbers upto 100
2)Test whether the no is even (or) odd
3)Test whether the number is prime (or) not
4)display a multiplication table.
5)display the stars in the form
*
* *
* * *
* * * *
No comments:
Post a Comment