internet technology
Describe the difference b/w
variable holding primitive value as compared with object reference through
program
The
main difference is that primitive variables hold values, while object variables
hold references to values. You can think of an object variable has holding the
location of the value, rather than the value itself. This is somewhat similar
to the concept of pointers in C and C++. However, it's somewhat more abstract
in Java, since you can't actually directly read or manipulate the memory
location in an object reference (and, in fact, it may not always be the same).
In java, we have the primitives such as int, double, boolean, short, long....
These parts of the language are lengths of bytes. They can only hold a value.
But also we have API types such as Integer, Double, Boolean, Short, Long...
(note the caps). Objects have: states, values, and behaviors. One 'thing' holds
a collection of different types information. The Object types can all interface
with java's Collections but the primitives c In java, we have the primitives
such as int, double, boolean, short, long.... These parts of the language are
lengths of bytes. They can only hold a value.
But also we have API types such as Integer, Double, Boolean, Short, Long... (note the caps)
Objects have: states, values, and behaviors. One 'thing' holds a collection of different types information. The Object types can all interface with java's Collections. The primitives cannot.
You assign a primitive...
int x = 10;
You create an Object...
Integer integer = new Integer( 101 );
// with the Objects you have built-in capabilities for Sets, Sorts, Randomization
// Objects have dot methods
String s = String.valueOf( integer );
int len = s.length(); an not.
But also we have API types such as Integer, Double, Boolean, Short, Long... (note the caps)
Objects have: states, values, and behaviors. One 'thing' holds a collection of different types information. The Object types can all interface with java's Collections. The primitives cannot.
You assign a primitive...
int x = 10;
You create an Object...
Integer integer = new Integer( 101 );
// with the Objects you have built-in capabilities for Sets, Sorts, Randomization
// Objects have dot methods
String s = String.valueOf( integer );
int len = s.length(); an not.
Explain inter
thread communication?
The
communication between one thread to another thread is called inter-thread
communication.
We can achieve this by using
- wait()
This method tells the calling thread to give up the monitor
and go to sleep until some other thread enters the same monitor and calls
notify( ).
- notify()
This method wakes up the first thread that called wait( ) on
the same object.
3.
notifyAll()
This method wakes up all the threads that called wait( ) on
the same object. The highest priority thread will run first.
These methods are implemented as final methods
in Object, so all classes have them. All three methods can be called only from
within a synchronized context. These methods are declared within Object.
Various forms of wait( ) exist that allow you to specify a period of time to
wait.
Describe the purpose of html applet tag?
Applet is any small application
that performs one specific task that runs within the scope of a larger program,
often as a plug-in. The term is frequently used to refer to a Java applet, a
program written in the Java programming language that is designed to be placed
on a web page. The word applet was first used in 1990 in PC Magazine. Java
Applets can provide web applications with interactive features that cannot be
provided by HTML. Since Java's bytecode is platform-independent, Java applets
can be executed by browsers running under many platforms, including Windows, Unix,
Mac OS, and Linux. When a Java technology-enabled web browser processes a page
that contains an applet, the applet's code is transferred to the client's
system and executed by the browser's Java Virtual Machine (JVM). An HTML page
references an applet either via the deprecated <APPLET> tag or via its
replacement, the <OBJECT> tag.
Ex.
<applet code="appletfile.class" width="500" height="650">
Java applet
</applet>
Describe Enhanced for loop through program?
The enhanced for loop will go through the array arr and set
i to each member of the "int" array.
class Program {
public static void main(String[] args) {
int []arr = {1,2,3,4};
for ( int i : arr ) {
arr[i] = 0;
}
for ( int i : arr ){
System.out.println(i);
}
}
}
public static void main(String[] args) {
int []arr = {1,2,3,4};
for ( int i : arr ) {
arr[i] = 0;
}
for ( int i : arr ){
System.out.println(i);
}
}
}
Why it
output : 0030? Let's take a look:
int []arr =
{1,2,3,4};
for ( int i : arr ) {
arr[i] = 0;
}
for ( int i : arr ) {
arr[i] = 0;
}
- Step 1. On the first iteration, i will be 1 because arr[0]=1, and set arr[1] to 0.
- Step 2. On the second iteration, i will be 0 because arr[1]=0 set by Step 1., and set arr[0] to 0.
- Step 3. On the third iteration, i will be 3 because arr[2]=3, and set arr[3] to 0.
- Step 4. On the last iteration, i will be 0 because arr[3]=0 set by Step 3, and set arr[0] to 0.
Another
sample code using "Enhanced For Loop" for multi-dimensional arrays:
class Program {
...
static void useEnhancedFor(int x[][]) {
for(int[] l:x) {
for(int m:l) {
System.out.print(m+" ");
}
}
System.out.println();
}
}
...
static void useEnhancedFor(int x[][]) {
for(int[] l:x) {
for(int m:l) {
System.out.print(m+" ");
}
}
System.out.println();
}
}
Describe invoking overridden
method with example?
Defining a method in the subclass that has the same name,
same arguments and same return type as a method in the super class and it hides
the super class method is called method overriding. Now when the method is
called, the method defined in the subclass is invoked and executed instead of
the one in the super class.
class Xsuper
{
int y;
Xsuper(int y)
{
this.y=y;
}
void display()
{
System.out.println("super
y = " +y);
}
}
class Xsub extends Xsuper
{
int z;
Xsub(int z , int y)
{
super(y);
this.z=z;
}
void display()
{
System.out.println("super
y = " +y);
System.out.println("sub
z = " +z);
}
}
public class TestOverride
{
public static void main(String[]
args)
{
Xsub s1 =
new Xsub(100,200);
s1.display();
}
}
Output :
C:\java> javac TestOverride.java
C:\java> java TestOverride
super y = 200
sub z = 100
The method
display () defined in the subclass is invoked.
Difference between string object and string literal.
A String literal is a sequence of characters between
quotation marks, such as "string" or "literal". String
objects are immutable. That means, once created, a String object cannot be
changed (short of using something like reflection to get at private data).
What is the String Literal Pool? It is a collection of
String objects. Although that's close, it's not exactly correct. Really, it's a
collection of references to String objects. Strings, even though they are
immutable, are still objects like any other in Java. Objects are created on the
heap and Strings are no exception. So, Strings that are part of the "String
Literal Pool" still live on the heap, but they have references to them
from the String Literal Pool. When a .java file is compiled into a .class file,
any String literals are noted in a special way, just as all constants are. When
a class is loaded (note that loading happens prior to initialization), the JVM
goes through the code for the class and looks for String literals. When it
finds one, it checks to see if an equivalent String is already referenced from
the heap. If not, it creates a String instance on the heap and stores a
reference to that object in the constant table. Once a reference is made to
that String object, any references to that String literal throughout your
program are simply replaced with the reference to the object referenced from
the String Literal Pool.
These are a few of the highlights you can keep in mind when
it comes to String literals:
- Equivalent String Literals (even those stored in separate classes in separate packages) will refer to the same String object.
- In general, String Literals are not eligible for garbage collection. Ever.
- Strings created at run-time will always be distinct from those created from String Literals.
- You can reuse String Literals with run-time Strings by utilizing the intern() method.
- The best way to check for String equality is to use the equals() method.
Difference b/w class and interface.
1. Interface is little bit like a class... but interface is
lack in instance variables....that's u can't create object for it.....
2. Interfaces r developed to support multiple inheritance...
3.the methods present in interfaces r pure abstract..
4. The access specifiers public, private,protected r possible with classes. but the interface uses only one spcifier public.
5. Interfaces contain only the method declarations no definitions.
2. Interfaces r developed to support multiple inheritance...
3.the methods present in interfaces r pure abstract..
4. The access specifiers public, private,protected r possible with classes. but the interface uses only one spcifier public.
5. Interfaces contain only the method declarations no definitions.
6. The interfaces are used in java to implementing the
concept of multiple inheritances here as classes are not used to implement
multiple inheritances.
7. We are not allocating the memory for the interfaces whereas the memory is allocated for
the classes.
8. Interfaces are always implemented whereas classes are always extended.
7. We are not allocating the memory for the interfaces whereas the memory is allocated for
the classes.
8. Interfaces are always implemented whereas classes are always extended.
Define scope and life time of a variable.
The Scope and Lifetime of Variables:
- We can declare variables within any block.
- Block is begun with an opening curly brace and ended by a closing curly brace.
- 1 block equal to 1 new scope in Java thus each time you start a new block, you are creating a new scope.
- A scope determines what objects are visible to other parts of your program. It also determines the lifetime of those objects.
Variable Scope in Java Programming
// Demonstrate block scope.
class Scope {
public static void main(String args[])
{
int n1; // Visible in main
n1 = 10;
if(n1 == 10)
{
// start new scope
int n2 = 20; // visible only to this block
// num1 and num2 both visible here.
System.out.println("n1 and n2 : "+ n1 +""+ n2);
}
// n2 = 100; // Error! y not known here
// n1 is still visible here.
System.out.println("n1 is " + n1);
}
}
Output :
n1 and n2 : 10 20
n1 is 10
- n1 is declared in main block thus it is accessible in main block.
- n2 is declared in if block thus it is only accessible inside if block.
- Any attempt to access it outside block will cause compiler time error.
- Nested Block can have access to its outermost block. [if block is written inside main block thus all the variables declared inside main block are accessible in if block]
Explain final, finally and finalize.
final:
final is a keyword. The variable decleared as final should be initialized only once and cannot be changed. Java classes declared as final cannot be extended. Methods declared as final cannot be overridden.
final variable is a constant.
final method cann't be overrid.
final class cann't be subclassed.
finally:
finally is a block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling - it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. finally is a block usually used to release all the resources utilized inside the try block such as to free resources like stream objects, to close sockets .The code in finally block is guaranteed of execution irrespective of occurrence of exception catched/uncatched. Finally is a block that is always executed after a try-catch block. This ensures that certain code gets executed even if an unexpected exception occurs. The finally block can not be overridden by a return, continue, or break. For example…
try {
...do some stuff here
} catch (SomeKindOfException e) {
System.err.println("SomeKindOfException caught: " + e);
} finally {
if (good) {
System.out.println("Everything looks good");
...your clean up code here
}
else {
System.out.println("Something got messed up");
}
}
return;
finalize:
finalize
is a method. Before an object is garbage collected, the runtime system calls
its finalize() method. You can write system resources release code in
finalize() method before getting garbage collected. Finalize() is a method is
called by the garbage collector on an object when the JVM determines that there
are no more references to it and it is marked for deletion. For example if you
needed to make sure some block of code (mySpecialCleanUpMethod()) was called
before your object was permanently deleted your finalize method would look
like…
protected void finalize() {
mySpecialCleanUpMethod();
}
Difference b/w literal and const keyword
A literal is a value that is expressed as itself rather than as a variable's value or the result of an expression, such as the number 3 or the string "Hello". A constant is a meaningful name that takes the place of a literal and retains this same value throughout the program, as opposed to a variable, whose value may change.
if String s1=”abc”;
String s2= new String(“abc”);
String s3=”abc”;
Which statement is true?
1. (s1==s2); 2. (s1==s3); 3.(s1.egual(s2));
A String literal is a java
language concept. This is a String literal: "a String literal"
A String object is an
individual instance of the java.lang.String class.
String s1 = "abcde";
String s2 = new String("abcde");
String s3 = "abcde";
are all valid but have a slight
difference: s1 will refer to an interned String object. This means, that
the character sequence "abcde" will be stored at a central place and
whenever the same literal "abcde" is used again, the JVM will not
create a new String object but use the reference of the 'cached' String.
s2 is guranteed to be a new String
object, so in this case we have:
(s1 == s2) is false
(s1 == s3) is true
(s1.equals(s2)) is true
Comments
Post a Comment