Tuesday, August 31, 2021

# 15. Local Variables

 In order to access or test non-static variables and non-static methods, there is only one approach called 1. through the object

In order to access or test static variables and static methods we have 3 approaches

1. directly by calling their names

2. through the class name

3. through object


We have total 4 types of variables

1. static

2. non-static

3. local variables

4. object/reference type variables


3. local variables(method level):


public com.rameshsoft.automation.corejava;

public class ExamDemo{

        int id=456;        // within the class we can access anywhere

public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}


public class ExamDemo{

        int id=456;        // within the class we can access anywhere

        private int practiceHrs=9;        // within the class we can access anywhere

// we cannot access outside the class 

//why because it is private


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}


public class ExamDemo{

        public int id=456;        // within the class we can access anywhere

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere

// we cannot access outside the class 

//why because it is private


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}


>>>public class ExamDemo{

        public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private


public void hello() {

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}

If the variable is public the scope is anywhere within the class or outside the class anywhere.

If it is private also within the class


Local variables:

1. when do we go to local variables

whenever we want to restrict the data to a certain portion of the area then we will go for local variables.

that portion of the area is nothing but a method here

whenever we declare the variables inside the methods such types of methods are called local variables

2. when we can say a variable is a local variable

whenever we declare the variables inside the methods such kinds of variables are called local variables.


>>>public class ExamDemo{

        public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private


public void hello() {

        int salary=250000;        // local variable

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}


1.How many local variables do we have within the class?

one

 int salary=250000; // declaring


>>>public class ExamDemo{

        static public int id=456;        // within the class we can access anywhere, static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private


public void hello() {

        int salary=250000;        // local variable

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}

 whenever we declare variables inside the methods called local variables.


>>>public class ExamDemo{

        public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private


public void hello() {

        id=91456;        // not local variable, reassigning the value

        int salary=250000;        // local variable

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}

// there is only one local variable in the above program

// id - we are using declared variable


>>>public class ExamDemo{

       static public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private

        double id=456.456;        // invalid

// variable name should be always unique


public void hello() {

        id=91456;        // not local variable, reassigning the value

        int salary=250000;        // local variable

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}


>>>public class ExamDemo{

       static public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private

        double id=456.456;        // invalid

// variable name should be always unique


public void hello() {

        int id=91456;        //  local variable, reassigning the value

        int salary=250000;        // local variable

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}

// can have class level variable names and local variable names same.

// we have two local variables


>>>public class ExamDemo{

       static public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private

        double id=456.456;        // invalid

// variable name should be always unique


public void hello() {

        int id=91456;        //  local variable, reassigning the value

        int salary=250000;        // local variable

// priority always local

        System.out.println(id);        // 91456

        System.out.println(salary);        //250000

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}


>>>public class ExamDemo{

       static public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private

        double id=456.456;        // invalid

// variable name should be always unique


public void hello() {

       // int id=91456;        //  local variable, reassigning the value

        int salary=250000;        // local variable

        System.out.println(id);

// here we dont have id then priority goes to class and execute.

}


public void hello1() {

        System.out.println(id+practiceHrs);

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}

the scope of local variable is within the method only. outside the method we cannot access.

>>>public class ExamDemo{

       static public int id=456;        // within the class we can access anywhere, non-static variable

// out side the class also we can access

        private int practiceHrs=9;        // within the class we can access anywhere, non-static variable

// we cannot access outside the class 

//why because it is private

        double id=456.456;        // invalid

// variable name should be always unique


public void hello() {

        int id=91456;        //  local variable, reassigning the value

        int salary=250000;        // local variable

        System.out.println(id+practiceHrs);

}


public void hello1() {

        System.out.println(id);

        System.out.println(salary);        //no salary in class then compile time error

        System.out.println(practiceHrs);

// if variable is not there inside the method then it will directly go to class level

}

// we can access this id and practicehrs within the class anywhere


public static void main(String[] args) {

        ExamDemo examDemo= new ExamDemo();

        System.out.println(examDemo.id);

}

}

// outside the method we cannot access local variables otherwise compile time error occur.


when do we go for local variables?

whenever we want to restrict the data to certain portion of the area then go for local variables

if you want to restrict the data to certain portion of are that is within the method only then we can go for local variable.

when we can say variable is a local variable?

whenever we declare the variables inside the methods called local variables

the scope of the local variable is within the method only, outside the method we cannot access local variables.

we can have class-level variable names and local variables names as same.

if we are trying to use within the method the priority always going to local


if we don't perform initialization of static variables  and non-static variables we will get default values


initialization of local variables is compulsory otherwise compile-time error occurs

initialization of local variables is mandatory if and only if whenever we are using that variable

public void hello() {

        int id;        // no initialization

        int salary=25000;

  System.out.println(id);        // here we are using local variable so we will get compile time error

}


public void hello() {

        int id;        // no initialization

        int salary=25000;

 // System.out.println(id);        // here we are not using local variable so no error

}

for local variables default values concept is not applicable, mandatory we should perform initialization

whenever we declare the variable inside the method then those variables are called local variables


for local variables no access specifiers/ modifiers

for local variables, there is only one modifier/ keyword is applicable

1. final


final is allowed to stop reassignment of that variable

this is a keyword that never goes to a method of local directly goes to class

initialization should be inside the method only

object type or reference type variabel:

object type or reference type variable ia a variable that always contains object

In how many places we can create objects:

In general, we can create the object in two places

1. class level ---> non static OT/RT variable

2. method-level---> static OT/RT variable














Saturday, August 28, 2021

# 14. java source file structure

 If we want to execute the program in java compulsory jvm always starts the execution from the main method.

whenever the programmer asks the jvm to run the program jvm always looks for main method.

main method is the starting point for the program.

If we dont define main method immediately we will get compile time error.

If there is no main method we cannot execute the program


JAVA Source file structure:

Source file is nothing but where we are going to write our programs

1. In a single source file we can have multiple classes/ class files

>>> class A1{

------

}

class A2{

-------

}

classA3{

-------------

}

2. If no class is declare as public then we can save that file with any name

A1.java

A2.java

A3.java

A4.java

Hello.java

Anything.java

3. It is a good programming practice that declare one class as public, the class which we declared as publice the public class and filename must be same otherwise we will get compile time error.

>>> class A1{

------

}

public class A2{

-------

}

classA3{

-------------

}

Ex: A2.java ===>valid

A1.java ===>invalid

A3.java===> invalid

Hello.java ===>invalid

4. If we are trying to define more than one class as public immediately we will get compile time error

  In a single source file we should not take more than one class as a public.

>>> class A1{

------

}

public class A2{

-------

}

public classA3{

-------------

}

// we will get compile time error

5. The class which we declare as a public in that public class only we must and should take main method.

>>> class A1{

------

}

class A2{

-------

}

public classA3{

public static void main(String[] args)

{

-------------

}// end of main

}// end of class


Benifits of ide/eclipse:

We need to focus on 

1. perspective(on what perspective u want to write the programs)

2.eclipse editor (where we are going to write our programs

3. console (where we can see the output)

4. project explorer/package explorer (where we are going to manage our projects)

5. git

6. debug


Eclipse:

right side click on + symbol

select java(default)

firse we need to create a project

goto file--->new--->project--->java project--->next---> projectname and version--->next--->finish

project got created

1.jre system library

2. src (source folder where we can write our programs)

second way to create project :

goto window---> show view--->click on console---> window--->show view--->package/project explorer

Whenever we create a project two files are going to be created automatically

Assume that the Flipkart application

we have three modules

1. men-------> 25 classes

2. women----> 20 classes

3. kids-------->45 classes

under the men module, we need to write  25 classes like that

src---> class names like men1,men2,women1,women2,kids1,kids2 etc

we have taken 90 classes

if we want to execute only kids classes then we need to look for those classes in 90 classes

if we want to look only men classes then we need to look for men classes out of 90 classes

its become complex

for example under kids module if we have different types of chacolates like 100 5star,150 kitkat,200 dairymilk, etc

if all are combined together in one box.

instead of that if we put them separately in boxes its very easy to get them.

in the same way dont write all your programs under src folder.

under src folder create packages

package is a collection of classes + interfaces+enums.

Standard syntax for package:

package <packagename>

package is a fixed keyword inorder to convey it as a package

pacakagename: can be anything but it should be very meaningful.

package men;

inside this men all the program related to men

package women;

package kids;

But this above are not valid in real time we have some standard sysntax:

domain name in reverse. modulename. submodulename

www.flipkart.com

package com.flipkart.men;

src---> new--->package

give some name like 

package com.flipkart.men;

package com.flipkart.women;

package com.flipkart.kids;


now to write all men program select this package--->new-->class---> give class name

how many classes we can write all classes

men1,men2,men3,women1,women2,women3,kids1,kids2,kids3,....... etc...

to execute kids class simply look for kids package

To run: rightclick--->run as java application

package com.flipkart.kids;

public class kids3{

 public void hello() {

 System.out.println("hello");

}

public void job(){

System.out.println("job");

}

public static void main(String [] args) {

  System.out.println("hello");

}

}

















 








Friday, August 27, 2021

# 13. main method

 In order to print some information on the console, in java, we have a method called 

System.out.println() method.

Standard syntax:

System.out.println();

>>>class Demo{

int a1=456, a2= 789;

static String name="RAMESHSOFT"

public int addition()

{

int result =a1+a2;

return result;

}

}

Ex:

int a1=456, a2= 789;

static String name="RAMESHSOFT"

Syntax:

1.System.out.println(String message);

System.out.println("Please do practice and it is mandatory");

Whatever we write in double-quotes as it is it will print on the console.

2. To print variable values on the console

System.out.println(variablename);

To print more than one variable

System.out.println(varname1+varname2+varname3+.......);

>>>System.out.println(a1);

output: 456

>>>system.out.println("a1");

Output: a1

>>> System.out.println(a1+a2);

here addition is going to be happen

output: 1245

here + is acting like a addition

System.out.println(a1+name);

output: 456 RAMESHSOFT

concatenation happened.

here + is acting like a concatenation

In java the only one overloading operator is + , sometimes it acts like addition and sometimes it acts like concatenation

>>> System.out.println(a1+name+a2);

Output: 456 RAMESHSOFT 789

>>> System.out.println(a1=a2+name);

Output: 1245 RAMESHSOFT

3. To print some message along with variable value

syntax:

System.out.println(String msg+variablename);

or

System.out.println(variablename+String msg);

>>> System.out.println("The value of a is:"+a1);

>>>System.out.println(a1+" The value of a is");

Output: 

The value of a is: 456

456 The value of a is

4. To print morethan one variable with some message

System.out.println(String msg1+variablename1+ String msg2+variablename2);

or

System.out.println(variablename1+String msg1+variablename2+String msg2);

Example:

>>> public class Addition{

public int numberOne=45;

public int numbertwo=47;

public int addition()

{

int result=numberOne+numberTwo;

return result;

System.out.println("result is: "+result);    

}

}

Whenever we compile this program we will get a compile-time error because after the return statement we should not write any statements.

A method should not contain more than one return statement.

per method we should write only one statement otherwise we will get a compile-time error.

return statement should be the last statement inside a method.

>>> public class Addition{

public int numberOne=45;

public int numbertwo=47;

public int addition()

{

int result=numberOne+numberTwo;

System.out.println("result is: "+result);  

return result;  

}

}

It will compile without any errors.

In the above program inside the method, we implemented the business logic of addition between two numbers.

Whenever we call this method it is going to be performed addition between two numbers and return result.

1. We implemented one business logic then we need to testing that it is working correctly or not.

2 in order to write a program in java we need to follow 3 standard steps

2.1 write a program

 save with .java extension

2.2. validate/compile

2.3. execute

In order to execute the java program standard syntax is

<javac>    <class name>  .java

To execute standard syntax is

,java>    <classname>

>>> javac Addition_Demo.java

>>> java Addition_Demo

The moment we click on enter here JVM is going to start. once JVM is started in this desktop JVM check this .class file is there or not.

JVM loads the .class file, whenever JVM is loading the .class file automatically all the static variables and static methods are going to be created.

JVM is going to look execution 

Whenever u ask the JVM to run the program JVM is going to look for the starting point to execute the program

Ex: to start a car we need a key point

All the static blogs are going to be executed automatically.

1. check for the class file

2. JVM looks for .class file

3. loads the .class file

4. JVM looks for execution point

What is the execution point?

Jvm is always going to starts the execution from the main() method.

the main method is the starting point for the execution

If the main method is not there we will get an error.

What is the main method in java?

In java, the main method is there for two reasons

1. whenever u ask the JVM to run the program, JVM always starts execution from the main method.

why?

JVM is internally configured in such a way that whenever the programmer is asked to run the program always starts the execution from the main method.

if the main method is not there JVM doesn't know where to start the execution that's why it's throwing a compile-time error.

* In order to execute a program, we need a main method

2. Inside the main method what we will do?

We will do testing

How to define the main method:

Standard syntax:

1. public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}


2. static public void main(String[] args)

{

// accessing or testing the implemented services or methods

}


3. public static void main(String... args)

{

// accessing or testing the implemented services or methods

}

In place of the args whatever we want we can write

4. public static void main(String[] RAMESHSOFT)

{

// accessing or testing the implemented services or methods

}

5. public static void main(String... RAMESHSOFT)

{

// accessing or testing the implemented services or methods

}

/* In place of [] we can write ... three dots

6. strictfp public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

All the above main methods are valid but commonly we use first method

Main method is bydefault static method.

Where we need to define the main method

1. After starting the class immediately take main method

<as>    class   <classname>  {

public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

;;;;;;;;;;;;;;;;;;;///defined or implemented part

;;;;;;;;;;;;;;;;;

}

2. Before end of a class take main method

<as>    class   <classname>  {

;;;;;;;;;;;;;;;;;;;///defined or implemented part

;;;;;;;;;;;;;;;;;

public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

}

3. In between the class take main method

<as>    class   <classname>  {

;;;;;;;;;;;;;;;;;;;///defined or implemented part

;;;;;;;;;;;;;;;;;

public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;; //implemented or defined part

}

4. Take main method in seperate class

<as>    class   <classname>  {

;;;;;;;;;;;;;;;;;;;///defined or implemented part/services part

;;;;;;;;;;;;;;;;;

}

<as>    class   <classname>  {

}

<as>    class   <classname>  {

public static void main(String [] RAMESHSOFT)

{

// accessing or testing the implemented methods / services

// execution part

}

}


>>> public class Addition_Demo{

public int numberOne=45;

public int numbertwo=47;

public int addition()

{

int result=numberOne+numberTwo;

System.out.println("result is: "+result);  

return result;  

}

public static void main(String[] RAMESHSOFT)

{

System.out.println("JVM started execution")

}//end of main method

}//end of class

there is nothing to execute

.

When this above method is going to be executed?

r u calling that method?

no

so it won't respond

We implemented one business service we need to call it

>>>javac Addition_Demo.java

>>>java Addition_Demo


public static void main(String[] RAMESHSOFT)

public void main(String[] RAMESHSOFT)

if we remove static we will get a compile-time error.

public static void main(String[] ...) ===> valid



































Thursday, August 26, 2021

# 12. non static method

 non-static method:

whenever the business implementations are not constant then we go for the non-static method.

standard syntax:

1. <access specifier>    void      methodname()

{

;;;;;;;;;;;;;;;;;;;;;;;

}

2. <access specifier> return type methodname()

{

;;;;;;;;;;;;;;;;;;;;;;;

return <statement>;

}

BR: WAP for the addition of 2 numbers

45+45=90

/* using void type

public class Addition_Program{

int num1=45;

int num2=45;

public void addition_Program()

{

int result=num+num2;

}

}

/* Using return type:

public class Addition_Program{

int num1=45;

int num2=45;

public int addition_Program()

{

int result=num+num2;

return result;

}

}

float, long, double are valid because they are higher than int

byte short is invalid.


Monday, August 23, 2021

# 11. static method

 Method:

Depends upon the data behavior we have two methods.

1. Static :

------------

When do we go for static methods?

case 1: Whenever the business implementations are not going to change or constant then we will go for constant

case 2: Whenever we want to create read-only services

case 3: Without creating an object we want to access services

2. Non-static:

-----------------

when do we go for non-static methods?

Whenever the implementations are not constant then we will go for non-static.


How to define a static method:

Syntax:

<access specifier>    static    void    methodname(--------)

{

------------------

}

  static   <access specifier>    void    methodname(--------)

{

------------------

}

<access specifier>    static    <return type>    methodname(--------)

{

------------------

return <stmt>;

}

  static   <access specifier>    <return type>    methodname(--------)

{

------------------

return <stmt>;

}


Access specifier is not mandatory, it's optional

Specifying access of a class

If we want to put some restrictions on our business method

Static: 

static is a keyword to convey it as a static method

Void:

it returns nothing

whenever we are keeping the method void, the business implementations are going to be executed but in the end, it won't return anything

Whenever you are keeping return type after executing the business implementations it's going to return some values.


Method name rules:

every method must and should have some name

it should start with a small letter

it should not start with numbers or special characters

method name should be clear and very meaningful

if a method name has multiple words every first word should start with a capital letter except the first word

Ex: addition_Program

As a part of the return type what should allow?

void is allowed

primitives like int, short, byte, long, float, double, boolean, char, 

class type as string allowed

interface type also allowed


<access specifier>    static    <return type>    methodname(--------)

{

------------------

return <stmt>;          /* is mandatory

}

return <stmt>; =======>

whenever our method is having a return type then the return statement is mandatory

we should not write anything after the return statement.

the return type is always based on return statement

return type should be same types as return statement of integer or higher than integer

if we are trying to return lower than integer then we will get compile time error

return statement should be the last statement inside the method otherwise we will get compile time error.

After the return statement, we should not take any statement otherwise we will get a compile-time error.

if we want to return some values then go for return type

if return statement is returning some type then the return type should be same or higher than that type.

but not lower otherwise we will get compile time error.

For the business method, we should take only one return statement we should not take more than one otherwise we will get a compile-time error.

Real-time approach:

ATM:

BR: Cash deposit:

----------------------

savings balance=25000

insert the atm card

valid card or not

if it is invalid===> invalid card

card is valid

enter the account number

insert the amount

it goint to save the amount

amount is deposited

25000+10000=35000

savings balance=35000

After the cash is deposited we want to return the savings balance

case1: if you want to return the savings balance at the end of the business implementations then go for <return type>.

case2: if you don't want to return the savings balance then go for <void type>.

For example take a calculator and perform 25+20 then it will return 45 value.


loginTestDemo() ===> valid

for more readability differentiate with _

login_Test_Demo() ===> more readable


class Demo{

int num1=25;

int num2=65;

or

int num1=25, num2=65;

public static void additionProgram()     /* it wont return anything because void

{

int result=num1+num2;

}

}

if we want to see the result or return something then

class Demo{

int num1=25, num2=65;

public static int additionProgram()     /* it wont return anything because of void

{

int result=num1+num2;

return result;

}

}

Lower to higher:

byte ===> short ===> int ===> long ===> float ===> double 

                                    char

int can hold char long float double 

int cannot hold byte short





















Saturday, August 21, 2021

# 10. Methods

 BR: WAP for the addition of two numbers

public class Addition_Program {

int number_One=45;

int number_Two=47;

number_One + number_Two =====> whenever we add these two numbers we will get 97

Here 97 is also data that should be placed inside a container called a variable.

We need to declare a new variable to place that value

97 is of type integer

}

----------------------------------------------------------------------

public class Addition_Program {

/* These two lines are like ingrediants

int number_One=45;

int number_Two=47;

/* business logic/ implementation

int result = number_One + number_Two

}


Now compile and execute

>>> javac Addition_Program.java

But this business logic/implementation of a business requirement should not be a normal statement that should be under inside one more container called Methods.

Methods:

If we want to write business logic/ business implementation it should be there inside a container called method.

A class contains variables and methods

There are two types of methods:

1. Static methods

2. Non-static methods




Friday, August 20, 2021

# 9. non static variables

Q) Where do we need to declare static variables?

 After starting the class immediately declare the static variable.

RAMESHSOFT ====> is a data

public static string Name = "RAMESHSOFT";

If we call that institute Name we will get RAMESHSOFT

In java initialization of the static variables is not mandatory, it is optional.

If we don't declare initialization of static variables then JVM is going to assign the default values

static int number_Two; ===> Valid

public static int number_Two; ===> Valid

Default values:

byte    short    int    long ===> 0

float    double ==========>0.0

String ================> null

boolean ==============> false

static int number_Two;   ===== > 0

static String ifYouDontDoPractice; =====> null

Whenever we declare the variable depends upon the data type for every variable internally some memory is going to be allocated.

Everything in the world occupies some space


Q) How to define non-static variables

Standard syntax:

If there is no static keyword then it will be non-static syntax

<access specifier>      data-type       variable-name    =     value/data ;

public int number_Two = 34;

<access specifier>       data-type        variable-name ;   

variable-name       =     value/data ;

public int number_Two ;

number_Two = 34 ;

Access specifier is not mandatory it is an optional


public int id=456; ========> non-static variable

static string target="java"; ==> static variable

double d=456.45; ========> non-static variable

float f1=456.456f; ========> non static variable

static int practiceHrs=9; ===> static variable

Q) Where do we need to declare non-static variables?

After starting the class immediately we need to declare.


If we dont specify initialization of variable then JVM will automatically assign some default value.

Q) Default values applicable for non-static variables?

Yes

byte    short    int    long ===> 0

float    double ==========>0.0

String ================> null

boolean ==============> false.


// Write addition program 45+47=92

// Assume 45 and 47 going to be changes means non-static

public class Addition_Program {

public int number_One=45;

public int number_Two=47;

}


 Example:


public class Addition_Demo{        // access specifier, class keyword, class name, start of the class


// declare static variables

// declaration and initialization in single line

static public int num1=10;        //data is constant,access specifier,data type,variable name,data

static int num2=20;        // access specifier is optional, data is constant, data type, variable name, data

public static int num3=30;        // access specifier, data is constant, data type, variable name, data


// declaration and initialization in two lines

static int num4;        // access specifier is optional, data is constant,data type, variable name

num4=40;        // variable name, data


// declare non static variables

// declaration and initialization in one line

public int num5=50;        // data changes, access specifier, data type, variable name,data

int num6=60;        // data changes,access specifier is optional, data type, variable name, data

int num7;        // data changes, access specifier is optional, data type ,variable name

num7=70;        // variable name,data


// if we don't initialize then, the default values are going to be assigned by JVM

// No initialization 

// Only declaration

// for static

static public int num8;        //default value for int =0

static float num9;        //default value for float=0.0

static String name;        //default value for String=null


// for non static

public int num9;        //default value for int=0

float num10;        // default value for float=0.0

String name;        // default value for String=null


}        //end of the class



Thursday, August 19, 2021

# 8. static and non-static variables

 45 is a data 

According to java, this data must be there in a container called variable

Which one do we take 

This 45 should be there in static or nonstatic or local or a reference type variable?

Static variables (class level):

Q) When do we go for a static variable.

Whenever the data of a variable is not going to be changed/ constant from object to object then go for static variables

Q) When do we go for non-static variables?

Whenever the data is going to be changed/ not constant from object to object then go for non-static variables.

case1: 45 is a data going to be changed then go for non-static

case2: 45 is a data is not going to be changed then go for static

For example: 

Assume that in a classroom we have 35 people, 

for all the student's institute names is going to change or not?

Static

For all these students trainer name is going to change or not

Static

Student name is going to change or not?

Yes from student to student

Non-static

Daily topics are going to change or not?

Yes

Non-static

A number of hands from person to person is going to change or not?

Not going to be changes

Static

Legs?

Not going to be changes

Static

Color?

Going to be changes

Non-static variable

Person phone number?

Going to changes

Non-static

Q) How to define static variables

Syntax:

<access specifier>      static         data-type       variable-name    =     value/data ;

-----------------------------------------------------------------------            -----------------

declaration                                                                                            initialization

Ex: public static int numberOne = 45;

 static    <access specifier>       data-type        variable-name       =     value/data ;

Ex: static public int number_One = 45 ;

We have done declaration and initialization in a single line

 static    <access specifier>       data-type        variable-name ;   

variable-name       =     value/data ;

Ex: static public int number_One;

number_One = 45 ;

Here Declaration is done in one line and initialization is done in another line.


Example:

public class Static_Program{        // access specifier, class keyword, class name, start of the class

// declaration and initialization in same line

public static int num1=20;        // access specifier, data is constant, data type, variable name, data

static public int num2=25;        // data is constant, access specifier, data type, variable name, data

static int num4=30;        // data is constant, access specifier is optional, data type, variable name,data

// declaration and initialization in different lines

static public int num3;        //declaration,data is constant, access specifier, data type, variable name

num3=80;        // initialization, variable name, data

}        //end of the class










Wednesday, August 18, 2021

# 7. Data types continuation

 Character data types

To represent this character data type in java we have a data type called char data type

Char data type is used to represent alphabets

Ex: a-z, A - Z

If we want to convey as char it should enclose data within single quotes ' '

char ' h ' 

char range -------> 0 to 65535

char ' d ' -----> valid

By using char we can represent a single character

char 'ap' ------> invalid

Only one character should represent in char

char ' '  --------> invalid

char must and should have a single character


Boolean data type:

To represent logical values we have a data type called boolean data type

there are only two logical values

1. true

2. false

Ex: boolean true

both true and false should be small letters

boolean false ===> valid

boolean True ===> invalid


String:

To represent the words we have a data type called string data types.

Word is a collection of alphabets

String ===> S should be a capital letter otherwise we will get a compile-time error.

Ex: String "JAVA"

Compulsory this data must and should enclose the data in double quotes " ".

String " Practice"

String "Practice089"

String " "

String "d"

String "&"

String "435"

String "pracTicE"

All are valid, whatever we write inside double quotes is valid.


Example:

public class Data_Types{          // access specifier, class keyword, class name, start of a class

//  data is constant, access specifier, data type, variable name, data

// static

static public char cdata='d';

static public String sdata="divya";


// data is constant, access specifier is optional, data type, variable name, data in single & double quotes

// static

static char cdata='d';

static String sdata="divya";


// data is not constant, data type, variable name, data

// non static

public char cdata='d';

public String sdata="divya";


// data is not constant, access specifier is optional, data type, variable name, data

// non static

char cdata='d';

String sdata="divya";

        // end of a class


Variables:

If we want to define some data in java compulsory the data must be there in a container called a variable.

A variable is a container where we are going to define/maintain/keep some data.

A water bottle is a container where we are going to maintain some water

A house acts as a class. Our house contains all the things, in the same way, the class contains all the things needed.

34 ===> is a data

this 34 should be there inside a container called a variable.

Depends upon the data behavior we have 4 types of variables

1. Static variables (class level)

2. Non-static variable (instance variables)

3. Local variables (method level)

4. Reference type variables (object type)

        4.1. Static variables (class level)

        4.2. Non-static variable (instance variables)

        4.3. Local variables (method level)












Tuesday, August 17, 2021

# 6. Data types

 Data types

Whenever we define some data in java, compulsory we should tell what type of data it is.

By using data type we can tell what type of data it is

 There are two data types

1. Primitive data types

Byte ----> 1 byte (8 bits)

Short ----> 2 bytes (16 bits)

Int --------> 4 bytes (32 bits)

Long ------> 8 bytes (64 bits)

Float-------> 4 bytes (32 bits)

Double------> 8 bytes(64 bits)

Char ---------> 2 bytes (16 bits)

Boolean-------> 1 bit

2. Non-primitive data types

String

Arrays

etc

Integer data types

----------------------------

Whole numbers

byte -------> -128 to + 127

Ex: 45

short ------> -32768 to +32767

Ex: 456

int ---------> -2147483648 to +2147483647  ( -2^31 to 2^31-1 )

Ex: 98768765

long -------> -2^63 to +263-1

Ex: 9166691345 l

In java whenever we want to convey the data as long suffix the data with l

Floating-point data types:

-----------------------------------

real numbers

To represent real numbers we have a data type called floating-point data

whenever our data have 0-6 decimal place of accuracy then go for float.

float -----------> 0-6 (f) ---------------> 4 bytes(32 bits)  //suffix with f

Ex: 32.12f, 34567231.1f,56677.99f

double ---------> 6+ -------------------> 8 bytes (64 bits)

Ex: 546.98768767, 5.97865423


under integer, we have whole numbers and under floating-point, we have real numbers.


>>> class Data_Types{        // access specifier optional, class keyword, class name, start of a class


// data is constant, access specifier, data type, variable name, data.

// static

static public byte bnum=100;

static public short snum=1000;

static public int inum= 1000000;

static public long lnum=8890768768l;

static public float fnum=98.89f;

static public double dnum=78.9876597;


// data is constant, access specifier is optional, data type, variable name, data.

// static

static byte bnum=100;

static short snum=1000;

static int inum= 1000000;

static long lnum=8890768768l;

static float fnum=98.89f;

static double dnum=78.9876597;


// data is not constant, access specifier, data type, variable name,data

// non-static

public byte bnum=100;

public short snum=1000;

public int inum= 1000000;

public long lnum=8890768768l;

public float fnum=98.89f;

public double dnum=78.9876597;


// data is going to change means constant, access specifier is optional, data type, variable name, data

// non-static

 byte bnum=100;

 short snum=1000;

 int inum= 1000000;

 long lnum=8890768768l;

 float fnum=98.89f;

 double dnum=78.9876597;

}        // end of a class






Monday, August 16, 2021

# 4. Types of data

Types of data 

Business requirement:

Write a java program for the addition of two numbers

45+45=90

public class Addition_Program {

;;;;;;;;;;;;;// statements

}

>>> cd desktop

Change directory

-> cd directory name

>>> javac Addition_Program.java

>>> java Addition_Program


In general, we have 5 types of data

1. Real numbers

2. Whole numbers

3. Alphabets

4. Words

5. Logical values


How many kinds of data are in java?

1. Real numbers (234, 1, 345, 12,90...) =========> integral data

2. Floating numbers (1.23, 324.1, 234.9... ) ======> floating data

3. Alphabets ( a-z, A-Z... ) ==================> char data

4. Words (Soft, practice,JAVA, salary...)  ========> string data

5. Logical values (true, false) ================> boolean data

In order to define data in java, we need to follow some standard steps.

1. Define the data ( 5 kinds )

90

2. Give/assign some name to the data (variable name)

number1 90

3. Tell what type of data it is?(data type)

integral number1 90

4. Specify the scope of the data (access specifiers)

public integral number1 90

5. Data is going to change or not? (static)

static public integral number1 90

Ex:

>>> public class My_Program{  // access specifier,class keyword ,class name, start of the class.

static public int num1=20;  //data is constant

static int num2=40;  // access specifier is optional

public int num3=25;    // data is not constant

int num4=80;    // data is not constant, access specifier is optional

}    // end of a class



Sunday, August 15, 2021

# 5. compilation process

 Java program compilation process

After completion of writing the java program, we need to save it by using .java extension

For example 

public class My_Program {

-----------------------------

---------------

-----------------------------------

}

In the above program, the class name is My_Program

Save the program with file name using .java extension

===> My_program.java

After the completion of saving the program, it needs to be compiled. Java compiler is responsible for compiling the java program 

===> javac My_Program.java

Whenever we compile the program, if there are no errors in the program .class file is going to be generated. If there are errors in the program .class file will not generate.

ls command ( list ) is used to find the list of files presented.

>>> ls

Whenever we type ls command it will show list of files available. It will generate .java and .class files

===> My_Program.java

===> My_Program.class

After compilation is done the .class file needs to execute by the JVM and produce the result.

===> java My_Program






Friday, August 13, 2021

# 3. Class in Java

Syntax and Rules for class


If we want to write something in the class room, we write it in the note book. 

Right! 

Book is a container which holds some documentation.

In the same way 

In java if we want to write a program that program should be inside a container called class.

Java is a programming language that is nothing but set of rules, syntaxes and regulations.

 A class is a container or a placeholder where we write program.

class is a fixed/standard keyword in order to convey it as a class, all should be in small letters.

In world everything has a name to call it in future/ to refer in future. 

In the same way in java also every class must have some name to call.


Rules to follow class name:

1. Class name must and should start with alphabets.

2. Class name should not start with numbers or special characters otherwise we will get compile time error.

3. Class name can contain numbers in between

4. Every class name must and should start with capital letter.

5. If a class name contains multiple words each word should start with capital letter.

6. Class name should be more descriptive to make it easier to understand

7. Class name should not contain special characters except '_'  underscore and '$' dollar symbols.

Ex: Flipkart_Electronics_Mobile


How to define a class:

-> <access specifier>    class    <class name>

{  // start of the class

-----------------------

-----------   //statements

----------------------

}  // end of the class

For example consider Facebook, when we post something in Facebook we have some options/restrictions like public, private, only me, only mutual friends, family. We chose based on our choice.



Access specifiers: Restricting the access

Specifying access of a class 

If we want to keep some restrictions on class we will go for access specifiers.

Types:

1. Public 

2. Private

3. Protected

4. <default>

It is not mandatory to set access specifier, its an optional. Even though if we don't specify any access specifier, by default it will become default.

Access specifier is optional.

Class keyword is mandatory.

Class name is mandatory

{ } are mandatory

>>> public class Myprogram {

statements

}

>>> public class My_Program {

statements

}








Wednesday, August 11, 2021

# 2. Program Standard steps

                            Java Program Standard Steps
   
                                           


        Real Time Approach: 

Assume that we have a subject in graduation called Environmental studies. Imagine a university called JNTUH  is going to prepare ES question paper. Once done with the question paper they are going to distribute this with corresponding colleges. Then the students needs to write the exam in a booklet which consists of some rules and guidelines like it contains only 32 papers no additional will be provided, use only blue pen, don't use red or green pen like this some guidelines are there.

Once writing the exam is done the answer sheet should be validate or correction process. Validation is the process of checking answer sheet whether the answers are correct or not and following rules and regulations or not. This process is done by JNTU Faculty.

Finally the university execute the results of all the students. Look at the following approach

Semester Exams

Environmental Studies ===== >  JNTUH

1. Write the exam =====> Students

    Booklet

    Rules and Guidelines

2. Validation / Correction Process =====> JNTU Faculty

    2.1. PASS

    2.2. FAIL

3. Execute the results =====> JNTUH

          Three standard steps to write program in java :

In the same way Java is a programming language Created by Sun Microsystems Later it is handover to Oracle.

JAVA =====> Sun Micro Systems =====> Oracle

1. Write a program =====> Programmer

     notepad / notepad++ / editplus / editplus++ / IDE's Eclipse or Visual Studio or Netbeans

    Rules, Regulations and Syntaxes

    .java extension

               
After completion of a program we need to save that program by using .java extension.


          Example: Filename.java
                             Product.java 

2. Validation / Compilation =====> Compiler

    2.1. ERRORS

    2.2. PASS

Now there are two cases:

If there are errors in the program .class file is not generated. If there are no errors in the program .class file will be generated.

Case 1: There is no errors =====> .class file (Binary/Machine/0's and 1's) is                                                                       generated

Case 2: Errors =====> .class file is not generated

After the completion of java program by using compiler we will compile the program.

Compiler is a software that converts the source code to class file that is binary language or 0's and 1's.

3. Execute the program =====> JVM ( Java Virtual Machine )

    .class file is generated

JVM executes the .class file.

 

True or False Questions:

1. Compiler is always going to compile or executes .java files

    True

2. Compiler always generates .class files

    False

Because if a program have errors it will not generate .class file

3. Compiler is always going to generate the .class files if and only if it will follow all the java rules regulations and syntaxes.

    True

4. JVM always executes either .java or .class files

    False

5. Compiler always executes .class files

    False


# 1. What is programming language?

 JAVA



what is Java?

Java is a programming language.

What is a language?

Language is a mode of communication, used to share ideas, opinions, information with one another.

For example, there are two persons John and Joe. To talk with each other or to share their thoughts or ideas or information they need something that is language. By using language they can communicate with each other. That language should be understandable by both John and Joe.

English==== Language ==== Communication Language === Share Information.

Here English is a language.

Which type of language?

It's a communication language.

Why it is used?

Communication Language ==== We can provide communication between persons to share information or ideas.

Now a person wants to learn English

----------------------------------------------

Being an English learner person should follow grammar.

Grammar ==== Collection of rules and syntaxes.


In the same way, coming to java

Java ==== Language ==== Programming Language ==== Person to Physical device ( computer), to develop software.

Here java is a language.

Which type of language?

Java is a programming language.

Why programming language is used?

Programming language is used to communicate persons with computers

The programmer wants to learn java

-------------------------------------------

Being java learners we should follow java rules, regulations, and syntaxes

Programming Language ==== Collection of Rules + Syntaxes + Regulations

Now, what is programming?

program is a set of rules to perform a specific task. 

Programming language is used by the programmer to communicate with the computer.

The computer only knows binary language means 0's and 1's because internally computer is built on top with a microprocessor that understands only 0's and 1's, that's why the computer only understands the binary language/ machine language.

But humans don't understand binary language so to communicate humans with computers we need a translator.

What is Translator?

A translator is built-in software that converts human-readable language to binary language/machine language and binary language/machine language to human-readable language.


 


# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...