Java Thoery
Java Thoery
What is Class?
What is Object?
Encapsulation?
Abstraction?
Polymorphism?
Inheritance?
Polymorphism: Overloading
ANS.: Overloading: Same method name with different arguments. Methods within Same class
ANS.: Declared as static and final. E.g.: static final int PI = 2.14
ANS.: Application: Stand Alone & execution starts with main ().
JVM?
ANS.: JVM converts source code to byte code not understandable for humans.
Packages in Java?
ANS.: lang, awt, javax, swing, net, io, util, sql etc.
Keywords, identifiers, literals, operators and punctuators are tokens available in Java.
Keywords are the words with special meaning associated with them. These are reserved for special purpose and must not be
used as normal identifier names. Some keywords of Java are default, return, if, private etc.No, keywords cannot be used as
identifiers.
When the objects need to interact with one another, they pass/request information to/from one-another. This interaction is
known as message passing. Objects interact with one another through messages.
Information hiding is the process of hiding all the secrets of an object that do not contribute to its essential characteristics. The
structure of object is hidden, the implementation of its methods. Only the essential characteristics of object are visible.
The behaviour of an object is described through associated functions called methods.In Java, every class is a subclass of Object
class and so, all objects inherit the methods of the Object class. Thus, it is not possible for objects to be without any methods.
The parameters that appear in the method call statement are called actual parameters.
The parameters that appear in the method definition are called formal parameters.
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Q. What is the condition of using a function in an expression ?
The condition of using a function in an expression is that it must return some value. The return type of the function should not
be void.
Q. How can objects be initialized with desired values at the time of object creation ?
Objects can be initialized with desired values at the time of object creation by using parameterized constructor and passing the
desired values as arguments at the time of creation of object.
Wrapper classes are specially designed classes that act as wrappers to primitive data types so that primitive values can be
accessed as objects. For example, Integer is a wrapper class for int data type and Float is a wrapper class for float data type.
The automatic conversion of primitive data type into an object of its equivalent wrapper class is known as Autoboxing.
The automatic conversion of an object of wrapper class into primitive data type is known as Auto-unboxing.
• public — The public members are accessible in all the classes whether a subclass or any other class in same package or
another package.
• private — The private members are accessible only inside their own class and nowhere else.
• protected — The protected members are accessible inside all the classes in their own package as well as in all
subclasses of their class.
• default — The default members are accessible inside all the classes of the same package.
Each element in an array is referred to by their subscript or index. The index of an element refers to the position of an element
in the array. In Java, the indices start from 0 and go on till size - 1.
Q. Determine the number of bytes required to store an int array namely A[23].
An int data type requires 4 bytes in memory. Therefore, the storage space required by array A[ ] will be 23 x 4 = 92 bytes.
We can convert a numeric value enclosed in a string format into a numeric value by using valueOf() method.
For example, the given statement will return the numeric value of the String argument "15".
• boolean
• String
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Q. What is an Infinite Loop
A for statement that does not include any statement inside the body. Eg for(i=1;i<=10;i++);
It’s the first line of method access specifier return type, function name and its parameter.
Function which can change the state of an object are called impure function
It is use to create a new Object or new Array. Eg. Int a[] = new int[10]
Void
To pass a primitive data to a function that uses Wrapper object as function argument
To store primitive value to object. To provide conversion system from String to Primitive and Vice versa
To create a delay
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
while loop do-while loop
while loop checks the test condition at the do-while loop checks the test condition at the
beginning of the loop. end of the loop.
Object Class
Object is a real world entity such as pen, laptop, Class is a blueprint or template from which
mobile, bed, keyboard, mouse, chair etc. objects are created.
Actual parameters are copied to formal Formal parameters refer to actual parameters.
parameters.
Any changes to formal parameters are not The changes to formal parameters are
reflected onto the actual parameters. reflected onto the actual parameters.
Limited to the same class. Accessible within the same class & subclasses
Protected Public
Accessible within the same class & subclasses Accessible from any class or package
Bubble Sort involves comparing adjacent Selection sort involves finding appropriate
elements and swapping them. element for every position and swapping it with
existing element at that position.
One-dimensional array stores data in a Two-dimensional array stores data in a grid or table,
single row or column. with rows and columns.
It uses one index to access array elements. It uses two indices to access array elements.
equals() compareTo()
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
This function only ckecks whether two This function checks whether a string is equal bigger
strings are identical or not. or smaller than other string or not.
String StringBuffer
String type object has fixed length. StringBuffer type object can change the length.
This statement is executed based on the Switch statements execute as per the user decision
condition inside the if-else statement
This statement is used to choose between This statement is used to choose among multiple
two options options.
Constructor Method
It is used to create and initialize an Object. Method is used to execute certain statements.
A constructor cannot have any return type. A method can have a return type.
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Ternary Operators:
1.Rewrite the following program segment using the if-else statements instead of the ternary operator.
String grade = (mark >= 90) ? “A” : (mark >= 80) ? “B” : “C”;
Ans.
String grade;
if(marks >= 90) {
grade = “A”;
} else if( marks >= 80 ) {
grade = “B”;
} else {
grade = “C”;
}
2.Rewrite the following program segment using the if ..else statement.
comm = (sale>15000)?sale*5/100:0;
Ans.
if (sale>15000)
comm=sale*5/100
else
comm=0;
3.Rewrite the following statement using suitable ‘if()’ statement.
int ans = res > 365 ? 180/3:90/ 3;
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
Ans:
int ans;
if( res > 365)
ans= 180/3;
else
ans= 90/ 3;
true or false
x=10; y=5; z=(++y + y-- + y-- + x-- + --x) x = _____? y = _____? z = _____?
TalentHome: Theory Quick Revision Sheet – For Classes call: 9773088112
String Functions:
toString() Convert Wrapper classes like Integer, Float , Double or Long to String
parseInt() Convert String to Integer parseFloat() Convert String to Float
parseDouble() Convert String to Double
String s = “Computer” ; s.subsString(0) – returns “Compter” s.subsString(5) – returns “ter” s.subsString(2,5) – returns “mpu”