Javanotes
Javanotes
applet
awt
beans
io
net
etc etc
jvm says if it is static method then it is easy for me to not to make the object
and just diectly call the class name
if any data member is static then it can be called by directly class name
C:\Users\miwsyed\Desktop\JAVAPROGRAMS>javap java.io.PrintStream
declarations not done inside the class if they ared defined in the class
HW
create a class family and get the detai over the console the detail of kal family
the detail includes n of members
total income is for monthly for everyone
addresss
then calculate total income
whatsoever class is made public the name of the file should be as same as the class
name or you will get error
access modifiers are not allowed with local variables ; public private.
instance variable
it gets memoery over heap
eag object gas its own set of instance variabbles and they can be
you can print instance variables only when you would have made a object
static variable cannot be called from a non static method //// method should also
be statci if you want to call a static method
local variable must be intitialed with somethin or they will give eroor
static variable get depeendednt there value is changed in the same memory
if there is incrementation in static variable and local variable and new objects
are being made then static will get incremeneted but local variable will be same
data types ;
Byte b; 1 byte ; 8 bits -2^7(128) to 2^7(127) - 1
Short s; 2 byte ; - 2^15(-32768) to 2^15(32767) - 1
Integer i; 4 byte
Long l' 8 byte
Char c ; 2 byte
Float f ; 4 byte
Double d; 8 byte
boolean bo; 1 bit ?/ no one knows
defualt value for char is null
default value for boolean is false
every datatype is cyclic in nature if you give higher value than the limit than it
will cycle back to the starting values or whatever
make record of student take in marks then declare who got first int this
SCANNER CLASS / this calss is present in util and we need to import it
import.util.Scanner
whenever we take input from the user
next() stops after the space is entered and stores that into a variable
nextLine() takes input after that space and stores that into another variable
so we need to print the both variables to print the entire sentence
typecasting when we have to assign large datatype size to small size data type that
is done explicitly
byte= 1;
short s = 2;
int i =3;
long l=4;
char c= 'a';
float f=1.2f;
double d=2.3;
boolean bo=false;
long is 8 and i is 4
i = (int) l ;
byte b = 5;
byte c = b*5;// on getting b into expression b gets promoted to int
will give error because int * byte = int and we are saving it into byte so here we
need to typecast b to run successfully
byte c = (byte)b*5;
and casting is done by the user by putting the desired datatype inside the
parenthesis
CONTROL STRUCTURES ;
if a particular condition is satisfies then perfrom the action or else ignore the
code
conditional statements;
if (condition)
{body}
if else
nested if
if else if
else has no parenthesis
switch ()
statement
break;
LOOPING STATEMENTS ;
for loop
while
do while
for each loop applied on objects
while loop ;
entry control loop
before printing statement condition is checked
ininitialisization
condition
updation
are done in three seperate ways
do while
exit control loop
first do the opereation the check the condition
in this case the body will get exceuted once
its increment is written in the body
we need to put condition inside the parenthesis of while and do while or else it
will give error
if we write true then it will run infinitely
for(int i=0;i<10;i++)
S.O.P("hey");
S.O.P("hello");
for(int i=0;i<10;)
System.out.println("hey");
will give infnitely hey
for(int i=0; ;)
System.out.println("hey");
infintely prints hey
for(; ;)
System.out.println("hey");
also prints infinitely
jumping statements
break;
continue; skips the next iteration below it and goes to the next one like in
attendance we dont want to take the attendance of rollno 8 so we write continue
below 7 then it will go to next that is 9
return;
TYPES OF METHODS
1.method definition
2.method call
actual argument passed when calling the function and in between the parenthesis ;
only the name of variable not definition cuz its been already declared
p sum()
{
string s;
s="shailja"
print(s);
}
return c;
}
and calling should be as
int p = print();
sop(p);// so the returned int is stored
Argument space
we can pass
numbers
aplha numeric
objects
pointers are not provided in java in argument space
when object is thrown from main class function to the original function then in
parenthesis type name of the class then any variable
OPERATORS
assignment operators
arithematic
realtional
logical
unary
shift operator
booean operator
combinational operators
a=a+5;
can be written as
a += 5;
30
-10
0
200
10
SHIFT OPERATORS ;
left shift
right shift
zero filled/ unsigned Right shift operator
whenever you right shift a nev=gative number the answer is one greater than the
actual result
WAP TO CRATE A MENU FOR A RESTAURENT THERE YOU ARE PROVIDIN G THE CUSTOMER TWO
OPTIONS VEGETARIA NAND NON VEGETARIAN
WHEN THE USER SELECTS A DISH THEN THE PRICE WOULD BE SHOWN USING INNER SWITCH ;
SWITCH WITHIN SWITCH
static array
dynamic array
import java.lang.*;
import java.util.Scanner;
class Student
{
Scanner scan= new Scanner(System.in);
String student;
int rollNo;
String section;
void read()
{ System.out.println("enter the name of the student: ");
student = scan.next();
System.out.println("enter the roll number of the student: ");
rollNo = scan.nextInt();
System.out.println("enter the section of the student is : ");
section = scan.next();
}
void display()
{
System.out.println("name of the student is : "+student);
System.out.println("Roll number of the student is : "+rollNo);
System.out.println("Section of the student is : "+section);
}
}
class Information
{
public static void main(String args[])
{
Student obj[] = new Student[4];
for(int i = 0; i <4 ; i++)
{
obj[i]=new Student();
}
h/w
take the detail of studet
his marks in one subject
his name
take all this in main method
for three students
throw this information to some method calculate which belongs to some another class
and the calculate method is adding 5 marks grace to the marks of student and then
returning the incremented marks back to the call area i.e main method /// array of
object as argument
CLASS INHERITANCE
polymorphism
UPCASTING
StringBuffer is not sequential you need to clear the previous value using
nextLine();
enum Day
{
//what evers in it is a object//
SUNDAY , MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY, SATURDAY
}
topic :
LOCALDATE
parse to take about specific date takes string type format (yy/mm/dd/h/m/s/ns)
SINGLETON CLASS
can create only one object in static method and return type should be class type
class A
{
private a()
{A a = null;}
static A read ()
{
if(a==null)
A a = null();
}
return a;
}
class B
{
p sum()
{
A.read();
}
}
ABSTRACT CLASSES
having abstract infront of the class// it is used for data hiding // methods int
his class are just made not code written eg abstract method() {} //we cant make
object of abstract class because
when any class is abstract then it can have an abstract method or not but if it is
then the abstract method should be defined in the child class//its constructor is
protected thats why we cant make objects// for inheritance of a normal class or
abstract class we write implements
for inheritance of interface class we wrtie extends
abstract class ::::: its child class is also abstract then it can provide
definitions to some of the methods of its super class
interface Game
{
void wayofplaying();//this is an abstract method
}
if we dont want to give denition of interface then in child make class as abstract
the child of the interface should have a strong access specifier like public
(public>protected>default>private)
maximum access means is strongest
or we get error of weaker access previlage error comes when stronger to weaker
and to run
we do Animals.run(); // we can directly call static methods and for default we need
to make an object from subclass
can we override static method ??
a class inplemets interface but an interface extends interface but a class extends
another class
interface Vehicle
{
void speed();}
interface Vehicle
{
void speed();
}
interface Car
{
void speed();
void mileage();
}
class Main
{
public static void main(String args[])
{
Car obj = new Lincon(); // for object definition it goes to lincon and
for method it searches in Car
obj.speed();
obj.mileage();
}
}
INNER CLASS
class human
{
in age;
void getAge();
{}
}
}
} // annonymous inner clas because it is made in main
nested Class
when there is a method which has its own attributes in a class we use nested class
to define its own attributes
lambda expression
applicable on interfaces and abstract class
in annynomous class there is even no need to write the name of the class