An Overview of java
•OOPS
•Java Language Basics
Java Architecture
Programming Logics & Techniques
Operators
Data Types
•OOPS
Objectives
• Need for object-oriented programming
About Procedural Programming
Working of Procedural Programming
•Compare OOPS with procedural programming
•Identify the advantages of object-oriented programming
• Identify classes and objects
• Features of OOP
About Procedural Programming Language
•Procedural programming
Involves dividing a large program into a set of subprocedures or subprograms that perform specific tasks. Module consists of single or multiple procedures.Procedures are also known as functions, routines, subroutines, or methods in various programming languages.
In a program following procedural methodology, each step of a subprogram is linked to the previous step. Java Architecture
Programming Logics & Techniques
Operators
Data Types
•OOPS
Objectives
• Need for object-oriented programming
About Procedural Programming
Working of Procedural Programming
•Compare OOPS with procedural programming
•Identify the advantages of object-oriented programming
• Identify classes and objects
• Features of OOP
About Procedural Programming Language
•Procedural programming
Involves dividing a large program into a set of subprocedures or subprograms that perform specific tasks. Module consists of single or multiple procedures.Procedures are also known as functions, routines, subroutines, or methods in various programming languages.
Working of Procedural Programming
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-EFdxhtI2jubHyM4tb-OMORos85p37X-gls2muAfhQ_aEb0JkAYzyMSpbyVIcuzdVZKnRPjry2hs3KfZ2SbsQUHKHALkJJf8gY1-T64CMfDecbH15sfOsmKfI_EYrelbtDviaYYJt0VM/s320/1.jpg)
proocedural languages are used in the traditional programming that is based on algorithms or a logical step-by step process for solving a problem.A procedural programming language provides a programmer a means to define precisely each step in the performance of a task Non-procedural programming languages allow users and professional programmers to specify the results they want without specifying how to solve the problem. examples are FORTRAN,C++,COBOL,ALGOL etc
OR we can put it this way:
Procedural language determines WHAT & HOW a process should be done, Non-procedural language is concerned with the WHAT not the HOW. Non-proc languages are those languages where you specify what conditions the answer should satisfy, but not how to obtain it.
About Object Oriented Progrmming
Object-Oriented Programming :A large application consists of component objects, which interact with each other. Can be used to develop various applications.
What is an Object?
object is a software construct that encapsulates data, along with the ability to use or modify that data, into a software entity.
•An object is a self-contained entity which has its own private collection of properties (ie. data) and methods (ie. operations) that encapsulate functionality into a reusable and dynamically loaded structure.
What is an Object Oriented Program?
Object-Oriented Program consists of a group of cooperating objects,exchanging messages, for the purpose of achieving a common objective.
What is a Class?
class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-EFdxhtI2jubHyM4tb-OMORos85p37X-gls2muAfhQ_aEb0JkAYzyMSpbyVIcuzdVZKnRPjry2hs3KfZ2SbsQUHKHALkJJf8gY1-T64CMfDecbH15sfOsmKfI_EYrelbtDviaYYJt0VM/s320/1.jpg)
proocedural languages are used in the traditional programming that is based on algorithms or a logical step-by step process for solving a problem.A procedural programming language provides a programmer a means to define precisely each step in the performance of a task Non-procedural programming languages allow users and professional programmers to specify the results they want without specifying how to solve the problem. examples are FORTRAN,C++,COBOL,ALGOL etc
OR we can put it this way:
Procedural language determines WHAT & HOW a process should be done, Non-procedural language is concerned with the WHAT not the HOW. Non-proc languages are those languages where you specify what conditions the answer should satisfy, but not how to obtain it.
About Object Oriented Progrmming
Object-Oriented Programming :A large application consists of component objects, which interact with each other. Can be used to develop various applications.
What is an Object?
object is a software construct that encapsulates data, along with the ability to use or modify that data, into a software entity.
•An object is a self-contained entity which has its own private collection of properties (ie. data) and methods (ie. operations) that encapsulate functionality into a reusable and dynamically loaded structure.
What is an Object Oriented Program?
Object-Oriented Program consists of a group of cooperating objects,exchanging messages, for the purpose of achieving a common objective.
What is a Class?
class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
blueprint:-->A class can't do anything on its own.
defines: -->A class provides something that can be used later.
defines: -->A class provides something that can be used later.
objects: -->A class can only be used, if it had been "brought to life" by instantiating it.
Example
Example
Methods
•An operation upon an object, defined as part of the declaration of a class.
•The methods, defined in a class, indicate, what the instantiated objects are able to do.
Examples
1.Driver wants to increase the speed of the car?
2.Driver wants to decrease the speed of the car?
Advantages of OOP
•Real-world programming
•Reusability of code
•Modularity of code
•Resilience to change
•Information hiding
OOPS Principles:
The three principles of Object Oriented Programming are:
- Encapsulation
- Inheritance
- Polymorphism
Encapsulation:
It is defined as a mechanism that associates the code and the data into a single unit to and keeps them safe from external interference and misuse. In other words, it is a wrapper that wraps code and data into one unit.
Practically, this is implemented with the help of a construct known as a class. A class is defined as a template that has a structure and behaviour that is shared by a group of objects. Each object of a class contains structure and behaviour of a class i.e. why sometimes objects are known as instance of a class. A class contains variables and methods. These are known as members of the class. Variables of a class explains the structure of a class, where as methods of a class explains the behaviour of a class. In Java, they are known as methods where as in C and C++ they are known as functions.
With the help of encapsulation we can protect the data present in instance variables and method body. But this can be implemented with the help of access specifiers. They are three access specifiers as shown below:
v public
v private
v protected
If a member of a class is declared as public , then object of any class can access it. But if the member of a class is declared as private, then the object of that class can only access i.e. objects of other class cannot access it. But, if the member of the class is protected then the sub class object can only access it. Member of a class include the variables and methods of that class.
Encapsulation (Example)
Inheritance:
Inheritance is defined as a process in which one object acquires characteristics and behaviour of another object. To understand clearly, let us consider two objects like father and son. In normal world, we can see that the features of a son are much similar of that his father. This concept is known as Inheritance.
In Programming, the basic definition of inheritance is Reusability. The concept of inheritance was first introduced by a programming language known as Smalltalk. If Java is using inheritance concept, then they are two types of classes i.e. sub class and super class. A sub class is a more specialized versions of a class, which inherit attributes and behaviors from their parent class. In other words, this sub class contains attributes and behaviours of parent class and it has capability to define on its own.
With in a class hierarchy, it is also possible for subclasses to override any methods that they have inherited, and they can create unique implementations for these methods. Moreover, if user wants to change the body of a method in a sub class that is inherited from the super class, then it is possible with the help of a concept known as “Method Overriding”.
e
e
Types of Inheritance
•Single inheritance
•Multiple inheritance
•Single inheritance
Subclass is derived from only one superclass
Polymorphism
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. We have already used two types of polymorphism (overloading and overriding). Now we will look at the third: dynamic method binding.
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. We have already used two types of polymorphism (overloading and overriding). Now we will look at the third: dynamic method binding.
Assume that three subclasses (Cow, Dog and Snake) have been created based on the Animal abstract class, each having their own speak() method.
Notice that although each method reference was to an Animal (but no animal objects exist), the program is able to resolve the correct method related to the subclass object at runtime. This is known as dynamic (or late) method binding.
•Java Language Basics
Objectives:
•Concepts of Java Programming Language
•Java Architecture
•Structure of Java Program
•About Data Types
•Overview of Types of Data Types
•Variables
•Methods
•Operators
•Methods
•Operators
•Control Flow Statement
•Creating a Java Application
•Types of Variables
•Access Specifiers
•Modifiers
•Java Language Basics
Objectives:
•Concepts of Java Programming Language
•Java Architecture
•Structure of Java Program
•About Data Types
•Overview of Types of Data Types
•Variables
•Methods
•Operators
•Methods
•Operators
•Control Flow Statement
•Creating a Java Application
•Types of Variables
•Access Specifiers
•Modifiers
Writing a java program
:
HelloWorld.java
Let's look into our first program. Save the following text into the file called HelloWorld.java
class Hello
{
{ public static void main(String args[])
{
System.out.println("Hello World");
} //main
} //class
}
We examine that there is a class called Hello. Since Hello is the only class in the file, it is called the main class. In order to make your program runnable, you should have a function called main which is defined as:
public static void main(Strings[] args)
The explanation is as follows:
The public qualifier tells you that the function main is callable from outside.
Then the next qualifier is static. We will study about static in near future .
Then, void tells you that the function main returns nothing to the system.
Then the function's name, main.
Then String[] args denotes the argument passed into the program. Arguments are nothing but the options or switches you would mention when invoking programs.
As in case of C language we use printf() and in case of C++ we use cout statement, like wise in case of Java you can consider System.out.println as a statement that is used to print some text on the screen. Note that the text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed, without the quotes of course. Remember that almost every command in Java ends with a semicolon(;).
NOTE:
The main class name has to be the same as the program file name. Otherwise, your Java program won't compile. In this example the class name is Hello. The program file has to be Hello.java.
You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work.
Then String[] args denotes the argument passed into the program. Arguments are nothing but the options or switches you would mention when invoking programs.
As in case of C language we use printf() and in case of C++ we use cout statement, like wise in case of Java you can consider System.out.println as a statement that is used to print some text on the screen. Note that the text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed, without the quotes of course. Remember that almost every command in Java ends with a semicolon(;).
NOTE:
The main class name has to be the same as the program file name. Otherwise, your Java program won't compile. In this example the class name is Hello. The program file has to be Hello.java.
You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work.
How to Compile Java Program
If user wants to compile a java program, then it is possible with the help of a command known as javac. This command internally calls Java Compiler, which will create a new file that contains the byte code version of the source code and the name of the file will be same as source code file with an extension of .class .Don't forget to change the directory to the directory where your Java program resides. Also, make sure that your JDK installation is correct.
javac Hello.java
When the above program gets executed, they are two expectations. The first is it produces a file called Hello.class, it means that your compilation is successful. The second is gives compile time errors, if they exists with in the program, then no .class file gets created.
It must be noted that a .class file gets created, if and only if they are no compile time errors.
How to Run Java Program
If user wants to compile a java program, then it is possible with the help of a command known as javac. This command internally calls Java Compiler, which will create a new file that contains the byte code version of the source code and the name of the file will be same as source code file with an extension of .class .Don't forget to change the directory to the directory where your Java program resides. Also, make sure that your JDK installation is correct.
javac Hello.java
When the above program gets executed, they are two expectations. The first is it produces a file called Hello.class, it means that your compilation is successful. The second is gives compile time errors, if they exists with in the program, then no .class file gets created.
It must be noted that a .class file gets created, if and only if they are no compile time errors.
How to Run Java Program
To run it, under Windows or UNIX, type the following:
java Hello
On writing the above line, it must be noted that there must be no .class extension. When the above command gets executed, internally the java command will call for JVM that will convert the byte code into suitable machine instructions that can be understandable by the processor present in the system on which the program is getting executed. After converting the processor will act on that statements and then user can see output. Some times if there is some error during execution or runtime, then instead of displaying output, it displays user understandable messages known as Exceptions.
NOTE:
In case of C and C++ programming languages, when a program gets executed, a file by name .exe gets generated. But in case of Java, there is no file by name .exe gets generated
java Hello
On writing the above line, it must be noted that there must be no .class extension. When the above command gets executed, internally the java command will call for JVM that will convert the byte code into suitable machine instructions that can be understandable by the processor present in the system on which the program is getting executed. After converting the processor will act on that statements and then user can see output. Some times if there is some error during execution or runtime, then instead of displaying output, it displays user understandable messages known as Exceptions.
NOTE:
In case of C and C++ programming languages, when a program gets executed, a file by name .exe gets generated. But in case of Java, there is no file by name .exe gets generated
Commenting Your Program
It is said to be a good practice, if you write comments beside the source code of the program. In real time industry, you cannot find software or program without comments. Sometimes, it is useful to make some note about your program. Not only making you clear about what's going on your program, but also making others able to read your program based on the comments of your program. The compiler will completely ignore your comment, however. In Java, there are three ways of commenting:
Double slash comment.
Example:
It is said to be a good practice, if you write comments beside the source code of the program. In real time industry, you cannot find software or program without comments. Sometimes, it is useful to make some note about your program. Not only making you clear about what's going on your program, but also making others able to read your program based on the comments of your program. The compiler will completely ignore your comment, however. In Java, there are three ways of commenting:
Double slash comment.
Example:
// Put your comments here
After you put the double slash (//), everything else behind it is considered as comment.
Embracing comment.
Example:
/* My comment is here
Neat huh?
*/
If your comment is pretty long, it is better to embrace it with /* and */. Everything within it is considered as a comment.
Java doc comment.
It is similar to above, except:
/** My comments here will be put into
documentation by Javadoc program
*/
It is embraced with /** and */. Notice the double star. Your comments within it will be documented by the Javadoc program, an automatic documentation maker from Java SDK package.
There is no limit to the length of a Java variable name. The following are legal variable names:
* MyVariable
* myvariable
* MYVARIABLE
* x
* i
* _myvariable
* $myvariable
* _9pins
* andros
* ανδρος
* OReilly
*This_is_an_insanely_long_variable_name_that_just_keeps_going_and_going_and_going_and_well_you_get_the_idea_The_line_breaks_arent_really_part_of_the_variable_name_Its_just_that_this_variable_name_is_so_ridiculously_long_that_it_won't_fit_on_the_page_I_cant_imagine_why_you_would_need_such_a_long_variable_name_but_if_you_do_you_can_have_it
The following are not legal variable names:
* My Variable // Contains a space
* 9pins // Begins with a digit
* a+c // The plus sign is not an alphanumeric character
* testing1-2-3 // The hyphen is not an alphanumeric character
* O'Reilly // Apostrophe is not an alphanumeric character
* OReilly_&_Associates // ampersand is not an alphanumeric character
Tip: How to Begin a Variable Name with a Number
If you want to begin a variable name with a digit, prefix the name you'd like to have (e.g. 8ball) with an underscore, e.g. _8ball. You can also use the underscore to act like a space in long variable names.
Embracing comment.
Example:
/* My comment is here
Neat huh?
*/
If your comment is pretty long, it is better to embrace it with /* and */. Everything within it is considered as a comment.
Java doc comment.
It is similar to above, except:
/** My comments here will be put into
documentation by Javadoc program
*/
It is embraced with /** and */. Notice the double star. Your comments within it will be documented by the Javadoc program, an automatic documentation maker from Java SDK package.
Variable:
A variable is defined as a storage area that is used to store data. Each and every variable has a scope and a lifetime. Let us consider a small example. If user is interested to write a program i.e. addition of two numbers, say 2 and 3. Then there is a necessity to store the above values. Here the variable comes into being. We use variable to store this values i.e. 2 is stored in one variable and 3 is stored in one variable.
If user wants to use a variable, while writing a java program, then it is mandatory to declare that variable before it is used.
Declaring a Variable:
Being a java programmer, if you want to say the operating system to reserve some space in the main memory in order to store some data, then it is possible with the help of a statement known as declaring a variable .The syntax to declare a variable is
datatype identifier = value;
The datatype explains the type of data that can be stored in that memory location. The identifier is defined as the rules that are used to name a variable. To be more specific, identifier is the name that we use with in the program that refers to that memory location where the data is stored. Whenever user refers this identifier in the program, the operating system will look up the memory address that corresponds the identifier and accesses the memory address. This concept is known as resolving. Now the last part is nothing but the right hand side of the assignment operator can be the value or an expression.
Identifier
Identifier is nothing but some rules that define the name of a variable or a class or an interface or a method or an array or an object. The name that the programmer wants to select must represent the nature of data that is stored in the corresponding memory location. The below are some restrictions that must be applied while choosing an identifier:
- An Identifier cannot begin with a number
- An Identifier cannot contain spaces.
- An Identifier must not be a keyword.
While using an identifier, it must be noted that it must be unique with in the block where it is declared i.e. programmer cannot declare two variables with same name within the same block otherwise it will lead to a compile time error. However the programmer can declare same identifier in different blocks.
* MyVariable
* myvariable
* MYVARIABLE
* x
* i
* _myvariable
* $myvariable
* _9pins
* andros
* ανδρος
* OReilly
*This_is_an_insanely_long_variable_name_that_just_keeps_going_and_going_and_going_and_well_you_get_the_idea_The_line_breaks_arent_really_part_of_the_variable_name_Its_just_that_this_variable_name_is_so_ridiculously_long_that_it_won't_fit_on_the_page_I_cant_imagine_why_you_would_need_such_a_long_variable_name_but_if_you_do_you_can_have_it
The following are not legal variable names:
* My Variable // Contains a space
* 9pins // Begins with a digit
* a+c // The plus sign is not an alphanumeric character
* testing1-2-3 // The hyphen is not an alphanumeric character
* O'Reilly // Apostrophe is not an alphanumeric character
* OReilly_&_Associates // ampersand is not an alphanumeric character
Tip: How to Begin a Variable Name with a Number
If you want to begin a variable name with a digit, prefix the name you'd like to have (e.g. 8ball) with an underscore, e.g. _8ball. You can also use the underscore to act like a space in long variable names.
Default Values in java:
Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
The following chart summarizes the default values for the above data types.
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.
Keywords are reserved words that are predefined in the language. All the keywords are in lowercase
.
abstract | continue | for | new | switch |
assert*** | default | goto* | package | synchronized |
boolean | Do | if | private | this |
break | double | implements | protected | throw |
byte | Else | import | public | throws |
case | Enum**** | instanceof | return | transient |
catch | extends | int | short | try |
char | Final | interface | static | void |
class | Finally | long | strictfp** | volatile |
const* | Float | native | super | while |
* not used | ** added in 1.2 | *** added in 1.4 | **** added in 5.0 |
Escape Characters
Seq | Name | Seq | Name |
\b | Backspace | \f | Form feed |
\t | horizontal tab | \" | double quote |
\n | New line | \' | single quote |
\r | carriage return | \\ | backslash |
\### | Latin encoded character | \uHHHH | Unicode encoded character |
No comments:
Post a Comment