Java q Spider
Java q Spider
Conventions: -
Convention or company stands flowed to write the java program
To write a class name we followed by convention name “upper camel case” (every word 1st letter should be
capital)
Comments: -
Comments are the not executive lines we write and this lines or hidden form the compiler
In comment we have 2types
Single line comment
// Used to hide one line
Multi line comment
/* */ used to hide multi lines
Variables
variable TO container to store in single values
Data types: data type is used to specify what type of data to be store inside a variable
Variable declaration
Syntax: data type variable name ;
EX: Int id;
Variable initialization: syntax
Variable name = value;
Expel: id = 1;
Variable declaration + variable initialization
Syntax: data type variable name = value;
Expel: int id = 1
Primitive type costing: - - converting one type of single value to another type is known as primitive cost
type
Note: the order of data is
bye>[shot<char]<int<[long<float]<double
Widening: - converting smaller type of data to larger type know as widening in this widening process the owned to be
or there not be loss any data
It is implicit process: - compiler done work knows implicit process
Explicit: - programmer done work is known as explicit
Class A
{
Public static void main ( ) {
Int a = 10;
Double b = a;
System.out.println (a);//10
System.out.println (b);//10.0
}
}
Narrow type costing: - converting larger data type to smaller data type is known as “narrowing”
With the help of narrowing, we should forcefully store large type of value in small container and this is process which
is done by the programmers is known as explicit process
When we archive narrowing thought a type costing operator and that is ( ) (parenthesis)
EX: - class A {
Public static void main (String [] rags)
{
Int a = 65; widening
char b = (char)a;
Int c = b; narrowing
System.out.println(a);
System.out.println, (b);
System.out.println(c);
What is operators
Operators or free defined symbols do some particular task
Types of operators
Athematic operators
Assignment operators
Unary operators
Relationally compiler operators
Logical operators
Ternary operators
Athematic operators: - it’s a binary operators consist of two operands one operators
It’s used to do some takes using this symbol (+ - * / %)
Assignment operators: - It’s al so binary operators used to assign values
Unary operators: - it’s operators wherein we have single operators
Unary operators we have 2types
Increment: 1.pre increment 2. post increment
Pre increment (++A)
Post increment (A++)
Decrement 1.pre decrement 2. post decrement
Pre decrement (--A)
Post decrement(A--)
A B AND
T T T
T F F
F T F
F F F
Relational or compiler operators: - it is binary operators where in it returns Boolean time data operators which we use or
< > <= >= ==
Not A
Logical operators: - logical operators also binary operators Boolean return symbols || && ! T F
F T
|| &&
!
A B OR
T T T
T F T
F T T
F F F
Ternary operators: - Ternay Has 3operands based on the given conduction it will give you Boolean result true or falls
based on that result it will bind to the option like then it is true option one will be the result when condition is falls
Option data type should be same as well as result container should also have same type of data
Syntax: - (condition)? ‘’option1 “: “option2”;
Class Vote {
Public static void main ()
{
Int age = 25;
String result = (age <=21)? “eligible”: “not eligible”;
System.out.println (result);
Buzz no: - any number that is divisible by 7 or the last digit of a number is 7
Class Buzzi {
Public static void main (String)
{
Int num = 28;
String result = (num%7 == 0 || number / 10== 7) ? “ Buzzi no ”: not “buzzi”;
System.out.println(result);
}
}
Decision making statement: - This are used to print same set of statement based on the condition and decision-making
statement are used in deferent scenarios based on the requirement and the condition
types of decision-making statement
Simple if
If else
If lese if/else if ladder
Nested if
Switch case
Simple if: - It operate based on the condition and if the condition is true it is go to print some set of instructions and if
it is false not going to print anything because it only exhibits positive scenarios not the negative
Syntax: - if (condition) {
s.o.println( )
}
If else: - In if else we have both positive and negative scenarios
Class Num{
Main( )
Int num = 11;
If ( num % 2 ==0){
System.out.println(“the num is even”);
}
else {
system.(“ the given num is odd);
}
}
}
If else is: - if else if you want check 2 or more condition then we will use if lese if
Class A {
Main () {
If (condition1) {
Statement1
If else (condition2) {
Statement 2
}
}
O 90
A 70 -90
B 50 -69
C 35 – 49
F 0 – 34
Syntax: -
Class {
Main ( ) {
Int marks =85;
Looping statement: -
If you want to do a particular task repeated time we go for looping statement
There are 5 types
While loop
Do while
For loop
Nested loop
For each
For loop: -
Syntax:
For ( initialization :condition ; updation){
s.o.pln.();
}
Ex:
For ( a =10; a<=100;a++){
s.o.pln(a);
}
While loop : while ( condition ) {
S.o.pln( a);
a++
}
Ex:
Methods:
Method is block of code to do some specific tasks it is also called as set instruction to do some specific task
Method also called as functions
Advantages of methods:
1. Code reusability
2. Code modularity
1. Code reusability: we will write the code once and use it many numbers of times due to this there will be the
reduces in memory consumption
2. Code modularity: using methods we keep the code in to smaller modules what it we will be easy for the updates
as well as for testing we no need to test the while application if we update the only modules which are updated
needs to be tested
EX: for a method:
Public static void main (String [] args)
Syntax of method: [ Access modifier] [modifier] [return type] [method name] [formal arguments]
{
System.out.println()
}
1. Access modifier:
Access modifier are used to specify the accessibility of a java member we have 4 types
Public
Protected
Private
Package level (default)
2. Modifiers:
Modifiers is used to specify the modularity in modifies we have 2 types
1. Static, 2. non-static
Modifiers also called as non-access modifies
1. static: static means one copy for class
2. non- static: non- static means multiple copes based on the object created
3. Return types: return types show cases types of data return by a method it’s has 2types
1. with return types 2. Without return types
1. with return type has all the types of data to be return
2. without return type we used void
4. Method name: method name is identifier the name which we give for a method is method name and we follow
lower canal case syntax to write a method name
5. Formal arguments formal argument we can write variable declaration array declaration, object references in
formal arguments and it has 2types
1. with formal argument 2. Without formal argument
Method terminologies: method definition method signature
access modifier method returns types[ method name (formal argument)]
{
Method block/method body/method implementation
}
}
Static variable
s.o.pln ( A.x);/10 Static method
1). main method ()
JVM 2). Others ()
Class A{ Main area Class static area Static area Help area
Static int x=10; Main ( ) ox1 A()
Int y= 20; s.o.pln(A.x); Int x 0 10
Public static void Main () ox1
main(){
s.o.pln(A.x);//10
A a =new A();
s.o.pln(a.y);//20
}}
Create class and have 2 static properties and 2 Non- static properties inside main method and call all 4 properties and
showcase the execute process
Constructor: - constructor is special type of method where in class name as constructer name
constructer are used to initialized object and use to execute line of code (or) set of instructor whenever the object
created and it is mainly used to load initialized and register Non-static properties
constructor does not have concept of static and non-static
constructor don’t have “return type”
if you write a return type it will become “method”
we can’s write return statement inside a constructer
EX: class A{
A(){
s.o.pln(“object created”);
}
Main( ) {
A a=new A();;
New A();
New A() Abundant object means the object are created but not stored
}}
Calss car {
Int regno;
Private int num=1000; 1. Load
Car (){ 2. Initialize
3. Register
s.o.pln(“car is purchased”);
regno = num;
num++;
Out put //
S.o.pln(“ registered” num+regno);
Car purchased
}
1000
Public static void main( ) {
Car purchased
Car c1=new car();
1001
Car c2=new car ();
}}
MA CSA SA HA
Types of constructors: -
There are 2 types of constructors 1. Default constructor 2. Custom/ user default constructers
1. default constructor:
the constructor which is added by the compiler implicitly known as “default constructor”
default constructor does not have any formal arguments (FA)
the access modifier depends on the class access modifier
2. custom constructer
the custom constructer which is added by the programmer is known as “custom constructer”
in custom constructor we have 2 types
parametrized
non-parametrized
a. parametrized: - constructer added by the programmer explicitly with formal argument is know as “parametrized
custom constructer”
b. non- parametrized: - constructer added by the programmer without any FA is known as “ non-parametrized”
create class employee have 2 static variable 2 non-static variable 2 static methods and 2non-static method and inside
main method call all the properties and showcase execute process ?
class Employee{
static int id=6;
static string name=”mahi”;
long mobile num=9876543211;
int age = 22;
public static void m1(int id){
s.o.pln(6);
}
Public static void m2( string name){
s.o.pln(“mahi”)’
}
Public static void m3(long mobile number){
s.o.pln(9876543211);
}
Public static void m2 ( int age){
s.o.pln(23);
}
Public static void main( String [ ] args ){
S.o.pln( empid);
Employee.m1(int id );
Employee.m2(String name);
Employee e1.new employee ();
E1.m1();
Employee e2.new employee ();
E2.method2();
Constructor:- it is used to initialize the non-static variable
EX: Class Employee{
Int id ;
Employee(int i){
Id = i ;
}
Public static void main( ){
Employee A1=new employee(1);
S.o.pln(A1.id);
Employee A2=new employee(2);
S.o.pln(A2.id);
}}
Create class student and have 3 non-static variable names, id, age initialize this which the help of custom constructer
inside main method create 3 student object and with each reference call all the properties
Class Student {
String name;
Int id;
Int age ;
Employe( string n, int n1, int n3)
name=n;
id=n1;
age=n3;
Constructer over loading: - in a class having two are more constructers with deferens in the formal arguments is known as
constructure over loading.
Rules: -
number of formal arguments should be same
Number FA should be same data type should be deferent
Data type should be same position data type should be deferent
EX: class A{
A(){
s.o.plnt( “A() );
}
A( int a) {
s.o.pln( “ A(int a)”) ;
}
Main( ) {
A a= new A();
A a1= new A ( 10);
}}
EX:
EX: public class OUT_put {
int i;
int j;
OUT_put(){
i=10;
}
OUT_put(int n){
j=n;
}
public static void main(String[] args) {
OUT_put a=new OUT_put();
OUT_put a1=new OUT_put(20);
System.out.println(a.i);
System.out.println(a.j);
System.out.println(a1.i);
System.out.println(a1.j);
}
}
EX:
public class Box {
int width;
int lenght;
int depth;
String color;
Box(String color){
this.color=color;
System.out.println("box is create");
}
Box(int n);
width=n;
lenght=n;
depth=n;
System.out.println("box is created");
}
Box(int width, int lenght,int depth, String color){
this.width=width;
this.color=color;
this.depth=depth;
this.lenght=lenght;
}
public static void main(String[]args) {
Box b1=new Box("black");
Box b2=new Box(10);
Box b3 = new Box (10,15,30,"white");
b1.display();
b2.display();
b3.display();
}
void display() {
System.out.println(width+ " "+color+""+depth+""+lenght);
}
}
Constructer chaining: - one constructer calling another constructer is known as constructer chaining, we can archive this
2ways that is using this (); super ();
This ();
This (); call is use to call the constructer present in the same class
This (); should be the fast executable line inside constructer
We can’t write more than one this( ); inside on constructer
Ex: class A {
A() {
this(1); Out put
1
s.o.pln(1);
}
A(int a ) {
s.o.pln(2);
}
Main(){
A a = new A();
}}
Oops
Inheritances: - aquiring property from super calls to sub class with the help of extends key word is known as inheritances
The class which donates the properties is known as super class
The class which accepted the property is called as sub class
Super class is also known as parent class and also is known as base class
Sub class also known as child class (or) derived class
Inheritances archive using extends key words
EX:
TYPES OF INHERITANCE
1. Single level inheritance
2. Multi level inheritance
3. Hierarchical inheritance
4. Multiple inheritance
5. Hybrid inheritance
1.Single level inheritance : acquiring properties from single parent class to single child class this known
single level inheritance
Ex:
package org.training; EX:
Mobile class
I phone 16 class
String Mname="Hyryder";
int Mprice = 100000;
void m1() {
System.out.println("hi");
}
void m2() {
System.out.println("hlo");
}
}
2.Multi level inheritance : combination of super classes and sub classes from top to bottom sequential way is known
as Multi level inheritance
public class A { EX:
Class A
int nprice=100;
String nname="spider man nodes";
void nodes() { Class B
System.out.println("nodes");
} Class C
}
public class B extends A{
int nprice1=200;
String nname1="Bat Man nodes";
void batman() {
System.out.println("Bat ");
}
}
public class C extends B{
int nprice2 = 150;
String nname2= "Iron Man nodes";
void iron() {
System.out.println("Iron");
}
public static void main(String[] args) {
C c1 =new C();
System.out.println(c1.nname);
System.out.println(c1.nprice);
System.out.println(c1.nname1);
System.out.println(c1.nprice1);
System.out.println(c1.nname2);
System.out.println(c1.nprice2);
c1.iron();
c1.batman();
c1.nodes();
}
}
3.Hirachical inheritance : having one parent class two or more child classes is known as hirachical inheritance
public class vechical { EX: parent class
int bcost= 10000; Vehicle class
int cprice=1900000;
void vechicalcondition() {
System.out.println("condition is good and nice"); child class child class
} Bike class Car class
}
syntax :
super.variable name ;
this syntax we will used to call the global variable present in the super class and it can be used non static variable as well
as non private variable
inside static method of static context, we cannot used super key word and this key word
ex :
Inside static method or static context we can’t use super keyword (or) this key word.
call to super :
call to super is use to call the super class constructor.
Multiple inheritances: multiple inheritance having two or more parents one child class is known as
multiple inheritances parent class parent class
Ex: Class A{ Class B{
} }
Child class
Class A{
Int = 10;
Create class
Blocks :
Blocks are used to initialize to these blocks are also multi line initialize in a class we cannot declare a variable and we
cannot initialize because it will lead error
Ex: class A{
Int x ;
x = 20; Output: -
main( ) { Compile time error
s.o.pln ( x);
}
To Initialize we have block and in blocks we have two types
1. Static
2. No-static
Static block:
In static blocks we cannot initialize static variable
In Static blocks we cannot initialize no-static variable with static key word
In static blocks we cannot initialize non-static variable
Static blocks are the one which are pre fixed with static key word
Static blocks are execute before main method from top to bottom sequentially
We can write “ n “ number of static blocks as for requirement
We can create object in side static blocks we can run loops, decision making statement inside the static blocks
Non-static blocks
Non-static blocks use to initialize non-static variable as will as we can initialize static variables
This are not prefixed with static keyword
It will be execute whenever we crate object top to bottom squatly
Whenever we create object in side non-static blocks it is not compile time error but it will result in “ run time
error”.
@Over ride
WhatsApp version.1 {
Void message {
s.o.pln( “text message “) ;
}
Void calls {
S.o.pln( “no calls” ) ‘
}
Access modifier :-
@Override it was interduces from JVM 1.5 it is used with override method we write @Override before
writing over method
EX:
@Override
Void m1(){
S.o.pln();
}
When we write @Override to sub class method it will throw error if the same method is not present in
super class
While we override programmer can do mistakes while writing the sub class method to identify this we
use @override
down casting :
Ex:
Class A{ Class B extends A{
Int x =10; Int y=20;
Main( String[] args)
} {
A a = new B();
B b= (B) new a;
S.o.pln (b.x);
s.o.pln (b.y);
}
}
Down casting: -
Costing super type references to sub type is known as down casting.
Using super type references sub class sub type specify properties can’t be referee to refer sub type
specific properties
Super type references should be down casted to subtype
Down costing is explicitly to archive down casting up casting is mandatory
To archive up casting inheritances in mandatory
With up casting s we can only refer super class properties
With down coating we access both super class and sub class properties
A a = new B ( ) ;
B b = ( b) a;
Instanceof operators : -
instances of operators check whether the object belong to the specific class type or not .
Syntax : - reafferences instanceof type/class name
a instances of B.
It return “true” if the references variable is instances of the given type of run time
Ex:
Class A{ Calss B extends A{ Class C extends A {
Int x =10; Int y = 20; Int z = 30;
} } }
Public static void main ( String []args)
{
A a = new A( );
B b = new B( );
C c = new C( );
S.o.pln ( a instance of A); // true
S.o.pln ( b instance of B); // true
S.o.pln ( c instance of C); // true
Class cast exception
Calss B extends A{ Class C extends A {
Class A{ Int y = 20; Int z = 30;
Int x =10; } }
} Public static void main ( String []args)
{
A a = new B();
If ( a instancesof B ){
B b = (B) a;
s.o.pln( 1);
if ( a instancesof C){ output:-- 1
C c = (C) a ;
s.o.pln(2);