Monday, 27 June 2022

4. Main method in Java

Main method

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

  1. Write a program
  2. Validate/compile
  3. Execute

Example:

1. Write a program:

class My_Program {        // access specifier is optional, class keyword, class name, the start of a class

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

------------------------        // ststements

}            // end of a class

2. Compile the program:

Now compile the program

>>> javac My_Program.java

If there are no errors in the program .class file will generate.

The moment we click on enter here JVM is going to start. Once JVM is started in this desktop JVM check whether 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. 

3. Execute the program:

JVM will start the execution

>>> java My_Program

Whenever you 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.

In the same way to execute the java program we need a starting point.

  1. JVM looks for .class file
  2. Loads the .class file
  3. JVM looks for execution point

What is the execution point?

JVM is always going to start 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 you ask the JVM to run the program, JVM always starts execution from the main method.

why?

Because 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:

<access specifier> <static keyword> <void> <main> <(String[] args)>

{        // start of the main method

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

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

}        // end of the main method

String ---> Here S should be capital letter as per the java rules

There are several ways to define main method in java program:

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[] CREATIVEKODZ)

{

// accessing or testing the implemented services or methods

}

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

{

// 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 by default static method.

Where we need to define the main method in a program?

1. After starting the class immediately take main method

public class My_Program  {

        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

public class My_Program {

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

public class My_Program  {

        ;;;;;;;;;;;;;;;;;;;///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 [] CREATIVEKODZ)

{

// accessing or testing the implemented methods / services

// execution part

}

}

If we remove static keyword we will get a compile-time error.

public static void main(String[] CREATIVEKODZ) ---> valid

public void main(String[] CREATIVEKODZ) ---> invalid

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

 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 don't define main method immediately we will get compile time error.

If there is no main method we cannot execute the program.

The basic java program is:

public class My_Program {        // start of a class

        public static void main (String[] args) {         //start of main method

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

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

        }        //end of main method

}        // end of a class

check your knowledge

I hope you have learned something new today!

Now it's time to check your knowledge!!

Let's answer the below queries

Write answers to the following questions in the comments section

1. What is main method?

2. Is static keyword is mandatory?

3. Write any three syntaxes of main method

4. How many standard steps are there in java to write a program and what are they?

5. What is execution point?

6. From where JVM starts the execution of a program?

7. Write a java basic program with class definition and main method?

8. Where we can define main method in a program?

If you are struggling somewhere to answer the above questions please review the post once again and try it.

😊HAPPY LEARNING👍
👉Please Subscribe, share, follow and comment🙏💓

THANKING YOU ALL

Wednesday, 8 June 2022

3. Compilation in Java

Java program compilation process

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

A compiler is a software that converts the source code to a class file that is the binary language of 0s and 1s.

The standard syntax to compile the java program is:

<javac>    <class name/file name>  .java

The standard syntax to execute a program in java is:

<java>    <classname/file name>

In java, we should save the file with the class name, which means the file name and class name should be identical.

Compilation example:

javac Filename.java

Execution example:
java Filename

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 be executed by the JVM and produce the result.

===> java My_Program

check your knowledge

I hope you have learned something new today!

Now it's time to check your knowledge!!

Let's answer the below queries

Write answers to the following questions in the comments section

  1. What is compilation?
  2. What is the standard syntax for compilation and execution of java program?
  3. Is class name and file name should be same?
  4. Write a simple java program with class definition and write compilation and execution steps of your program with your file name?
  5. What is ls command?

If you are struggling somewhere to answer the above questions please review the post once again and try it.

😊HAPPY LEARNING👍
👉Please Subscribe, share, follow and comment🙏💓

THANKING YOU ALL

Saturday, 4 June 2022

2. Class in Java

Syntax and Rules for class



If we want to write something in the classroom, we write it in the notebook. 

Right! 

A book is a container that 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 a set of rules, syntaxes, and regulations.

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

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

In the world, everything has a name to call it future/ to refer to in the 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 the alphabet.

Ex: Program, Electronics, Dolls, etc.--------> valid

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

Ex: 78Program, 80Dolls, #animals -------> not valid

3. Class name can contain numbers in between

Ex: Program20, Electronics4, Dolls50babies--------> valid

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

Ex: Program, Electronics, Dolls, etc.--------> valid 

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

Ex: MyProgram, BabyDolls, ElectronicDevices---------> valid

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

Ex: BabyDollsStore, ElectronicDevicesShop, MyProgramForPractice---> valid

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

Ex: Flipkart_Electronics_Mobile, Flipkart$Electronics$Mobile-----> valid 

A java file can have any number of classes but it can have only one public class and the file name should be the same as the public class name.

How to define a class:

<access specifier>    class_keyword <class name>

{  

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

-----------   

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

}  

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

Access specifiers: Restricting the access

Specifying access to 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 an access specifier, it's optional. Even though if we don't specify any access specifier, by default it will become the default.

  • Access specifier is optional.
  • The class keyword is mandatory.
  • Class name is mandatory
  • { } are mandatory

Syntax:

public class Myprogram {

statements

}

Syntax:

public class My_Program {

statements

}

Comments in java

There are two types of comments in java

  1. Single line comments
  2. Multi line comments/ block comment/ traditional comment

Single line comments: it will starts with // in the beginning of the comment

// I am a single line comment

Multi line comment: it starts with /* forward slash and asterisk in the beginning of the comment and ends with */ asterisk and forward slash.

/* I am a

·        Multi line comment

I can be used in java */

Documentation comments:

These begin with a forward slash followed by two asterisks and end with an asterisk followed by a forward slash

/** this is a documentation comment */

/** this is also a

Documentation comment*/

When a documentation comment begins with more than two asterisks Javadoc assumes that you want to create a ‘box’ around the comment in the source code. It simply ignored the extra asterisks

/************************

This is the start of a method

*************************/

This will retain just the text “ This is the start of a method” for the documentation.

Now define the class using comments

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

Example 1:

// class definition with multi line comments

// access specifier, class keyword, class name

public class My_Program {        // start of the class

/* statements

statements

statements 

*/

}        // end of a class

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

Example 2:

// class definition with single line comments

// access specifier is optional, class keyword, class name

class My_Program {        // start of the class

// statements

// statements

// statements

}        // end of a class

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

check your knowledge

I hope you have learned something new today!

Now it's time to check your knowledge!!

Let's answer the below queries

Write answers to the following questions in the comments section

  1. What is a class?
  2. How many types of comments are there in java and what are they?
  3. What are access specifiers and how many of them?
  4. Name all access specifiers in java?
  5. Are access specifiers are mandatory in java?
  6. What are the rules for class name?
  7. Write class definition syntax with comments and without comments?



😊HAPPY LEARNING👍
👉Please Subscribe, share, follow and comment🙏💓

THANKING YOU ALL


Friday, 3 June 2022

1. Introduction to Java


Java

Java is a programming language.

What is a language?

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

For example, there are two people 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 for communication between persons and 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 of 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 with humans with computers we need a translator.

What is a Translator?

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

What is Java

  • Java is an object-oriented programming language used in distributed environments on the internet.
  • High-level programming language
  • Easy to read and understand
  • Simple and easy to use

Background

Java is the most popular language created in 1991, publicly released by 1995

Developed by James Gosling at Sun Microsystems

Java was later acquired by Oracle

The motto is “Write once and run anywhere” (WORA). This means that compiled Java code will run on all java compatible platforms without the need for recompilation.

The first name is ‘Oak’ because of the Oak tree outside Gosling’s office. Later changed to Green then to Java Coffee, he loves the coffee from Indonesia and shortened it to Java in 1995.

Usage

  • ·       Web applications
  • ·        Mobile applications
  • ·        Game development
  • ·        Embedded systems
  • ·        Desktop applications
  • ·        Develop software
  • ·        Mobile devices
  • ·        Electronic devices

§  Televisions

§  Air conditioners

§  Washing machines

  • ·        Online registration forms
  • ·        Banking apps
  • ·        Online shopping apps

Features of java



Editions in Java

There are three editions in Java. Programmers can learn any of these editions based on the application they want to make. 

  • Java Standard Edition - Contains core libraries, like java. lang, java. util, etc. 
  • Java Enterprise Edition - Includes Java APIs, like JMS, EJB, JSPs/servlets, etc.
  • Java Micro Edition - This edition is used to program Java in cell phones, set-top boxes, handhelds, and so on. 

The most widely used edition in Java is Java SE (Standard Edition). Java SE encompasses the basics of Java—most applications require Standard Edition. 

How a java program run


 

  1. Write a java program
  2. Save the java program as a .java file
  3. Compile the .java file with the help of the java compiler
  4. Once the program is compiled the compiler is going to generate another file called the .class file
  5. .class file is also known as bytecode file
  6. This bytecode file can run on any platform either Windows or Linux or macOS.

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 an ES question paper. Once done with the question paper they are going to distribute it to the corresponding colleges. Then the students need 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 the only blue pen, don't use the red or green pen like this some guidelines are there.

Once writing the exam is done the answer sheet should be validated or correction process. Validation is the process of checking the 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 executes 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 a 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 the .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.

A compiler is a software that converts the source code to a class file that is the binary language of 0s and 1s.

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

    .class file is generated

JVM executes the .class file and generates the output.

Anatomy of java

JDK: Java development kit, it is a super set of JRE and is a software development environment used to develop java applications and applets.

Once the JDK has been installed it will provide a lot of other components like JRE (Java Runtime Environment), JVM (Java Virtual Machine), class libraries, and other libraries.

Write and compile the program by using the compiler provided by JDK.

Once the program is compiled .class file will be generated.

JRE: Run the .class file using JRE

JRE will run and execute the .class file with the help of three components such as JVM, class libraries, and other libraries.

JVM: JVM is a virtual environment inside which the programs will run. It is platform-independent, it loads the java code, validates the code, and executes the code.

To run the program how does it achieve this environment?

For achieving this virtual machine environment, it would need some runtime libraries, that are provided by this class libraries.

Installing Java

Open the browser and type JDK java download

Click on oracle.com

Select the appropriate OS and download

Install JDK

To check whether JDK is successfully installed or not:

Open cmd

Type java -version

Installing Eclipse

Open browser and type eclipse download

Click on eclipse.org

Click on get eclipse IDE and download it

Install and launch

True or False Questions

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

    True

2. Compiler always generates .class files

    False

Because if a program has errors it will not generate a .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

check your knowledge

I hope you have learned something new today!

Now it's time to check your knowledge!!

Let's answer the below queries

Write answers to the following questions in the comments section

  1. What is java?
  2. What is JDK?
  3. What are the applications/uses of java?
  4. What is a language?
  5. What is a translator?
  6. What is a program?
  7. Who invented java and in which year?
  8. What are the features of java?
  9. Why java is named like that?
  10. How many standards are there in java and what are those?
  11. How many standard steps in java and explain it in your words?
  12. What is JVM, JRE and explain in brief?
  13. Installation steps of Java and Eclipse?
  14. What is .class file and when it will generate(include cases)?
  15. What is the extension of java file?
If you are struggling somewhere to answer the above questions please review the post once again and try it.

😊HAPPY LEARNING👍
👉Please Subscribe, share, follow and comment🙏💓

THANKING YOU ALL


4. Main method in Java

Main method In order to write a program in java, we need to follow 3 standard steps Write a program Validate/compile Execute Examp...