javanotes
javanotes
main()
{
int a=10,b=20;
c=sum(a,b);
--- sum(a,b);
----
---
getch();
}
sum(int a,int b)
{
a=70
b=90
retrun a;
}
animal
carn herbi
non veg
1.encapsulation
2.Inhertence
3.polymorphism
4.abstraction
5.security c++,Dotnet,python,Java
class student
{
int rno;
string name;
float avg;
void get()
{
rno=1;
name='krishna';
avg=67.8;
}
void get()
{
print
}
}
void display()
{
--; C++ java dotnet python
---;
---;
}
}
i
1991- 4 s/e TV DVD A/C 1995 OAK JAVA
sony samsumg Lg
remote
In november 1995 , Sun micro Systems introduced a new programing language
to the world called JAVA. Java means a particular blend of coffee.
The devlopement was started in 1991 by 4 software engineers and team leader is Mr.
James Gosling. It took around 4 years. Their idea was to create software that could
be embedded in consumer electronic devices. The experment was failure and efforts
were taken to produce a portable,platform independent language called 'OAK'. Later
it was renamed as JAVA.
The advantage of java is Operating System independent and also for web application.
Features of JAVA.
1. Simple : The java designers removed a number of complex features that exists in
c, C++. Java does not support pointer,operator overloading etc.JAVA does not use
the goto statement or header files, structure and unions.
+-*/% int char float double long short int a,b a+b int a[10], b[10],c; c=a+b;
javac java
5. Object oriented : Everthing is an object in java. java does not concentrate only
on procedures. The data and the methods togather describes the state and the
behavior of an object.
The Sun Java introduced JDK 1.0 in 1995 into the market
1. JAVA compiler - javac : convert the English like source code into byte code.
( Which creates class file).
2. JAVA interpreter - java : converts Byte code into machine native code
{
int a, b;
----;
int c=a+b;
----;
----;
}
}
10 20 30
Class which is combination of different data type variables (data members) and the
functions which are going to operate on those (Methods)
called as class.
2. public : with in the package (in all the class ) and other package also can
able to access.
3. protected : with in the class and any class which is derived from this
can able to access.
static: If we specify static for any method such methods can call without any
object.
void : if we specify void for any method it does not return any value.
we may or may not pass any arguments with main. if we pass any arguments such
arguments
should be of String type.
variable: value is going to change from time to time. a=10 a=10.5 a='A'
Data type : The type of information that holds a variable can be called as data
type. In java the data types can be divided into 2 types
1. Primitive data types 2. Reference data types pi=3.14 a=10 b=30 c=70
1. Primitive data type : We can also called as simple or predefined data types.
which holds only one value to the variable
2.float: holds 4 bytes in memory. Either positive or negative with decimal portion
can be called as float. Accepts up to 6 decimal positions. the range of values
that accepts is :
float a; a=10.543219 a=-10.765432
-3.4E+38 to +3.4 E +38 float a; a=10.343223
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,808
6. Char : holds 2 bytes in memory. Other than numbers can be called as characters.
char ch; ch='a' ch='b'
7. Byte : holds 8 bits in memory. The binary raw bits can be stored with this data
type.
8. Boolean : holds 1 bit in memory. Either for true or false can assigned with this
data type (1/0)
Reference Data type: By using existing data type we can create new one. we can also
called as user defined or structure data type. we can able to store more that one
value to the variable at a time.
Ex: Class , Array, Interface.
10 20
Where System is the Static class and out is the parameter field and println() is
the method. Which will display the message along with the message.
The input we can pass as command line arguments. The arguments will be passed to
main method in String format. The string consists of numbers should be converted
into corresponding data type with the help of the following 3 static classes.
class sum
{
public static void main(String [ ]x)
{
int a=10,b=20;
int c= a+b;
System.out.println( "The sum is " + c);
}
}
c:>javac sum.java
c:>java sum
output: The sum is 30
class Rect
{
public static void main(String [ ]x)
{
int l=10,b=20;
int A= l*b;
System.out.println( "The area of rectangle is " + A);
}
}
class Rect
{
public static void main(String [ ]x)
{
int h=10,b=20;
int A= (b*h)/2;
System.out.println( "The area of Triangle is " + A);
}
}
if we save the above
with xyz.java
if we compile it generates the sum.class file.
c:> javac xyz.java
c:> java sum
1. Find area of triangle 1/2 bh
2. Find area of circle pi r* r
3. find area of trepezium 1/2 h *(a+b);
Integer.parseInt()
class sum2
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); '400'
int b=Integer.parseInt(x[1]); '200'
int c = a+b;
System.out.println("The sum is " + c);
}
}
javac sum2.java
java sum2 400 200
save the above program as sum2.java
float a=Float.parseFloat(x[0]);
double a=Double.parseDouble(x[1]);
1. area of trepezium A= 1/2* h *(a+b);
2. area of triangle 1/2 bh
3. area of circle 3.14*r * r
class trep
{
public static void main(String [ ]x)
{
float a= Float.parseFloat(x[0]);
float b=Float.parseFloat(x[1]);
float h=Float.parseFloat(x[2]);
double A = h*(a+b)*0.5;
System.out.println("The area is " + A);
}
}
A=0.5 *b*h
class triangle
{
public static void main(String [ ]x)
{
int b=Integer.parseInt(x[0]);
int h=Integer.parseInt(x[1]);
double A = 0.5*h*b;
System.out.println("The area is " + A);
}
} A=22/7 *r*r
class circle
{
public static void main(String [ ]x)
{
int r=Integer.parseInt(x[0]);
double A = 3.14*r*r;
System.out.println("The area is " + A);
}
}
java circle 45
class swap
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 10
int b=Integer.parseInt(x[1]); 20
a=a+b; int c=a;
b=a-b; a=b;
a=a-b; b=c;
System.out.println("after swapping " + a + " " +b);
}
}
Ex:
c:>javac sum2.java
c:>java sum2 20 40
output: The sum is 60
Operators in java
2. Relational operators : Which are used to specify the relationship that exists
between two are more number of operands. a != b
< > <= >= != = == a != b a=100 a==b a=15 b =20 a==b no a=b; a
3. logical Operators : Which are used to compare the given expressions logically
for true or false. And or not and && || a=10 !a
&& || ! a>b && a>c a>b || a>c
a=10 !a
t t t t
t
t f f t f
t
f t f f t
t !a;
f f f
f f f
4. Bitwise operators: Which are used to compare the given expressions logically bit
by bit.
1 0
^ XOR 1 ^1 =0 0 ^ 0 =0 1^0=1 0^1=1
0 1
0 0
5. Increment operators: Which are used to add one value to exisitng value.
++ is the increment operators. It is of two types. ++a=10 a++ ++a
a=10 a=a+1 =11 ++a = 11 a=10 a++
a=10 ;
System.out.print(a++); 10
System.out.print(a); 11
1. Pre increment ++a : one value will be add to existing value and new value will
be consider for this time processing.
a=10;
System.out.print(++a); 11
System.out.print(a); 11
2. post increment :a++ : one value will be add to existing value and new value will
be consider for next time processing.
Decrement operator : -- is the decrement operator. One value will be deducted from
existing value.
it is of two types.
1. pre-decrement 2. post - decrement
1. pre-decrement : one value will be deducted from the existing value and new value
will be consider for this time processing.
a=10
a=10
--a System.out.print(--a) 9
a-- System.out.print(a--) 10 next time 9
System.out.print(a) 9
2. post - decrement: one value will be deducted from the existing value and new
value will be consider for next time processing.
a--;
7000+12000+5000=23000
class Big
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 10
int b=Integer.parseInt(x[1]); 15
if(a>b)
System.out.println("The big is "+ a);
else
System.out.println("The big is "+ b);15
}
}
class Buzz
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]);
if(a%7==0 && a%10==7) 63 147 217 427
System.out.println("It is Buzz number "+ a);
else
System.out.println("It is not Buzz number "+ a);
}
}
2599 7
999 0
class Lucky 1234 1 7856 8 987 6 1116 9
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]);
int b=a%9; 7212 3 7929=27=9
if(b==0)
System.out.println("Lucky number is 9 ");
else
System.out.println("Lucky number "+ b);
}
}
class Lucky
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 7
if(a>=0)
System.out.println("Positive Number ");
else
System.out.println("Negitive Number");
}
}
class Major
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 9
if(a>=18)
System.out.println("Major ");
else
System.out.println("Minor");
}
}
1234 =10 =1
721=10=1
631+ 10=1
class Magic
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 82 1 631 1 721 1 1234 1
if(a%9==1)
System.out.println("Magic Number ");
else
System.out.println("Not Magic Number");
}
}
if(expr)
{
if(expr)
{
if(expr)
{
----;
----;
----;
}
else
{
----;
----;
----;
}
}
else
{
-----;
-----;
-----;
}
else
{
----;
----;
----;
}
class Big
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]);
int b=Integer.parseInt(x[1]);
int c=Integer.parseInt(x[2]);
if(a>b)
{
if(a>c)
System.out.println("The big is "+ a);
else
System.out.println("The big is "+ c);
}
else
{
if(b>c)
System.out.println("The big is " + b);
else
System.out.println("The big is " + c);
}
}
}
class Leap
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 1996 2000 1900 1800 1700 1600 1992
if(a%100==0)
{
if(a%400==0)
System.out.println("Leap year "+ a);
else
System.out.println(" Not Leap Year"+ a);
}
else
{
if(a%4==0)
System.out.println("Leap Year");
else
System.out.println("Not leap year");
}
}
}
Ladder If
Which is to for comparing more number of expressions. if the given expression is
true then
execute the statements and exit from the if. when it is false compare another if
and so on.
The else is for previous if statement.
if(expr)
{
----;
----;
----;
}
else if(expr)
{
----;
----;
----;
}
else if(expr)
{
----;
----;
----;
}
else if(expr)
{
----;
----;
----;
}
else
{
----;
----;
----;
}
class marks
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 80
int b=Integer.parseInt(x[1]); 70
int c=Integer.parseInt(x[2]); 90
double avg=(a+b+c)/3; 63
if(avg<35)
System.out.println("Fail");
else if(avg<50)
System.out.println("Pass");
else if(avg<60)
System.out.println("Second");
else if(avg<75)
System.out.println("First");
else
System.out.println("Distinction");
}
} 3 --- UB 6
2 -- MB 5
class berth 1 --- LB 4 --7 SL 8 SU 45/8
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 42
int r= a%8;
if(r==1||r==4)
System.out.println("LB");
else if(r==2||r==5)
System.out.println("MB");
else if(r==3||r==6)
System.out.println("UB");
else if(r==7)
System.out.println("SL");
else
System.out.println("SU");
}
}
Ternary Operator : ? and : can be called as Ternary Operator. ? is for True
part : is for false part
variable = expr1? expr2:exp3;
class Big
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 10
int b=Integer.parseInt(x[1]); 25
int c=a>b?a:b;
System.out.println("The big is " + c);
}
}
class Big
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); a > b 10 20 30
int b=Integer.parseInt(x[1]); 40 80 70
int c=Integer.parseInt(x[2]);
int d=a>b?(a>c?a:c):(b>c?b:c);
System.out.println("The big is " + d);
}
}
class Magic
{
public static void main(String [ ]x)
{
int n=Integer.parseInt(x[0]);
String s= n%9==1?"Magic Number" : "Not Magic Number";
System.out.println(s);
}
}
Switch - case :
which is a selective statement, Through which we can choose one out of number of
cases. if the selected case is not matched control will be passed to default
statement. Each case is terminated
by force with break statement.
switch(choice)
{
class A
{
public static void main(String [ ]x)
{
int n=Integer.parseInt(x[0]);
switch(n%7)
{
case 1: System.out.println("Monday");
break;
case 2: System.out.println("Tuesday");
break;
case 3: System.out.println("Wednesday");
break;
case 4: System.out.println("Thursday");
break;
case 5: System.out.println("Friday");
break;
case 6: System.out.println("Saturday");
break;
default: System.out.println("Sunday");
}
}
}