0% found this document useful (0 votes)
5 views

javanotes

The document provides an overview of Java programming, including its history, features, and basic syntax. It covers concepts such as data types, operators, control flow statements, and object-oriented principles like encapsulation, inheritance, and polymorphism. Additionally, it includes examples of Java code for various operations and applications.

Uploaded by

shrutikreddy14
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

javanotes

The document provides an overview of Java programming, including its history, features, and basic syntax. It covers concepts such as data types, operators, control flow statements, and object-oriented principles like encapsulation, inheritance, and polymorphism. Additionally, it includes examples of Java code for various operations and applications.

Uploaded by

shrutikreddy14
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

int a;

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;

2. Platform independent: Which referece to ability of a program to move from one


computer to .another without any difficulty. Java is platform independent at source
level and binary level.

3. Robust: Java is strictly typed languge. Hence it requires explicite method


declaration. java checks code at the time of compiling the application and at
interpretation level. which has both compiler and interpreter.
source program Machine Native Code

javac java

Byte Code Byte Code


4. Secure : Java provides controlled environment for the execution. Java is more
than programming language.It provides several layers for security control.

first layer data and methods encapsulated


second layer complies the program
third layer interpret the byte code.
fourth layer takes care loading of the classes

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.

Which supports polymorphisim,inheritence,encapsulation,abstraction and security.

6.Distibuted : java can be used to develop applications that portable across


multiple platforms and operating systems. java designed to support network
applications.

7. Multithreaded: Java programs use a process called multithreading to perform many


tasks simultaneously.java provides master solution for synchronization multiple
process. ex: game
application.
----
----
----
---
---
---

java introduced into the market by different companies

1. JAVA - Sun micro Systems

2. Blue J - Micro Soft


3. J Builder - Borland

4. Visual age - IBM

The Sun JAVA is more popular one among all others

The Sun Java introduced JDK 1.0 in 1995 into the market

The jdk consists of the following tools

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

3. Applet viewer : Which is used to execute the web applications

Structure of JAVA program


windows - Notepad, notepad plus, Dos - Edit , Linux- Vi , Unix - Vi
class sum
{
public static void main(String [ ]args)

{
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.

The members of the class can be accessed by variable called object.

There are 4 types of access specifiers:

1. private : with in the class scope.

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.

4. default: with in the package in any class.

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.

main() : our program starts from this.

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'

constant : value never change. 22/7 i=p*n*r/100

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

1. int : holds 4 bytes in memory. Either positive or negative without decimal


portion can be called as integer. int a; 32 bits 2 int a;
a=10,20,30,40
a=20
a=10000
a=-20

4 bytes-> 32 bits 2 power 32 65536X65536


The range of values accepts : -2,147,483,648 to + 2,147,483,647

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

3. Double : holds 8 bytes in memory. Either positive or negative with decimal


portion can be called as double. Accepts up to 12 decimal positions. The range of
values that accepts a double is:

-1.7E+308 to +1.7E308 double a=10.56874638294

4. Short : holds 2 bytes in memory. Either positive or negative without decimal


portion can be called as short. the range that accepts is -32768 to + 32767.

short int a; 16 0- 65536

5. Long : holds 8 bytes in memory. Either positive or negative without decimal


portion can be called as long. The range that accepts long is:

-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

Out put statement in Java : System.out.println(" Message " + variable );

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.

Integer Float Long Double Character Short Byte Boolean


int float long double char short
Integer.parseInt() --> converts string type into integer type. "10" "CW"
Float.parseFloat()---> converts string type into float type.
Double.parseDouble()---> converts String type into double type.

class sum
{
public static void main(String [ ]x)
{
int a=10,b=20;
int c= a+b;
System.out.println( "The sum is " + c);
}
}

save the above program as sum.java add.java

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);

int -> Integer


float -> Float
double ->Double
char -> Character
long -> Long
byte -> Byte
boolean ->Boolean

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);
}
}

class simple i=p*n*r/100


{
public static void main(String [ ]x)
{
int p=Integer.parseInt(x[0]); 10
int n=Integer.parseInt(x[1]); 20
int r=Integer.parseInt(x[2]);
double i= (p*n*r)/100;
System.out.println("The simple interest is " + i );
}
}

Ex:
c:>javac sum2.java
c:>java sum2 20 40
output: The sum is 60

Operators in java

An operator is used to specify the kind of operation that is going to operate on


the operands. In java the following are the different operators a+b

1. Arithmetic operators: Which are used for arithemetic operations purpose.


a + b a-b a*b a/b a%b 5/2 = 2.5 5%2=1

+ - * / % 5/2 =2.5 5%2= 1 9%3 = 0 10%3=1 5+2=7 5-2 3

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 & 1 = 1 1 & 0 =0 0&1=0 0 &0=0


1|1=1 1|0 =1 0|1=1 0|0=0
And or not 1 0 1&1 1 1|0 1
~1 0 ~0 1
1&0 0
& | ~
1 & 1 1 1 |1 1 1|0 1 0|1= 1 0|0 =0 ~1 =0 ~0=1

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--;

Control flow statement: If is the control flow statement. it is of 3 types


1. Simple if 2. nested if 3. ladder if.
1. Simple if : Which is to compare one expression at a time.
if the given expression is true which executes the statement after if. other wise
control is
passed to else. [ ] () { }

if(expr) if(expr) if(expr)


------; { {
------; ------;
else -----; ------;
------; -----; ------;
-----; ------;
} }
else ----;
{
-----;
-----;
-----;
}

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");
}
}

1. Check the given number is even or odd


2.positive or negative
3. major or minor
4. check Magic number or not

Nested if : with in the if statement we can specify another if statement


called nested if. when the condition fail the control is passed to
corresponding if. The last else is for first if statement. which is for
comparing 2 or 3 conditions.

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)
{

case variable : -----;


-----;
----;
break;
case variable : -----;
-----;
----;
break;
case variable : -----;
-----;
----;
break;
case variable : -----;
-----;
----;
break;
default: ----;
-----;
-----;
}

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");
}
}
}

You might also like