SIDE-BANNERSIDE-BANNER * Oh God! Let there be PEACE. Make them calm, gentle, humble and stable. Make them realize their mistakes. Make them to confess, seek and pray for forgiveness. Let there be forgiveness for true hearts. Show them the right path on which they can walk. Let there be justice, satisfaction and happiness. Let them know that you are the justice and truth. Let them know that the innocent is never punished. Give them the strength, courage and wisdom to quit bad and to achieve good purpose. Let there be leaders who are followed by goodness. Make them competitive to achieve good. Remove their greed, wickedness and bad ambitions. Let them know that your love is infinite. Speak to them in simple ways so they understand. May your heart hear their pure hearts. * Never say die.Live and let live.* Accept Good if not, it Perishes.* What you Sow is what you Reap.* United we Stand divided we Fall.* Where there is a Will there is a Way.* Love is the strongest Power on earth.* When going gets Tough the Tough gets going.* For every problem created there is a perfect Solution.* When your hand starts paining you don't Cut it, Cure it.* A good purpose of life is the persistence to be constructive.* The arch of justice is wide, but it bends towards freedom and justice.* Lion king says he wants all the Power but none of the Responsibilities.* Life is a puzzle which can be solved by Hard work, Character and Discipline.* Money is like Manure, Spread it and it does good.* Pile it up in one place and it Stinks.* When a man wants to be a King he should exactly know the responsibilities of a King.* The only thing necessary for evil to triumph is for good men to do nothing - Edmund Burke.* Face is the reflection of mind.* A purpose of life is a life of purpose.* Beauty lies in the eyes of the beholder.* Necessity is the mother of all inventions. Real friends are those who help us in troubles.* Freedom and Power are lost if it’s miss utilized. Repeating mistakes will lead to sin and blunders.* Quantity is appreciated only when it has Quality.* Everyday is a new day, which brings hope with it.* Ego of superiority is the destruction of individuality.* We cannot learn to swim without going into the water.* Everything happens for good and thus leads to destiny.* Every problem has a perfect solution, we need to find them.* A good purpose of life is the persistence for constructiveness.* It’s hard to create good things where as it’s easy to break them.* Ideas are appreciated and respected only when we express them.* Mistakes do happen by humans, they are forgiven when we pray.* Freedom means giving others the right to achieve good purposes.* We have to put our efforts and leave the rest for destiny to decide.* All big things start with a first step.* First step is sometimes difficult.* Prayers come true when the purpose is good, thus pray for everyone.* Dreams come true when we have faith and pray for good things in life.* We got to have strength, courage and wisdom to achieve good things in life.* Every relationship has a meaning; we have to give them the proper meaning.* The only thing necessary for the triumph of evil is for good men to do nothing.* If wealth is lost, nothing is lost. If health is lost, something is lost. But, if character is lost, everything is lost.* “Stand up, be bold, be strong. Take the whole responsibility on your own shoulders, and know that you are the creator of your own destiny.” - Swami Vivekananda.
HOME-PAGEREALIZATIONQUOTESPUZZLESPRAYERSPERCEPTIONSMUSIC-DOWNLOADSTORIESJOKES
BOOKSBITTER-TRUTHANCIENT-SCRIPTURESBEAUTIFUL-LIFETHOUGHTSFRIENDSHIPPRAYERS-TO-WORSHIPWinning-Publications

QUANTITY is appreciated only when it has QUALITY. Recitation is the mother of Memory. Necessity is the mother of Invention. Knowledge grows when distributed. Enrichment for Information Technology. Persistence to be constructive is the key to success.

Monday, February 18, 2008

CORE-JAVA (Class, Variable & Method)

*[READ: Why-JAVA] *[Start-Learning-JAVA] *[Why-Java's-Hot] *[Start-Using-LINUX] *[Java-Q-&-A] *[Question] *[Java-SE-6-Doc] *[Struts-Doc] *[Java-Tutorial-Index] *[Java-Certification]
*[Tech.] *[Struts.] *[Servlet.] *[JSP.] *[EJB.] *[JNDI-JMS] *[SQL.] *[JDBC.]
CORE-JAVA: *[OOP] *[CLASS] *[ABSTRACT] *[EXCEPTIONS] *[THREADS] *[UTILS] *[PACKAGES] *[JVM] *[CASTING] *[NETWORKING] *[RMI-XML] * Add to Google


* What is the difference between a constructor and a method? (corejava) (Constructor) (Method) (Test(open))

A: A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

* What is the difference between declaring a variable and defining a variable? (Variable) (Declaring Variable)
A: In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization. e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.

* What is the default value of an object reference declared as an instance variable?
A: null unless we define it explicitly.

* What type of parameter passing does Java support? (Pass by value) (Argument)
A: In Java the arguments are always passed by value .

* Are Primitive data types passed by reference or passed by value? (Argument)
A: Primitive data types are passed by value.

* Are Objects passed by value or by reference?
A: Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.

* What is passed by reference and what by value ?
All Java method arguments are passed by value. However, Java does manipulate objects by reference, and all object variables themselves are references.

* What are pass by reference and pass by value? (corejava)
A: Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.

* Which class should you use to obtain design information about an object? (Class)
The Class class is used to obtain information about an object’s design.

* Does Java provide any construct to find out the size of an object?
A: No there is not sizeof operator in Java. So there is not direct way to determine the size of an object directly in Java.

* Is sizeof a keyword in C/C++ language?
The sizeof operator is not a keyword.

* What is the Local class? (Local)
A: The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region. A local class is a nested class that is not a member of any class and that has a name.

* What is the difference between a field variable and a local variable? (variable)
A field variable is a variable that is declared as a member of a class. A local variable is a variable that is declared local to a method.

* What is a LOCAL, MEMBER and a CLASS variable?
Variables declared within a method are LOCAL variables.
NON-STATIC Variables declared within the class i.e not within any methods are MEMBER variables (INSTANCE variables).
STATIC Variables declared within the class i.e not within any methods and are defined as static are CLASS variables.

* Why are there no "global variables" in Java, as in C language?
Global variables is a concept of C language which is considered as wrong, for a variety of reasons: Adding state variables breaks referential transparency. State variables lessen the cohesion of a program. In terms of Java, "global variables" are similar to "public static MEMBER variables".
In the spirit of encapsulation, it is a best practice to have member variables as private. However we can access these values indirectly with public get() and set() methods, which can obtain the values of member variables. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state, When we add one variable, we limit the use of our program to one instance. What we thought was global, someone else might think of as local: they may want to run two copies of our program at once. For these reasons, Java decided to ban global variables.

* How are this() and super() used with constructors? (constructor) (Super) (this Keyword) (Super Keyword)
A: Keyword this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

* Are true and false keywords? (keyword) (Boolean)
No, the values true and false are not keywords, they are Boolean values.

* In System.out.println(),what is System, out and println, explain.
A: "System" is a predefined final class, "out" is a PrintStream object and "println" is a built-in overloaded method in the out object.

* Can a private method of a superclass be declared within a subclass? (Requirements in Overriding and Hiding)
A subclass can declare a method with the same signature as a private method in one of its superclasses. But it cannot override the method.
A private field or method or inner class belongs to its declared class and hides from its subclasses. There is no way for private stuff to have a runtime overloading or overriding.

* What restrictions are placed on method overriding? (overriding)
Overridden methods must have the same name, argument list, and return type.
The overriding method may not limit the access of the method it overrides.
The overriding method may not throw any exceptions that may not be thrown by the overridden method.
Methods may be overridden to be more public, not more private.

* What is Overriding? (overriding)
A: When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass. When the method is invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass.
Methods may be overridden to be more public, not more private.

* What is the difference between method overriding and overloading? (overriding)
Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments (signature).
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures. Methods are overridden on a signature-by-signature basis.

* What are the Object and Class classes used for?
The OBJECT class is the highest-level class in the Java class hierarchy.
Class OBJECT is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
The CLASS class is used to represent the classes and interfaces that are loaded by a Java program. Instances of the class CLASS represent classes and interfaces in a running Java application.

* What is the difference between inner class and nested class? (inner)
When a class is defined within a scope of another class, it becomes a inner class.
If the access modifier of the inner class is static, then it becomes nested class.
Nested classes are divided into two categories: static and non-static.
Nested classes that are declared "static" are simply called "static nested classes".
"Non-static" nested classes are called "inner classes".

* What modifiers may be used with an inner class that is a member of an outer class? (inner)
A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.
"Non-static nested classes" (inner classes) have access to other members of the enclosing class, even if they are declared private.
"Static nested classes" do not have access to other members of the enclosing class.
As a member of the OuterClass, a nested class can be declared private, public, protected, or package private (default).
Outer classes can only be declared public or package private (default).

* What are different types of inner classes? (inner)
A: Static Nested class, Member inner classes(Non Static), Local classes and Anonymous classes
* Static Nested classes - If we declare a class within a class and specify the static modifier, the compiler treats the class just like any other top-level class. Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. Top-level inner classes implicitly have access only to static variables. There can also be inner interfaces. All of these are of the nested top-level variety.
* Member inner classes (Non Static) - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested top-level class. The primary difference between member classes and nested top-level classes is that member classes have access to the specific instance of the enclosing class.
* Local classes - We can declare an inner class within the body of a method. Such a class is known as a "local inner class".
Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a more publicly available interface. Because local classes are not members, the modifiers public, protected, private, and static are not usable.
* Anonymous classes - We can also declare an inner class within the body of a method without naming it. These classes are known as "anonymous inner classes". Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.

* Is null a keyword? (keyword)
Null is a value and not a keyword.

* What is a native method? (native)
A native method is a method that is implemented in a language other than Java.

* What are "order of precedence" and "associativity", and how are they used?
Order of precedence determines the order in which operators are evaluated in expressions.
Associativity determines whether an expression is evaluated left-to-right or right-to-left. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first.
* All "Binary operators" except for the assignment operators are evaluated from left to right. Binary operators are often called logical operators AND, OR ( && || )
* All "Assignment operators" are evaluated right to left.
There are 12 assignment operators, ( = *= /= %= += -= <<= >>= >>>= &= ^= |= )
Ex: a=b=c means a=(b=c) (right-to-left)
* class Test {
---public static void main(String[] args) {
---int i = 2;
---int j = i * (i=3);
---System.out.println(j); // output is 6 (* is left to right)
---i = 2;
---int j = (i=3) * i;
---System.out.println(j); // output is 9
---System.out.println(++j); // output is 10
---System.out.println(j++); // output is 10
---System.out.println(j); // output is 11
---j += (j = 3);
---System.out.println(j); // output is 14
---j = j + (j = 3);
---System.out.println(j); // output is 17 } }

* If a variable is declared as private, where may the variable be accessed? (variable)
A private variable may only be accessed within the class in which it is declared.

* Which class is extended by all other classes?
The Object class is extended by all other classes.

* How is rounding performed under integer division?
The fractional part of the result is truncated. This is known as rounding toward zero. Also see: Math, DecimalFormat. Primitive Conversion: Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Narrowing conversions may lose information about the overall magnitude of a numeric value. (Constant Values of integers)
* Widening:
# byte to short, int, long, float, or double
# short to int, long, float, or double
# char to int, long, float, or double
# int to long, float, or double
# long to float or double
# float to double
* Narrowing:
# byte to char
# short to byte or char
# char to byte or short
# int to byte, short, or char
# long to byte, short, char, or int
# float to byte, short, char, int, or long
# double to byte, short, char, int, long, or float

* Does a class inherit the constructors of its superclass?
A class does not inherit constructors from any of its superclasses. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

* What is the purpose of the System class?
The purpose of the System class is to provide access to system resources.

* Name the eight primitive Java types.
The eight primitive types are byte, char, short, int, long, float, double, and boolean.

* I made my class Cloneable but I still get "Can't access protected method clone". Why?
A class implements the Cloneable interface to indicate the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.
We have to override/implement our own public clone() method, even if it doesn't do anything special other than calling super.clone(). All the below statements are true for a clone:
x.clone() != x
x.clone().getClass() == x.getClass()
x.clone().equals(x)

* Difference between a Class and an Object ?
A CLASS is a definition or prototype whereas an OBJECT is an instance or living representation of the prototype

* Describe what happens when an object is created in Java?
Several things happen in a particular order to ensure the object is constructed properly:
* Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implemenation-specific data includes pointers to class and method data.
* The instance variables of the objects are initialized to their default values.
* Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed.
* The constructor for the most derived class is invoked. The first thing a constructor does is call the consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

* What is constructor chaining and how is it achieved in Java ?
A child object constructor always first needs to construct its parent (which in turn calls its parent constructor). In Java it is done by an implicit call to the no-args constructor as the first statement.
The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

* Is "abc" a primitive value?
The String literal "abc" is not a primitive value. It is a String object.

* You can create a String object as String str = "abc"; Why cant a button object be created as Button btn = "abc";? Explain.
The main reason you cannot create a button by Button btn= "abc"; is because "abc" is a literal string (something slightly different than a String object, by-the-way) and btn is a Button object.
The only object in Java that can be assigned a literal String is java.lang.String. Ex:
Button btn = new Button("String-Name");
Important to note that we are NOT calling a java.lang.String constuctor when we type String s = "abc", as it is a special support given by the Java language. The String class is not technically a primitive "data type". String objects are immutable, which means that once created, their values cannot be changed.

* To what value is a variable of the String type automatically initialized?
The default value of an String type is not null, but an empty character.
String str = new String(); // represents an empty character sequence.
System.out.println( str.length() ); // is "0" and not "null"

* What is the output from System.out.println("Hello"+null);
Hellonull

* Can a method be overloaded based on different return type but same argument type ?
No, because the methods can be called without using their return type in which case there is ambiguity for the compiler.

* Is a class a subclass of itself?
Yes, a class is a subclass of itself.

* What is the difference between a while statement and a do statement? (statement) (while-statement)
A: A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

* What is the purpose of a statement block? (statement) (Expressions, Statements, and Blocks)
A statement block is used to organize a sequence of statements as a single statement group.

* What is the difference between an if statement and a switch statement? (statement) (if-then-Statement) (Switch-statement)
The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed.
The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

* What is the difference between a break statement and a continue statement? (statement) (Break-and-Continue-Statement)
A break statement results in the termination of the statement to which it applies (switch, for, do, or while).
A continue statement is used to end the current loop iteration and return control to the loop statement.

* What restrictions are placed on the values of each case of a switch statement? (statement)
During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.

* How are commas used in the initialization and iteration parts of a for statement? (statement) (for-statement)
Commas are used to separate multiple statements within the initialization and iteration parts of a for statement. Ex:
for (initialization; termination; increment) { statement(s) }

* Can a for statement loop indefinitely? (statement)
Yes, a for statement can loop indefinitely. For example, consider the following: for(;;) ;

Difference between Swing and Awt? (corejava)
A: AWT are heavy-weight components. Swings are light-weight components. Hence swing works faster than AWT.

What if the main method is declared as private? (main)
A: The program compiles properly but at runtime it will give an error message "Main method not public.".

What if the static modifier is removed from the signature of the main method? (main)
A: Program compiles. But at runtime throws an error "NoSuchMethodError".

What if we write static public void instead of public static void? (main)
A: Program compiles and runs properly.

What if we do not provide the String array as the argument to the method? (main)
A: Program compiles but throws a runtime error "NoSuchMethodError".

What is the first argument of the String array in main method? (main)
A: The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.

If we do not provide any arguments on the command line, then the String array of Main method will be empty or null? (main)
A: It is empty. But not null.

How can one prove that the array is not null but empty? (main)
A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

* What is the argument type of a program's main() method? (main)
A program's main() method takes an argument of the String[] type.

Can an application have multiple classes having main method? (main)
A: Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is no conflict amongst the multiple classes having main method.

Can I have multiple main methods in the same class? (main)
A: No the program fails to compile. The compiler says that the main method is already defined in the class.

* What is the return type of a program’s main() method? (main)
A program’s main() method has a void return type.

How are Observer and Observable used?
A: Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. (Ex.)
The Observer interface is implemented by objects that observe Observable objects.

How does Java handle integer overflows and underflows?
A: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

* What are the legal operands of the instanceof operator?
The left operand is an object reference or null value, and the right operand is a class, interface, or array type.
(Ex:) Parent obj1 = new Parent(); Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); // true
Output: obj1 instanceof Parent: true

* What are E and PI? (Math)
E is the base of the natural logarithm and PI is mathematical value pi.
java.lang.Math (Constant Values)
public static final double - E - 2.718281828459045
public static final double - PI - 3.141592653589793

* To what value is a variable of the boolean type automatically initialized?
The default value of the boolean type is false

* What is the difference between the prefix and postfix forms of the ++ operator?
The prefix form performs the increment operation and returns the value of the increment operation. (Ex: ++int)
The postfix form returns the current value of the expression and then performs the increment operation on that value. (Ex: int++)

* How can we convert a numeric IP address like 192.18.97.39 into a hostname like java.sun.com? (Networking)
String hostname = InetAddress.getByName("192.18.97.39").getHostName();

* Which non-Unicode letter characters may be used as the first character of an identifier?
The non-Unicode letter characters "$" or "_" may appear as the first character of an identifier.

* Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
The digits 0 through 9 may NOT be used as the first character of an identifier but they may be used after the first character of an identifier. (RFID and Auto-ID)

* How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
Note: "Bit" is the smallest unit of information in a computer, with a value of either 0 or 1.
"Byte" is a sequence of eight bits.
"Bytecode" is the Machine-independent code generated by the Java-Compiler and executed by the Java Interpreter.

* What is the range of the char type?
The range of the char type is (0) to (2^16 - 1).
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

* What is the difference between the Boolean "& operator" and the "&& operator"?
If an expression involving the Boolean "& operator" is evaluated, both operands are evaluated. Then the & operator is applied to the operand.
When an expression involving the "&& operator" is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.
Ex: if (a=b & b=c) compare both a=b and b=c.
if (a=b && b=c) compare b=c only if a=b is true.

* Which Java operator is right associative (from right to left)?
There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a. The = operator is right associative.
The conditional operator (?) is syntactically right-associative.
(it groups right-to-left), so that a?b:c?d:e?f:g means the same as a?b:(c?d:(e?f:g)).

* What is the advantage of the event-delegation model over the earlier event-inheritance model?
The event-delegation model has two advantages over the event-inheritance model. (Link:1) (Link:2)
First, it enables event handling to be handled by objects other than the ones that generate the events (or their containers). This allows a clean separation between a component's design and its use.
The other advantage of the event-delegation model is that it performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process un-handled events, as is the case of the event-inheritance model.

* What is the % operator?
It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.

* Is the ternary operator written (x : y ? z) or (x ? y : z) ?
It is written x ? y : z.
Ex: result = someCondition ? value1 : value2; // if true then value1, else value2.

* What is a "stateless" protocol ?
HTTP is a stateless protocol, there is no retention of state between a transaction which is a single request response combination. Each command is executed independently, without any knowledge of the commands that came before it.

* What is the difference between logical data independence and physical data independence?
Logical Data Independence - meaning immunity of external schema to changes in conceptual schema.
Physical Data Independence - meaning immunity of conceptual schema to changes in the internal schema.
(Data Independence)
* Internal schema describing details of data storage and their access paths.
* Conceptual schema, describing a model of the real world the database represents.
* External schema, one for each user to represent, hiding what that user is not interested in.
* Logical data independence: capacity to change the conceptual schema without having to change external schema (and the applications using it) – e.g. adding new concepts, or new relations, to the conceptual schema will not influence external views already defined of new concepts.
* Physical data independence: capacity to change the internal schema without having to change conceptual (or external) schema – e.g. changing the way the physical files are stored.

* What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

* What is the range of the short data type?
The range of the short type is -(2^15) to 2^15 - 1. The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

* Why can't I say just abs() or sin() instead of Math.abs() and Math.sin()?
The import statement does not bring methods into our local name space. It lets us abbreviate class names, but not get rid of them altogether.
Moreover java.lang.Math is a "final" class which means it can't be extended.

* Why does it take so much time to access an Applet having Swing Components the first time?
Because behind every swing component are many Java objects and resources. This takes time to create them in memory. JDK 1.3 from Sun has some improvements which may lead to faster execution of Swing applications.

* Why are the methods of the Math class static? (static)
So they can be invoked as if they are a mathematical code library.

* What is the difference between static and non-static variables? (static)
A: A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

* What does it mean that a method or field is "static"? (static)
A: Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work out is a static field in the java.lang.System class.

* What do you mean by static methods? (static)
By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

* What is static in java? (static)
A: Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class. Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, it is not possible to override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.

* What is the difference between a static and a non-static inner class? (static)
A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.

* What happens to a static var that is defined within a method of a class? (static)
Can’t do it. You’ll get a compilation error.

* How many static init can you have? (static)
As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.

* What is final? (final) (sun link)
A: A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).

* Write the Java code to declare any constant (say gravitational constant) and to get its value. (final)
---Class ABC
----{ static final float GRAVITATIONAL_CONSTANT = 9.8;
-----public void getConstant()
-----{ system.out.println("Gravitational_Constant: " + GRAVITATIONAL_CONSTANT); } }

* What does it mean that a class or member is final? (final)
A final class can no longer be subclassed. Mostly this is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. Methods may be declared final as well. This means they may not be overridden in a subclass. Fields can be declared final, too. However, this has a completely different meaning. A final field cannot be changed after it’s initialized, and it must include an initializer statement where it’s declared. For example, public final double c = 2.998; It’s also possible to make a static field final to get the effect of C++’s const statement or some uses of C’s #define, e.g. public static final double c = 2.998;

* What does the “final” keyword mean in front of a variable? A method? A class? (final)
FINAL for a variable: value is constant. FINAL for a method: cannot be overridden. FINAL for a class: cannot be derived.

* What is a transient variable?
Transient variable is a variable that may not be serialized. Transient meaning: not lasting longer, temporary, staying only a short time. Variables may be marked transient to indicate that they are not part of the persistent state of an object.
If an instance of the class Point:
class Point { int x, y;
transient float rho, theta; }
were saved to persistent storage by a system service, then only the fields x and y would be saved. This specification does not specify details of such services; see the specification of java.io.Serializable for an example of such a service.

What is serialization? (serialize)
A: Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.

How do I serialize an object to a file? (serialize)
A: The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a FileOutputStream. This will save the object to a file.

Which methods of Serializable interface should I implement? (serialize)
A: The serializable interface is an empty interface, it does not contain any methods. So we do not implement any methods.

How can I customize the seralization process? i.e. how can one have a control over the serialization process? (serialize)
A: Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.

What is the common usage of serialization? (serialize)
A: Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serialized.

What is Externalizable interface? (serialize)
A: Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods. Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in).

What happens to the object references included in the object? (serialize)
A: The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized along with the original object.

What one should take care of while serializing the object? (serialize)
A: One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

What happens to the static fields of a class during serialization? (serialize)
A: There are three exceptions in which serialization does not necessarily read and write to the stream. These are 1. Serialization ignores static fields, because they are not part of any particular state. 2. Base class fields are only handled if the base class itself is serializable. 3. Transient fields.

* How many methods do u implement if implement the Serializable Interface? (serialize)
The Serializable interface is just a "marker" interface, with no methods of its own to implement. Other 'marker' interfaces are
java.rmi.Remote
java.util.EventListener

* Why does JComponent have add() and remove() methods but Component does not? (Swing)
Because JComponent is a subclass of Container, and can contain other components and jcomponents.

* Can applets communicate with each other? (applet)
A: At this point in time applets may communicate with other applets running in the same virtual machine. If the applets are of the same class, they can communicate via shared static variables. If the applets are of different classes, then each will need a reference to the same class with static variables. In any case the basic idea is to pass the information back and forth through a static variable. An applet can also get references to all other applets on the same page using the getApplets() method of java.applet.AppletContext. Once we get a reference to an applet, we can communicate with it by using its public members. It is conceivable to have applets in different virtual machines that talk to a server somewhere on the Internet and store any data that needs to be serialized there. Then, when another applet needs this data, it could connect to this same server. Implementing this is non-trivial. A Java class can be used both as an applet as well as an application by adding a main() method to the applet.

* Why isn't there operator overloading?
Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn't even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().

* What is Singleton Design Pattern?
Its a design used to create only one instance of a class, or to control the number of object creations. The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.
Singletons often control access to resources such as database connections or sockets. For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time. (Ex:)
public class MySingleton {
--private static MySingleton single;
--private MySingleton() { MySingleton.getInstance(); // construct object }
--public static synchronized MySingleton getInstance() {
---if (single==null) { single = new MySingleton(); }
--return single; } }

Reflection. Java.lang.reflect. Helps to examine or modify the runtime behavior of applications. Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods.

* * * * * * * * * * * * *
*[Read: Significance of Nava-ratna (Nava-Graha).] *[Read: Amazing Constructive Vibrations.] *[Liberate the Sole.]

Read: Peace Home Download Music Prayers Life Is Beautiful
Winning Publications.
*Significance of Nava-ratna (Nava-Graha). *Amazing Constructive Vibrations. *The piercing of the ears (karnavedha) . *Nature Cure. *Steps to improve work place. *Steps Involved In Relationships. *Some of the aspects which materialize the relationships.

No comments:

About Me

My photo
Simple guy who believes in being Competitive rather than being Ambitious. Persistence to be constructive, without frustrations, is a good purpose of life.
.

* * * * * * * * * * * * *

MeetUp Message Board:
Read: Wishes-and-Prayers. *Issue-of-Illegal-Immigration-&-Happy-Kingdom. *The-New-York-Hillary-Rodham-Clinton *Cast-God-Religion! *Barack-Obama-Meetup *Duty- Responsibility-Law-and-Order *John-Edwards-One-America *Good-Wishes-Life-is-Meaningful-&-Beautiful *Dennis-Kucinich-Meetup *Let-there-be-peace! *Bill-Richardson-for-President-2008 *Logic-of-Good-and-Bad-convert-bad-to-good *MORAL-STORY-Elephants-held-by-small-ropes.
* * * * * * * * * * * * *

Realizations.
*Realizations-In-Real-Life-Please-be-gentle-and-humble. *Manoj-D-Kargudri. *Amazing-Constructive-Vibrations. *Astrology. *Creating-Leaders. *How-ideas-are-concluded-and-decisions-are-materialized. *“Relationships-in-Life”-“Partnerships-in-Life”. *The-path-of-victory-the-path-of-life-winning-in-looseing. *An-attempt-for-definition. *Speak-with-a-heart. *These-are-contagious. *Key-to-happy-kingdom. *MIRACLES. *Better-to-create-one! *Cast-God-and-Religion! *Manoj-Kargudri. *Things-become-inevitable! *We-are-all-looking-for! *Phase-of-Life. *Destiny-Karma-and-God. *Struggle-perfection-and-Money. *Independence-and-Freedom. *Relationships-and-Happiness.
* * * * * *

Quotes.
*Love-Compassion-Tolerance-Forgiveness-Courtesy. *Manoj-D-Kargudri. *True-to-Heart-going-back-to-Basics!
* * * * * *

Puzzles-Riddles-Think.
*River-Crossing-Puzzles-Brain-Teasers. *Manoj-Kargudri. *Perpetual-Motion-Gravity-and-Kinetics. *Illusions-Paradoxes-Perpetually-ascending-staircase. *Milk-man-with-no-measureing-jar. *Amazing-Horoscope-Mind-Reader. *Find-the-hidden-images-in-these-STEREOGRAMS. *Are-they-12-or-13? *What-would-U-do? *ABCD-Four-Digit-Number. *10-Digit-Number. *4-QUESTIONS...GOOD-LUCK! *Think-Wise.
* * * * * *

Prayers.
*God-gave-us-everything-we-ever-needed. *Good-Wishes. *Manoj-Kargudri. *Love-Compassion-Tolerance-Forgiveness-Courtesy. *Interview-With-God! *He-is-the-one! *Candle-of-Hope! *Let-there-be-Peace! *Manoj-Kargudri.
* * * * * *

Perceptions.
*Issue-of-Illegal-Immigration. *To-which-religion-does-this-universe-belongs-to? *Law-and-order-helps-to-maintain-justice. *Implementing-regulations-by-justice. *Putting-our-sincere-efforts. *Religion-and-cast. *Impact-of-reservation-based-on-religion-and-cast. *Free-and-Fare-Education-system-Electoral-system.
* * * * * *

Stories.
*The-Monkey’s-Justice-for-two-cats. *The-Blind-Men-And-The-Elephant. *Manoj-Kargudri. *Two-rich-merchants-and-a-thief. *Healing-is-contagious. *Two-saints-in-a-market-place. *A-Terrible-Fight-Between-Two-Wolves. *Hen-that-laid-golden-eggs. *Healing-forgiveness-and-affection. *Elephants-held-by-small-ropes. *Story-of-Punyakoti-the-strength-of-truth. *What-is-the-reason? *Reply-Depends-on-the-Question. *Critical-mass-Experiment. *The-Brahman's-Wife-and-the-Mongoose. *The-Boy-Who-Cried-Wolf. *Difference-between-Heaven-and-Hell! *Freedom-and-Prison! *It's-in-Your-Eyes!
* * * * * *

Jokes.
*Please-listen-to-me. *The-Silent-Treatment! *Surgeon-Vs-Mechanic. *Manoj-Kargudri. *God's-doing-a-lot-better-job-lately.
* * * * * *

The-Bitter-Truth.
*Duty-Responsibility-Law-and-Order. *"Happy-Kingdom"-proudly-proclaimed-as-"Paradise". *Trying-to-learn-something-for-the-first-time. *Time-is-the-only-solution. *For-every-Action-there-is-an-Equal-and-Opposite-Reaction. *Logic-of-Good-and-Bad. *Manoj-Kargudri. *Duties-Responsibilities-verses-Luxuries-and-Pleasures. *Beggars!
* * * * * *

Life-is-Beautiful.
*Creating-successful-constructive-personality. *Why-God-Gave-Us-Friends? *Way-to-Paradise. *Creating-the-Beautiful-World. *Doing-the-job-of-goodness. *Life-is-Meaningful. *Manoj-Kargudri. *The-A-to-Z-for-Goodness. *Life-is-full-of-Tests. *History-Proves-That. *Love-in-different-forms. *True-to-the-heart.
* * * * * *

Prayers-To-Worship.
*Please-do-not-leave-me-ever. *Let’s-make-it-happen. *Faith-in-Patience. *The-only-one-I've-got. *Someone-somewhere. *How-I-Love-You. *Will-You? *Successful-Life. *Manoj-Kargudri. *Please-say-something. *Way-to-Paradise. *My-Everything.
* * * * * *

Friendship.
*Life-Still-Has-A-Meaning. *Heavenly-Garden. *GOD-SPEAK-TO-ME! *Why-God-Made-Friends? *I-asked-the-Lord! *A-Best-Friend! *Why-GOD-Gave-Us-Friends? *Portrait-of-a-Friend. *Friends-till-the-end. *Some-Assorted! *Forever-Friends. *What-is-a-friend!
* * * * * *

Winning-Publications.
*Significance-of-Nava-ratna (Nava-Graha). *Amazing-Constructive-Vibrations. *Manoj-Kargudri. *The-piercing-of-the-ears (karnavedha) . *Nature-Cure. *Steps-to-improve-work-place. *Steps-Involved-In-Relationships. *Some-of-the-aspects-which-materialize-the-relationships.
* * * * * *

Music-Download.
*Bhakti-Songs. *Manoj-Kargudri. *English-Songs. *Gazal-Bhajan-Hindi. *Hindi-Life. *Hindi-Love. *Hindi-Old-Kishore. *Hindi-Old-Mukesh. *Hindi-Old-Songs. *Hindi-Rock. *Hindi-Pops. *Instrumental. *Vocal-Ragas.
* * * * * *

Technology.
*READ: Why-JAVA *Manoj-Kargudri. *Start-Learning-JAVA *Why-Java's-Hot *Start-Using-LINUX *Java-Q-&-A *Question *Java-SE-6-Doc *Struts-Doc *Java-Tutorial-Index *Java-Certification *Tech. *Struts. *Manoj-Kargudri. *Servlet. *JSP. *EJB. *JNDI-JMS *SQL. *JDBC. *CORE-JAVA: *OOP *CLASS. *Manoj-Kargudri. *ABSTRACT *EXCEPTIONS *THREADS *UTILS *PACKAGES *JVM *CASTING *NETWORKING *RMI-XML
* * * * * *

MUSIC-DOWNLOAD.
*Hindi-Vocal-Raga. *Hindi-Remix-Rock. *Hindi-Old-Songs. *Hindi-Mukesh-Mohd-Rafi-Songs. *Hindi-LOVE-Songs. *Hindi-Remix-LIFE. *English-Rock-Songs. *Kannada-Janapada-Geete. *Kannada-Film-Songs. *Kannada-Devotional-Songs. *Instrumental-Music. *Manoj-Kargudri. *Hindi-Pop-Songs. *Hindi-Bhakti-Songs. *Hindi-Kishore-Kumar-SAD. *Hindi-Kishore-Kumar-JOY. *Hindi-R-D-Burman. *Hindi-Gazals. *English-Soft-Songs.
* * * * * *

Add to Google