JAVA IMPORTANT POINTS TO REMEMBER



CORE-JAVA IMPORTANT POINTS TO REMEMBER

Q: Why C++ is Object Based Programming Language and Why Java is Object Oriented Programming Language?

Ans: C++ is also an OOP Language!

Java syntax is C++ minus pointers, templates, multiple inheritance, operator overloading....

Maybe java enforces OO programming in a stricter manner than C++, whereas C++ is more flexible and has to be used more sensibly

The main difference between object based and object oraiented language is the language which supports concepts of inheritance and dynamic binding is known as object oriented and the language which doesn’t support these 2 concepts is called object based language.

Q: Is true that JAVA is not 100% object oriented language?

Ans:Java is not 100% object oriented because it does not support multiple inheritance. Java is not 100% OOP's, because it uses primitive data types like Boolean, short, int, long etc.

Q: What are the differences between script language and programming language?

Ans: Scripting languages run inside another program. Scripting languages are not compiled. Scripting languages are easy to use and easy to write. Scripting languages today are used to build complex software.

Define Java in one line?

Ans: Compiled once can run in anywhere. [Platform independent]

(write once run anywhere.)

Class Def: A blue print from which objects are created.

Object Def: An object is a software bundle of state (variables) and behaviors (methods).

(Or)

Instance of class is object (using new operators)

Encapsulation : Encapsulation is a process that allows selective hiding of properties and methods in a class.

Ex: private, public see /home/sivashankar/Documents/JAVA STUFF/Sun Java 2/session 1.ppt#Encapsulation

Abstraction: Data abstraction is a process of identifying properties and methods related much to a particular entity as relevant to the application

Abstract

➢ Memory in Java is automatically garbage collected. You never have to worry about memory corruption.

➢ Array initialization: int[] a = new int[100];

Translator: is a program that convert computer program into machine language. It is of 3-types

a. Interpreter: converts line by line at a time.

(or)

Convert bytecode into machine code. (in java)

b. Compiler: Read all lines and convert all lines into machine language.

(or)

Convert source code into bytecode. (in java)

c. Assemble: assembly level language is converted into m/c level lang.

Stream : represents flow of data from one plac to another place. (~pipe)

three types of streams are there: In system class we have 3 fields they are

System.out ----display o/p on monitor etc.,

System.in -----by default is keyboard

System.err ----to o/p error msg

How can i attach keyboard to InputstreamReader?

A: InputStreamReader obj=new InputStreamReader(System.in);

BufferedReader br=new BufferdReader(obj);

char ch = br.read(); ----reads single char.

Static Method: is a method that is called and executed without using any object. Syntax: Classname.methodName();

Ex:System.out.print(x);

Accepting a float/double/int value from Keyboard:

float n=Float.parseFloat(str);

Command line arguments:

Ex:c:\> java Arr 10 11 12

args[0]=10 | args[0]=Arr ------------(in C lang)

args[1]=11 | args[1]=10

args[2]=12 | args[2]=11, args[3]=12

String: string is a group of characters.

String is class in java.lang.String. (user defined data type)

Ex: String str=”Helo”;

String s1=new string();

◆ JVM is written in C-language.

◆ .class or byte code is System independent. It contains byte code instructions understandable by JVM.

◆ .exe is contains m/c code understandable by the microprocessor, it is system dependent

◆ virus is self replicating. It will not spread in text file. Virus cannot spread in .class because it is text file.

◆ java is case sensitive.

◆ Exception represents runtime error. If any runtime error occur it does not terminate,but in C/C++ if any runtime error occur then the program will abnormal termination occur.

◆ String getName() - to get the name of the class

◆ Each word of class names and Interface names start with a Capital letter. Method names start with small letter, then each word start with capital letter. Ex: parseInt();

What is the difference b/w the #include and import ?

◆ #include makes the compiler to copy the entire header file code into a C/C++ program hence, the program size increases abnormally thus it wastes memory and processor time.

◆ import statement makes JVM to goto a particular package, execute the code there and substitute the results into the java program. JVM will not copy any Java program therefore import is more efficient than #include.

Can you write main() without any arguments?

Ans: No, JVM recognize only “main(String args[])”

What is hash code?

It is the unique ID number given to every object by the JVM. Hash code is also called reference number.

What is difference b/w == operator and equal method?

== operator compares the reference of the string object.

equal method compares contents of the string objects.

What is the difference b/w object oriented PL and object based PL?

Object oriented PL follow all the features of OOP's Eg:C++, Java, Smalltalk,simula67.

Object based program language follow all the features of OOP's except Inheritance . Ex: Java script, VB script.

What is the difference between instance variables and static variable?

Instance variable is a variable whose separate copy is available to each object. If the value of instance variable is changed in one object it will not effect the other object.

Static variable is a variable whose single copy is shared by all the object, any modification to the static variable will effect all th objects.

What is the difference between Assignment and Initialization?

Assignment can be done as many times as desired whereas initialization can be done only once.

Wrapper Classes: Wrapped classes are classes that allow primitive types to be accessed as objects..[ Casting primitive data type.]

or

wrapper classes are those which are used to change primitive datatype values into object references. So that they can be serializable. some examples of wrapper classes are:- Integer,Float,Byte,Character,Long..

1. widening : casting a lower data type into higher data type.

2. Narrowing : converting a higher data type into a lower data type .

Rule : primitive data types represent single value. whereas advanced data types (reference) represents a group of values.

Rule: we cannot convert one class type into another type unless those classes have relationship by the way of inheritance.

Explain the differences between interface and class in Java

Interface has no implementation where class provides implementation for interface

What is the use of bin and lib in JDK?

– Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib contains API and all packages.

Binding: Connecting a method call( i.e. ) to a method body( i.e. Function) is called binding.

What is final, finalize() and finally?

final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.

finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.

finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.

CONSTRUCTOR

Constructor is a special kind of method that determines how an object is initialized when created.

a. A constructor has the same name as the class.

b. A constructor has no return value.

c. A constructor is always called with the new operator.

Controlling Access to Members of a Class

What are different types of access modifiers?

public: Any thing declared as public can be accessed from anywhere.

private: Any thing declared as private can’t be seen outside of its class.

protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

default modifier : Can be accessed only to classes in the same package.

|Specifier |class |subclass |package |world |

|private |X | | | |

|protected |X |X* |X | |

|public |X |X |X |X |

|package |X | |X | |

Method overriding:

Def: Creating two methods with the same name but,there is no change in type and arguments. Example of Inheritance is Method overriding. It has super and subclass.

Is null, sizeof is a keyword?

The null, sizeof is not a keyword.

What are Transient and Volatile Modifiers?

Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.

Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.

What is the Vector class?

The Vector class provides the capability to implement a grow able array of objects.

What are inner class and anonymous class?

Inner class : classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.

Anonymous class : Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.

What is the difference between yielding and sleeping?

When a task invokes its yield() method, it returns to the ready state.

When a task invokes its sleep() method, it returns to the waiting state..

Abstract Class---method without body

a. Inheritance.

b. Instance of class can not be created.

c. An abstract class has at least one abstract method.

Example:

A programmer uses abstract class when there are some common features implemented in the same way in all of its objects. A programmer writes an interface when ever features is having different implementation in different objects. Here implementation leaved to third party vendors.

Concrete method: a method with body

final

1.prevents its content from being modified.(no inheritance).

2. Instance of class can be created.

3. Final is similar to constant in c/c++.

4. Final methods cannot be overridden.

Example:

public class Math

{

. . .

public static final double PI =3.14159265358979323846;

. . .

}

You can access this constant in your programs as Math.PI.

5. The use of final to a variable is similar to the use const in C.

AWT: The abstract windowing toolkit or group of classes for writing programs with graphical user interfaces.

• Objects---call by reference

methods---call by value

• If x=22.9 then

floor(x)=22

round(x)=23

ceil(x)=23

this() :

a. this() is used to invoke a constructor of the same class

Ex:

super() :

a. super() is method to call the constructor of the super class from the sub class

b. super.methodnameInsuperclass(arg) is used to execute any method of the superclass from the subclass

✓ Rule : Calling the superclass constructor must be the first statement in subclass constructor.

Ex:

Q: The java.util package contains two classes that are designed to work with hashtables. They are _______ and _________.

A: Hashtable , HashMap class

Q: Why is it necessary to hash?

A: Hashing assures that the format of keys is uniform and unique

Q: If a Runtime Exception is thrown in the finalize method

A: The exception is simply ignored and the object is garbage collected

Q: final handles exception is false.

Q: What is the output of the following program? class example { public static void main(String args[]) { int j; do { j++; } while(j < 0); System.out.println(j); } }

A: The program does not compile as j is not initialized

Q: Which of the following is used by a thread to enter the waiting state?

A: wait() ,sleep(), suspend() methods.

Q: In order to use a servlet for handling the submission of a web form, subclass HttpServlet() and put service code in

A: doGet() or doPost() method

Q: The list of locations for the compiler to search in order to resolve class names at compile time is called

A: build path

1. Memory in Java is automatically garbage collected. You never have to worry about memory corruption.

2. Array initialization: int[] a = new int[100];

3. CONSTRUCTOR

a. A constructor is a special method whose purpose is to construct and initialize objects.

b. A constructor has the same name as the class.

c. A constructor has no return value.

d. A constructor is always called with the new operator.

4.

5. Polymorphism:

If same method is performing various tasks.

• Example of polymorphism is Method overloading.

• Ex1: Method overloading

Class sample {

Void add(int a,int b)

{

int c=a+b;

System.out.println (“add is =”+c);

}

Void add (int a, int b,int c) {

int d=a+b+c;

System.out.println (“add is =”+d);

}

}

Class main {

Public static void main(string args[]) {

Sample obj=new sample();

Obj.add(5,10);

Obj.add(5,10,15);

}

}

• Ex2: you can assign a subclass object to a superclass variable.

Employee e;

e = new Employee(. . .); // Employee object expected

e = new Manager(. . .); // OK, Manager can be used as well

6. Package:

• Def: Java allows you to group classes in a collection called a package.

• Using packages:

import java.util.*;

Then you can use

Date today = new Date();

◆ Adding a class into a package

To place classes inside a package, you must put the name of the package at the top of your source file, before the code that defines the classes in the package. For example, the file Employee.java starts out like this:

package com.horstmann.corejava;

public class Employee

{

. . .

}

– What is a transient variable?

Ans: A transient variable is a variable that may not be serialized. .

11. Is null a keyword?

Ans: The null value is not a keyword..

• Is sizeof a keyword?

Ans: The sizeof operator is not a keyword..

• What is the Vector class?

Ans: The Vector class provides the capability to implement a growable array of objects.

• What is the difference between yielding and sleeping?

Ans: When a task invokes its yield() method, it returns to the ready state.

When a task invokes its sleep() method, it returns to the waiting state..

• What are wrapped classes?

Ans : Wrapped classes are classes that allow primitive types to be accessed as objects..

• Abstract Class

← Inheritance.

← Instance of class can not be created.

Example:

• final

← prevents its content from being modified.(no inheritance).

← Instance of class can be created.

← Final is similar to constant in c/c++.

← Final methods cannot be overridden.

Example:

public class Math

{

. . .

public static final double PI =3.14159265358979323846;

. . .

}

You can access this constant in your programs as Math.PI.

[pic]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download