Theory
Java – Theory
What is Java?
Java is a 3rd Generation Object Oriented programming Language, developed by James Gosling in the year 1995. It was initially codenamed as Green and hypothetically called Oak named after the Oak tree outside Gosling’s house.
It is case sensitive and has a unique two step translation process that includes both Compilation and Interpretation which makes Java Platform independent.
Java is a 3rd Generation Object Oriented programming Language, developed by James Gosling in the year 1995. It was initially codenamed as Green and hypothetically called Oak named after the Oak tree outside Gosling’s house.
It is case sensitive and has a unique two step translation process that includes both Compilation and Interpretation which makes Java Platform independent.
What is Procedure Oriented Programming Language?
Pop is a Procedural Oriented Programming Language. In this, the stress is laid on function rather than data. Data may be kept floating throughout the Programming. Hence by scanning the whole program from beginning to the end and we can rectify any error caused. E.g. of POP based language are GW-BASIC, C, etc…
Characteristics of POP
1) Emphasis is laid on functions (Logical steps).
2) Functions share global data.
3) Data values can keep floating from one function to another.
4) Uses top down approach of programming.
Pop is a Procedural Oriented Programming Language. In this, the stress is laid on function rather than data. Data may be kept floating throughout the Programming. Hence by scanning the whole program from beginning to the end and we can rectify any error caused. E.g. of POP based language are GW-BASIC, C, etc…
Characteristics of POP
1) Emphasis is laid on functions (Logical steps).
2) Functions share global data.
3) Data values can keep floating from one function to another.
4) Uses top down approach of programming.
Drawbacks of POP
1) As Data values are global to all the functions, you may require making necessary changes in the functions due to change in the data values.
2) It is not suitable to solve complex problems in real situations.
1) As Data values are global to all the functions, you may require making necessary changes in the functions due to change in the data values.
2) It is not suitable to solve complex problems in real situations.
What is Object Oriented Programming Language?
OOP is an Object Oriented Programming Language. It is a modular approach to programming in which equal stress is given to data as well as methods and it allows the data be applied within the stipulated program area. It also provides reusability features to develop productivity logic.
OOP is an Object Oriented Programming Language. It is a modular approach to programming in which equal stress is given to data as well as methods and it allows the data be applied within the stipulated program area. It also provides reusability features to develop productivity logic.
Features of OOP
1. It gives equal stress on data and function.
2. It makes the program simple by dividing it into a number of objects.
3. The objects can be used as a bridge to have data flow from one function to another.
4. Data can be easily modified without any change in the function.
1. It gives equal stress on data and function.
2. It makes the program simple by dividing it into a number of objects.
3. The objects can be used as a bridge to have data flow from one function to another.
4. Data can be easily modified without any change in the function.
Advantages of OOP
1) We can extend the use of existing class through inheritance.
2) Using the concept data hiding can generate secured program.
3) We can create different modules in our project through objects.
4) Multiple instances of an object can be generated to co-exist without any interference.
5) It is highly beneficial to solve complex problems.
6) It is easy to modified and maintain software complexity.
1) We can extend the use of existing class through inheritance.
2) Using the concept data hiding can generate secured program.
3) We can create different modules in our project through objects.
4) Multiple instances of an object can be generated to co-exist without any interference.
5) It is highly beneficial to solve complex problems.
6) It is easy to modified and maintain software complexity.
Examples of OOP based languages are Simula, C++, Java, Python, and Smalltalk etc…
Basic Principles of OOP
1) Encapsulation – Binding up of data members and member functions together into a single unit called (class) is called Encapsulation.
2) Data Abstraction – Act of representing only essential features without including its background details is called Data Abstraction.
3) Polymorphism – The ability of a method to behave in more than one form is called polymorphism. Function Overloading is a way to implement it, in which more than one function has the same name but different argument list.
4) Inheritance – The ability of a class to derive the properties from another class is called Inheritance. The class that inherits is called the Derived / Sub / Daughter class and the class from which it is derived is called Base / Super / Parent class. The keyword used to implement Inheritance is ‘extends’.
5) Object – It is an identifiable entity with some characteristics and behaviour.
6) Class – It is a blueprint that represents a set of objects which share common characteristics and behaviour.
1) Encapsulation – Binding up of data members and member functions together into a single unit called (class) is called Encapsulation.
2) Data Abstraction – Act of representing only essential features without including its background details is called Data Abstraction.
3) Polymorphism – The ability of a method to behave in more than one form is called polymorphism. Function Overloading is a way to implement it, in which more than one function has the same name but different argument list.
4) Inheritance – The ability of a class to derive the properties from another class is called Inheritance. The class that inherits is called the Derived / Sub / Daughter class and the class from which it is derived is called Base / Super / Parent class. The keyword used to implement Inheritance is ‘extends’.
5) Object – It is an identifiable entity with some characteristics and behaviour.
6) Class – It is a blueprint that represents a set of objects which share common characteristics and behaviour.
Related Terms :
Data binding – is the process to link to the function call with function signature at run-time i.e., during execution of a program.
Data binding – is the process to link to the function call with function signature at run-time i.e., during execution of a program.
Attribute:-The characteristics which make every object having its identity make them distinguished is called attribute.
Behaviour – The behaviour of any class or object is represented through various functions and methods.
Message Passing – When object need to interact with one another they do so by passing information to and from one another, this is called Message Passing
Data Hiding – is the data which cannot be accessed directly outside, class premises although they are available in the same program
Garbage Collection – The Garbage-collected Heap is where the objects in Java programs are stored. Whenever we allocate an object using new operator, the heap comes into picture and memory is allocated from there. Java does this automatically using Garbage collection mechanism, using an algorithm named Mark & Sweep logic. Remember that the local object reference resides on Stack but the actual object resides in Heap only. Also, arrays in Java are objects, hence they also resides in Garbage-collected Heap.
Different Types of Java Programs
1) Standalone Application – An application program that is developed by programmers which consists of various methods and these methods being invoked from within a ‘main ( )’ method is called standalone application.
2) Internet Applets – These are small java programs that are embedded in a web page and which requires a web browser to be run/executed.
3) Servlets – They extend the functionality of web browser.
Source Code – The High level language code written by programmers which is not understandable by the computer is known as Source Code. It has an extension of *.java
Byte Code – is an intermediate code that consists of a set of pseudo machine language instructions that are understood by the JVM and are independent of the underlying hardware. It is called byte code because each chunk of code is of 8 BITS (1 byte = 8 Bits).
1) Standalone Application – An application program that is developed by programmers which consists of various methods and these methods being invoked from within a ‘main ( )’ method is called standalone application.
2) Internet Applets – These are small java programs that are embedded in a web page and which requires a web browser to be run/executed.
3) Servlets – They extend the functionality of web browser.
Source Code – The High level language code written by programmers which is not understandable by the computer is known as Source Code. It has an extension of *.java
Byte Code – is an intermediate code that consists of a set of pseudo machine language instructions that are understood by the JVM and are independent of the underlying hardware. It is called byte code because each chunk of code is of 8 BITS (1 byte = 8 Bits).
The byte code files have an extension of *.class
Object Code – The interpreter of Java named JVM (which stands for Java Virtual Machine) then converts the intermediate Byte Code into machine specific executable which runs on the host machine.
API (Application Programming Interface) – consists of libraries of pre compiled code that programmers can use in their application and programs for designing softwares. Hence we can say that Java API consists of functions and variables that programmers can reuse.
API (Application Programming Interface) – consists of libraries of pre compiled code that programmers can use in their application and programs for designing softwares. Hence we can say that Java API consists of functions and variables that programmers can reuse.
Java Packages – Packages are collection of similar nature classes. A package can be created by using the keyword ‘package’ and the keyword used to include a package in our program is ‘import’. Java contains an extensive library of pre-written classes grouped together into packages –
java.lang.* It is a default package containing String, Math, Integer, thread etc.
java.io.* It is the basic Input Output package of Java
java.util.* The java utility package
java.applet.* The applet package
java.net.* The Networking package of Java
java.awt.* It stands for Abstract Windows Toolkit, for designing Windows based applications.
java.lang.* It is a default package containing String, Math, Integer, thread etc.
java.io.* It is the basic Input Output package of Java
java.util.* The java utility package
java.applet.* The applet package
java.net.* The Networking package of Java
java.awt.* It stands for Abstract Windows Toolkit, for designing Windows based applications.
Basic Features of Java:-
1) Write once run anywhere(WORA)
2) Platform Independent
3) Offers many security features to make its program safe and secure
4) Built in graphics & supports multimedia
5) Light Weight code
6) Open Product… i.e., freely available to all.
1) Write once run anywhere(WORA)
2) Platform Independent
3) Offers many security features to make its program safe and secure
4) Built in graphics & supports multimedia
5) Light Weight code
6) Open Product… i.e., freely available to all.
Blue – J
Blue-J is an Integrated Development Environment (IDE) for writing, editing, compiling, testing, executing and debugging the Java programming language, developed mainly for educational purposes, but also suitable for small-scale software development. Blue J was initially designed at University of Kent by David Barns and Michael Kollins
Blue-J is an Integrated Development Environment (IDE) for writing, editing, compiling, testing, executing and debugging the Java programming language, developed mainly for educational purposes, but also suitable for small-scale software development. Blue J was initially designed at University of Kent by David Barns and Michael Kollins
The main screen graphically shows the class structure of an application under development (in an UML-like diagram), and objects can be interactively created and tested. This interaction facility, combined with a clean, simple user interface, allows easy experimentation with objects under development.
Features of Blue – J
1. Simplicity of the interface – The GUI user friendly interface is simpler than in full-scale professional environments, and thus easier to learn.
2. The “Code Pad” – The code pad is a tool that instantly evaluates arbitrary Java expressions and statements.
3. Regression testing
4. Provision for syntax highlighting
5. Built in debugger.
6. Program input/output i.e. In Blue-J both the parameter values for and the return values from method calls can be entered / inspected directly.
7. Java ME support – Java ME (Micro Edition) projects can be developed and deployed from Blue-J.
1. Simplicity of the interface – The GUI user friendly interface is simpler than in full-scale professional environments, and thus easier to learn.
2. The “Code Pad” – The code pad is a tool that instantly evaluates arbitrary Java expressions and statements.
3. Regression testing
4. Provision for syntax highlighting
5. Built in debugger.
6. Program input/output i.e. In Blue-J both the parameter values for and the return values from method calls can be entered / inspected directly.
7. Java ME support – Java ME (Micro Edition) projects can be developed and deployed from Blue-J.
IDE for Java (other than Blue J)
Sl IDE Brief Description
1 Eclipse Free, open source IDE. The most popular of the Java IDEs, but harder to set up and configure than the commercial ones.
2 J Builder Lots of J2EE support including UML and app server integration.
3 Emacs This is a powerful, free editor that does color syntax highlighting, automatic indentation, and parenthesis/brace balancing.
4 M7 Nitro X Expensive but very powerful J2EE IDE based on Eclipse.
Sl IDE Brief Description
1 Eclipse Free, open source IDE. The most popular of the Java IDEs, but harder to set up and configure than the commercial ones.
2 J Builder Lots of J2EE support including UML and app server integration.
3 Emacs This is a powerful, free editor that does color syntax highlighting, automatic indentation, and parenthesis/brace balancing.
4 M7 Nitro X Expensive but very powerful J2EE IDE based on Eclipse.
Java Tokens & Character Set
It is a set of valid characters that a language can recognize. The character set of Java is UNICODE. This has been selected due its vastness of characters. It is 2 byte character code. The first 128 characters of Unicode are identical to the ASCII character set.
Tokens are smallest individual units of a Java program. There are 5 different types of tokens in Java –
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators Hint: Try to remember KILOS
It is a set of valid characters that a language can recognize. The character set of Java is UNICODE. This has been selected due its vastness of characters. It is 2 byte character code. The first 128 characters of Unicode are identical to the ASCII character set.
Tokens are smallest individual units of a Java program. There are 5 different types of tokens in Java –
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators Hint: Try to remember KILOS
Keywords are the reserved words which convey a special meaning to a language compiler. They can not be used for any other purpose like function name, variable name or object names.
There are around 48 keywords, of which const and goto have been reserved even though they are not currently being used for any purpose true, false and null might appear to be keywords but actually they are literals, and hence are reserved words.
There are around 48 keywords, of which const and goto have been reserved even though they are not currently being used for any purpose true, false and null might appear to be keywords but actually they are literals, and hence are reserved words.
Identifiers are fundamental building blocks of a program. They are named memory locations or simply names given to variables, objects, classes, functions, arrays etc… They are case sensitive.
There are certain Identifier naming rules –
1. They can consist of infinite number of alphabets and digits
2. They must start with an alphabet and not with a digit
3. They can not be a keyword or reserved word
4. They cannot have any spaces
5. They cannot contain any special character other than _(underscore), $(dollar)
1. They can consist of infinite number of alphabets and digits
2. They must start with an alphabet and not with a digit
3. They can not be a keyword or reserved word
4. They cannot have any spaces
5. They cannot contain any special character other than _(underscore), $(dollar)
While naming identifiers there are certain conventions that make them identifiable
1. Class names begins with an Uppercase character
2. Identifiers having multiple words, the first character of 2nd and subsequent words are in Uppercase
3. Constants are in all CAPITALS
1. Class names begins with an Uppercase character
2. Identifiers having multiple words, the first character of 2nd and subsequent words are in Uppercase
3. Constants are in all CAPITALS
Comments (Remarks):- refers to statements which are ignored by the java compiler and increase the readability of a program. These are used to state a purpose of the instructions used in a program.
Literal are the fixed values that are assigned to variables which do not change its values during program execution. They are of 5 types –
1.Integer Literal
i.Decimal
ii.Octal
iii.Hexadecimal
1.Integer Literal
i.Decimal
ii.Octal
iii.Hexadecimal
To represent Decimal Integers we simply use the integer type number as literal, e.g. int p = 45; So, System.out.println (p); // Shows 45 as output
To represent Octal Integers however we precede the number with a 0 (zero) as literal, e.g. int p = 045; System.out.println (p); // Shows 37 as output coz, (45) in octal is 37 in decimal number system.
Moreover to represent any Hexadecimal Integer we use 0x (Zero and lowercase letter x) preceding the number as hexadecimal literal e.g. int p = 0x45;
System.out.println (p); // Shows 69 as output coz, (45) in hexadecimal is 69 in decimal number system.
System.out.println (p); // Shows 69 as output coz, (45) in hexadecimal is 69 in decimal number system.
2. Real Literals
i. Floating point literals have 7 digits of precision after the decimal point
ii. Double literals have 16 digits of precision after decimal point
i. Floating point literals have 7 digits of precision after the decimal point
ii. Double literals have 16 digits of precision after decimal point
3. Character Literals are enclosed within a pair of single quotes
char ch = ‘A’;
Character type user inputs are taken in the following way –
e.g. char ch = (char) in.read ( ) ;
char ch = ‘A’;
Character type user inputs are taken in the following way –
e.g. char ch = (char) in.read ( ) ;
4. Boolean Literal
There are only 2 types of boolean literals i.e. true & false the default being false.
There are only 2 types of boolean literals i.e. true & false the default being false.
5. Null Literal
Operator – are the entities or symbols that tell the compiler that what operation has to be performed with one, two or three number of operands within a given expression.
There are approximately a set of 37 different operators that Java uses
Operators can either operate on 1, 2 or 3 operands and accordingly named as –
Operator – are the entities or symbols that tell the compiler that what operation has to be performed with one, two or three number of operands within a given expression.
There are approximately a set of 37 different operators that Java uses
Operators can either operate on 1, 2 or 3 operands and accordingly named as –
Unary (works on 1 operand) e.g. + + and – –
Binary (works on 2 operands) e.g. +, – , * , / , >, , = , > , << )
Special Operators (this , dot (.) , new, (type) , instanceof )
Binary (works on 2 operands) e.g. +, – , * , / , >, , = , > , << )
Special Operators (this , dot (.) , new, (type) , instanceof )
Data types – are the entities that tell the compiler that which variable will hold what kind of values.
Primitive data types – Also called as Basic Data type. They are pre defined or built in data types because they are already built in java. E.g. byte, short, int, long, float, double, char, boolean.
Non Primitive Data Types: Directly or indirectly dependent on primitive data types. They store the data values with reference to the address or locations. So, they are called as Reference Data Types. E.g. array, classes interfaces etc…
Primitive data types – Also called as Basic Data type. They are pre defined or built in data types because they are already built in java. E.g. byte, short, int, long, float, double, char, boolean.
Non Primitive Data Types: Directly or indirectly dependent on primitive data types. They store the data values with reference to the address or locations. So, they are called as Reference Data Types. E.g. array, classes interfaces etc…
Range, Size and hierarchy of various primitive data types
Type ….Size…. Range
boolean 1 byte (uses 1 bit) true and false
byte 8 bits (1 bytes) – 128 to + 127
char 16 bits (2 bytes) 0 to 65,535
short 16 bits (2 bytes) – 32768 to +32767
int 32 bits (4bytes) – 2 31 to + 231 – 1
long 64 bits (8bytes) – 2 63 to + 263 – 1
float 32 bits (4bytes) – 3.4E+38 to +3.4E+38
double 64 bits (8bytes) – 1.7E +308 to 1.7E+308
Type ….Size…. Range
boolean 1 byte (uses 1 bit) true and false
byte 8 bits (1 bytes) – 128 to + 127
char 16 bits (2 bytes) 0 to 65,535
short 16 bits (2 bytes) – 32768 to +32767
int 32 bits (4bytes) – 2 31 to + 231 – 1
long 64 bits (8bytes) – 2 63 to + 263 – 1
float 32 bits (4bytes) – 3.4E+38 to +3.4E+38
double 64 bits (8bytes) – 1.7E +308 to 1.7E+308
Type conversion The process of convert one primitive datatype to another either from lower precision to higher or vice versa I known as type conversion. This can be achieved in two ways –
Implicit Data Type Conversion – Such Type of conversion is performed by the compiler without programmer’s intervention. In this type of conversion a datatype of lower precision gets converted to a datatype with higher precision.
e.g. int a = 10;
double d = a;
System.out.println(d);
The above code snippet gives 10.0 as output; this clearly indicates that the int datatype got automatically converted to double.
Implicit Data Type Conversion – Such Type of conversion is performed by the compiler without programmer’s intervention. In this type of conversion a datatype of lower precision gets converted to a datatype with higher precision.
e.g. int a = 10;
double d = a;
System.out.println(d);
The above code snippet gives 10.0 as output; this clearly indicates that the int datatype got automatically converted to double.
Explicit data type Conversion – Such type of conversion is user defined conversion which forces an expression to be converted into specific type. Generally this is done to force a conversion from higher precision data type to lower.
e.g. double pi = 3.1415;
int i = pi;
e.g. double pi = 3.1415;
int i = pi;
The above statement is considered illegal and hence gives an ERROR message “POSSIBLE LOSS OF PRECISION”
Hence the above code should be Type Casted in the following manner –
int i = (int) pi;
System.out.println(i);
Now, the output to the above code will be 3
Hence the above code should be Type Casted in the following manner –
int i = (int) pi;
System.out.println(i);
Now, the output to the above code will be 3
Potential problems with Type Casting
1. When a datatype is converted from double to float results in loss of precision i.e. a double datatype has 16 digits after decimal point which gets truncated to 7 digits.
2. When a datatype is converted from double or float to integer type results in loss of entire fractional part leaving behind only the integral part.
3. When a higher order integer datatype like int or long is converted to byte or short might result in the data going out of range.
1. When a datatype is converted from double to float results in loss of precision i.e. a double datatype has 16 digits after decimal point which gets truncated to 7 digits.
2. When a datatype is converted from double or float to integer type results in loss of entire fractional part leaving behind only the integral part.
3. When a higher order integer datatype like int or long is converted to byte or short might result in the data going out of range.
Take an example
int a = 130;
byte b = (byte) a;
System.out.println(b);
int a = 130;
byte b = (byte) a;
System.out.println(b);
The output for the above code is – 126
Explanation for the above –
The above code tries to type cast int to byte, the data range for int is – 2 31 to + 231 – 1 whereas that of byte is – 128 to + 127, hence think in terms of a Number Line, when we start filling 130 in the number line starting from 0 (zero) we reach the end of the line at 127 and so the filling process continues from the –ve end and finally reaches – 126, hence the output…
The above code tries to type cast int to byte, the data range for int is – 2 31 to + 231 – 1 whereas that of byte is – 128 to + 127, hence think in terms of a Number Line, when we start filling 130 in the number line starting from 0 (zero) we reach the end of the line at 127 and so the filling process continues from the –ve end and finally reaches – 126, hence the output…
Operators in Java
Operator – are the entities or symbols that tell the compiler that what operation has to be performed with one, two or three number of operands within a given expression.
There are approximately a set of 37 different operators that Java uses
Operators can either operate on 1, 2 or 3 operands and accordingly named as –
- Unary (works on 1 operand) e.g. + + and – –
- Binary (works on 2 operands) e.g. +, – , * , / , >, <, == etc…
- Ternary (works on 3 operands) e.g. ? :
Operators can categorized as –
- Arithmetic Operators (+ , – , * , / , %)
- Relational Operators (> , < , >= , <= , == , !=)
- Logical Operators (&&, || , !)
- Conditional Operators (? : )
- Bitwise Operators (&, | , ~ , ^)
- Shift Operators (>> , << )
- Special Operators (this , dot (.) , new, (type) , instanceOf )
Shortcut Operations in Java
Java implements shortcut methods to work with basic arithmetic operators like
+, – , * , / and %
Hence, a = a + 5; can be written as a + = 5;
And the above + = operator is called Addition Assignment operator.
Similarly we have –
- Subtraction Assignment – =
- Multiplication Assignment *=
- Division Assignment /=
- Modulus Assignment %=
Unary Operators
Post/Pre – Increment and Post/Pre – Decrement Operators
a = a + 1 can also be written as either a++ (Post Increment) or ++a (Pre Increment)
a = a – 1 can also be written as either a– – (Post Decrement) or – –a (Pre Decrement)
The difference between Pre and Post is –
In a Postfix operator like Post Increment or Post Decrement the value of the operand is first used and then increased or decreased as the case may be for e.g.
int a = 5;
System.out.println(a++);
Gives 5 as output as the value of ‘a’ is first used and then increased to 6, so if we have one more print statement after the above stated line will give 6 as output.
System.out.println(a); // Output is 6
In a Prefix operator like Pre Increment or Pre Decrement the value of the operand is first increased or decreased as the case may be and then used for e.g.
int a = 5;
System.out.println(++a);
Gives 6 as output as the value of ‘a’ is first incremented and then displayed.
Relational Operators are used to find the relationship between two quantities, whether they are equal, un-equal, greater than less than etc…
Logical Operators are used to join 2 or more relational operators and of 3 types –
- && (and) returns true when both or all the relations are true
- || (or) returns true when any one of the relations are true
- ! (not) inverts true to false and false to true
Say if the value of a=4, b=6 then…
if(a>5 && b>5) will return false coz both the conditions are not true
if(a>5 || b>5) will return true coz one of the conditions is true
if(!true) will return false
It is not mandatory to have a relation always inside an ‘if’, even without any ‘if’ clause we can check the return of any relation like –
Say the value of x=7; then
System.out.println(x==7); will give the output as true, and if we place (!) in front of the relation, System.out.println( ! (x==7)); will invert the output from true to false.
How can i get your autograph bro? 😂
ReplyDeleteDon't be so eager man..you will 😂😉
ReplyDeleteDon't be so eager man..you will 😂😉
ReplyDelete