Core Java by Sanjay Sir
Core Java by Sanjay Sir
---------------------------------------------------------------------
Core Java by Sanjay Sir
---------------------------------------------------------------------
Contents Page No.
------------------------------------------------------------------------------
Main Method
package sample1;
public class demo1
{
//class body
public static void main(String [] args) //main method declaration
{
System.out.println("Hi.."); //printing statement
System.out.println("hello..");
System.out.println("Hi..");
}
}
----------------------------------------------------------------------------------------------------------
package sample1;
System.out.println("Hi");
}
}
-----------------------------------------------------------------------------------------------------------------
Why use methods? To reuse code: define the code once, and use it many times.
1. main method
In any Java program, the main () method is the starting point from where compiler
starts program execution.
So, the compiler needs to call the main () method.
without main method we can't run any java program.
2. Regular method
1. static regular method
1. static method call from same class --> methodname();
2. static method call from diffrent/another class -->className.methodname();
2. non- static regular method
3. non-static method call from same class --> create object of same class
4. non-static method call from diffrent/another class --> create object of diff class
Note: At the time of program execution main method is going to get executed
automatically,
whereas regular methods are not going to get executed automatically.
At the time of program execution priority is scheduled for main method only.
To call a regular method we need to make call method call from main method,
until unless if the method call is not made regular method will not get executed.
Regular methods can be called multiple times.
When You will use Static / Non-Static Method?
Static Method: If You want to maintain Constant Information of object throughout the
classes then static method is used.
Eg: CEO for every employee is same i.e., CEO change then it will change for every
employee.
Non-Static Method: For each object if you want different information then we will use non
static method.
Eg: If Employee Changes Then Employee ID also Changes
}
}
}
---------------------------------------------------------------------------------------------------------------
1. A) static Regular method call from same class
Class : 1st
Class: 2nd
//3. non-static method call from same class --> create object of same class
package Methods;
public class sample4
{
public static void main(String[] args)
{
//classname objectname=new classname(); --> object creation
sample4 s4=new sample4(); // object creation
s4.m5(); //objectname.methodname();
s4.m6();
s4.m6(); //reuse
}
//non-static -->regular method
public void m5()
{
System.out.println("running non-static regular method m5 from same class");
}
//non-static -->regular method
public void m6()
{
System.out.println("running non-static regular method m6 from same class");
}
}
---------------------------------------------------------------------------------------------------------
Class: 2nd
Class 2nd
-----------------------------------------------------------------------------------------------------------------
Document Prepared by: Mr. Vaibhav Yendole 17
Learn Java by Sanjay Sir
//example6: method with parameter--> String parameter
package Methods;
public class sample10
{
public static void main(String[] args)
{
studentName("swapnil");
studentName("abc");
}
public static void studentName(String sname)
{
System.out.println(sname);
}
}
-----------------------------------------------------------------------------------------------------------
//example7: method with parameter--> all types of parameter
package Methods;
public class sample11 {
public static void main(String[] args) {
studentInfo("swati",200,'A',65.5f);
System.out.println("-----------");
studentInfo("amol",250,'B',68.5f);
}
public static void studentInfo(String sname,int sRollNum,char sgrade, float sper)
{
System.out.println(sname);
System.out.println(sRollNum);
System.out.println(sgrade);
System.out.println(sper);
}
}
-----------------------------------------------------------------------------------------------------------
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
-------------------------------------------------------------------------------------------------------------
1. for loop
package Loops; (Ascending Order)
public class example1_forLoop
{
public static void main(String[] args)
{
// 1(start position) 6<=5(End Position) 6 (No of Steps)
for(int i=1; i<=5; i++) // i++ For Increament i with one value
{ // i+2 for Increament I with Two Values
System.out.println(i); // 1 2 3 4 5
}
}
}
---------------------------------------------------------------------------------------------------------------------
Document Prepared by: Mr. Vaibhav Yendole 20
Learn Java by Sanjay Sir
package Loops;
public class example3_forLoop_print_Odd_Numbers_from_1_to_99
{
public static void main(String[] args)
{
for(int i=1; i<=99; i=i+2)
{
System.out.println(i); //1 3
}
}
}
-------------------------------------------------------------------------------------------------------------------
package Loops; (Descending/Reverse Order)
public class example5_forLoop_print_numbers_from_5_to_1
{
public static void main(String[] args)
{
// 5(Start) 0>=1(End) 0(Step)
for(int i=5; i>=1; i--)
{
System.out.println(i); //5 4 3 2 1
}
}
}
--------------------------------------------------------------------------------------------------------------------
package Loops;
public class example6_forLoop_print_odd_numbers_from_99_to_1
{
public static void main(String[] args)
{
for(int i=99; i>=1; i=i-2)
{
System.out.println(i); //5 4 3 2 1
}
}
}
Document Prepared by: Mr. Vaibhav Yendole 21
Learn Java by Sanjay Sir
------------------------------------------------------------------------------------------------------------------
package Loops; (For Tables/ Multiples)
}
--------------------------------------------------------------------------------------------------------------
}
}
----------------------------------------------------------------------------------------------------------------
2. while loop
package Loops;
public class example11_whileLoop
{
public static void main(String[] args)
{
int i=1; //start condition
while(i<=5) //end condition //6<=5
{
System.out.println(i); //5
i++; //6 //increment or decrement
}
}
}
--------------------------------------------------------------------------------------------------------------
package Loops;
public class example13_whileLoop_print_even_num_from_2_to_100
{
public static void main(String[] args)
{
int i=2; //start condition
3. Do While Loop
package Loops;
public class example16_DoWhile
{
public static void main(String[] args)
{
int i=10; //start condition
do
{
System.out.println(i); // 10
i++; //11 // increment or decrement
}
while (i<=5); //end condition
}
}
------------------------------------------------------------------------------------------------------------------
Example on (for loop)
package Control_Statements;
// 25>=35
if(marks>=35)
{
System.out.println("Pass");
}
}
----------------------------------------------------------------------------------------------------------------
4. Nested If
package Control_Statements;
public class example4_nested_if {
public static void main(String[] args) {
String UN="abc";
String PWD ="xyz";
if("abc"==UN) //outer if
{
System.out.println("Correct UN: ");
if("xyz"==PWD) //inner if or nested if
{
System.out.println("Correct PWD: Login successfull");
}
else
{
System.out.println("Wrong PWD--> Login Failed");
}
}
else
{
System.out.println("wrong UN--> Login failed");
}
}
}
5. Switch
package Control_Statements;
public class example5_switch1 {
public static void main(String[] args) {
int inp=8;
switch (inp)
{
case 1: System.out.println("Today is mon: "); // 1 to 7 are the int inputs
break;
Keywords are predefined, reserved words used in Java programming that have special
meanings to the compiler. For example:
Java identifiers
Identifiers are the name given to variables, classes, methods, package etc
2. Global variable:
The variable which is declared outside the method/block/constructor is called global
variable.
Scope of global variable remains through the class.
Global variable is also called permanent variable.
3. Static/Class variable:
1. static variable call from same class --> variableName();
2. static variable call from diff class --> className.variableName();
Note: we can access static global variable in both static & non-static method
package Types_Of_variables;
public class sample1
{
int a=10; //global variable
public void m1()
{
int b=20; //local variable
System.out.println(b); //20
System.out.println(a); //10
}
public void m2()
{
int c=30; // local variable
System.out.println(c); //30
//System.out.println(b);
System.out.println(a); //10
}
public static void main(String[] args) {
sample1 s1=new sample1();
s1.m1();
s1.m2();
}
}
---------------------------------------------------------------------------------------------------------------------
Example2: Static Global Variable Call from Same Class & Different Class
package Types_Of_variables;
}
------------------------------------------------------------------------------------------------------------------
package Types_Of_variables;
}
----------------------------------------------------------------------------------------------------------------
Example3: Non-static Global Variable Call from Same Class & Different Class
package Types_Of_variables;
}
------------------------------------------------------------------------------------------------------------------
package Types_Of_variables;
}
--------------------------------------------------------------------------------------------------------------
Static Global Variable Call from Same Class & Different Class
Types of Constructor
1. Default Constructor
2. User defined Constructor
1. Default Constructor
If Constructor is not declared in java class, then at the time of compilation compiler
will provide Constructor for the class
If programmer has declared the constructor in the class, then compiler will not
provide default Constructor.
The Constructor provided by compiler at the time of compilation is known as
Default Constructor
--------------------------------------------------------------------------------------------------------------------
//Step3: usage
public void div()
{
int divValue = num3/num4; //30/5 = 6
System.out.println(divValue);
}
System.out.println("---------------");
package Constructor;
public class sample4
{
//Step1: variable declaration
int num1; //10
int num2; //20
//Step3: usage
public void addition()
{
int sum = num1+num2;
System.out.println(sum);
}
//Step3: usage
public void mul()
{
int mulValue = num1*num2;
System.out.println(mulValue);
}
}
------------------------------------------------------------------------------------------------------------------
sample6(int a, int b)
{
num1=a; //20 //assign local variable info into global variable
num2=b; //25
}
public void addition()
{
int sum= num1 + num2;
System.out.println(sum);
}
public void mul()
{
int mulValue= num1 * num2;
System.out.println(mulValue);
}
}
package Constructor;
public class sample7
{
}
Example of With Parameter Constructor (Multiple Constructor) (Class:2)
package Constructor;
public class sample8
{
}-------------------------------------------------------------------------------------------------------------------
package Static_non_Static_Use;
-----------------------------------------------------------------------------------------
package Static_non_Static_Use;
public class static_use
{
public static void main(String[] args)
{
}
---------------------------------------------------------------------------------------------------------------------
Impact of Using Static and Non Static Variables
Document Prepared by: Mr. Vaibhav Yendole 49
Learn Java by Sanjay Sir