Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Core Java

Core Java

Ratings: (0)|Views: 260|Likes:
Published by api-26483995

More info:

Published by: api-26483995 on Oct 14, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Core Java
Q) What is difference between Java and C++?

A) (i) Java does not support pointers. Pointers are inherently insecure and troublesome. Since pointers do not exist in
Java. (ii) Java does not support operator overloading. (iii) Java does not perform any automatic type conversions that
result in a loss of precision (iv) All the code in a Java program is encapsulated within one or more classes. Therefore,
Java does not have global variables or global functions. (v) Java does not support multiple inheritance.
Java does not support destructors, but rather, add the finalize() function. (vi) Java does not have the delete operator.
(vii) The << and >> are not overloaded for I/O operations

Q) OOPs concepts
Polymorphism
Ability to take more than one form, in java we achieve this using Method Overloading (compile time polymorphism),
Method overriding (runtime polymorphism)
Inheritance
Is the process by which one object acquires the properties of another object. The advantages of inheritance are
reusability of code and accessibility of variables and methods of the super class by subclasses.
Encapsulation
Wrapping of data and function into a single unit called encapsulation. Ex:- all java programs.
(Or)

Nothing but data hiding, like the variables declared under private of a particular class are accessed only in that class and cannot access in any other the class. Or Hiding the information from others is called as Encapsulation. Or Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.

Abstraction
Nothing but representing the essential futures without including background details.
Dynamicbinding
Code associated with a given procedural call is not known until the time of the call at runtime. Dynamic binding
is nothing but late binding.

Q) class & object?
class\ue000 class is a blue print of an object
Object\ue000 instance of class.

Q) Object creation?
Object is constructed either on a memory heap or on a stack.
Memory heap

Generally the objects are created using the new keyword. Some heap memory is allocated to this newly created
object. This memory remains allocated throughout the life cycle of the object. When the object is no more referred,
the memory allocated to the object is eligible to be back on the heap.

Stack
During method calls, objects are created for method arguments and method variables. These objects are created on
stack.
Q) System.out.println()
\ue000 println() is a methd of java.io.printWriter.
\ue000 \u201cout\u201d is an instance variable of java.lang.System class.
Q) Transient & volatile

Transient --> The transient modifier applies to variables only, the object are variable will not persist. Transient
variables are not serialized.
Volatile --> value will be changed unexpectedly by the other part of the program, "it tells the compiler a variable

may change asynchronously due to threads"
Q) Access Specifiers & Access modifiers?
Page 1

Access Specifiers\ue000 A.S gives access privileges to outside of application(or) others, they are Public, Protected,
Private, Defaults.
Access Modifiers\ue000 A.M which gives additional meaning to data, methods and classes, final cannot be modified at any
point of time.

Private Public Protected No modifier
Same class
No
Yes
Yes
Yes
Same package Subclass
No
Yes
Yes
Yes
Same package non-subclass
No
Yes
Yes
Yes
Different package subclass
No
Yes
Yes
No
Different package non-subclass
No
Yes
No
No
Q) Default Values
long
-2^63 to 2^63 \u20131\ue000 0L
double
0.0d
Int
-2^31 to 2^31 \u20131\ue000 0
float
0.0f
Short-2^15 to 2^15 \u20131\ue000 0
Boolean
false
Byte
-2^7 to 2^7 \u20131\ue000 0
char
0 to 2^7 \u20131\ue000 null character (or) \u2018\u0 000\u2019
Q) Byte code & JIT compiler & JVM & JRE & JDK
\ue000 Byte code is a highly optimized set of instructions. JVM is an interpreter for byte code. Translating a java program
into byte code helps makes it much easier to run a program in a wide variety of environment.
\ue000 JVM is an interpreter for byte code
\ue000 JIT (Just In Time) is a part of JVM, it compiles byte code into executable code in real time, will increase the
performance of the interpretations.
\ue000 JRE is an implementation of the Java Virtual Machine, which actually executes Java programs.
\ue000 JDK is bundle of software that you can use to develop Java based software, Tools provided by JDK is
(i) javac \u2013 compiler
(ii) java \u2013 interpretor
(iii) jdb \u2013 debugger
(iv) javap - Disassembles
(v) appletviewer \u2013 Applets
(vi) javadoc - documentation generator (vii) javah - 'C' header file generator
Q) Wrapper classes
Primitive data types can be converted into objects by using wrapper classes. These are java.lang.package.
Q) Does Java pass method arguments by value or by reference?
Java passes all arguments by value, not by reference
Q) Arguments & Parameters
While defining method, variable passed in the method are called parameters. While using those methods, values
passed to those variables are called arguments.
Q) Public static void main (String [] args)
\ue000 We can overLoad the main() method.
\ue000What if the main method is declared as \u201cPrivate\u201d?
The program compiles properly but at runtime it will give "Main method not public." Message
\ue000 What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".

\ue000 We can write \u201cstatic public void\u201d instead of \u201cpublic static void\u201d but not \u201cpublic void static\u201d. \ue000 Protected static void main(), static void main(), private static void main() are also valid. \ue000 If I do not provide the String array as the argument to the method?

Program compiles but throws a runtime error "NoSuchMethodError".
\ue000If no arguments on the command line, String array of Main method will be empty or null?
It is empty. But not null.
\ue000 Variables can have the same name as a method or a class
Q) Can an application have multiple classes having main() method?

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 not conflict amongst the multiple classes
having main method.

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

The automatic initialization is performed through the constructor, constructor has same name has class name. Constructor has no return type not even void. We can pass the parameters to the constructor. this() is used to invoke a constructor of the same class. Super() is used to invoke a super class constructor. Constructor is called immediately after the object is created before the new operator completes.

\ue000 Constructor can use the access modifiers public, protected, private or have no access modifier
\ue000 Constructor can not use the modifiers abstract, static, final, native, synchronized orstrict fp
\ue000 Constructor can be overloaded, we cannot override.
\ue000 You cannot usethis() andSuper() in the same constructor.

Class A(
A(){System.out.println(\u201chello\u201d);
}}
Class B extends A {
B(){System.out.println(\u201cfriend\u201d);
}}

Class print {
Public static void main (String args []){
B b = new B();
}o/p:- hello friend

Q) Diff Constructor & Method
Constructor
Method
Use to instance of a class
Grouping java statement
No return type
Void (or) valid return type
Same name as class name
As a name except the class method name, begin

with lower case.
\u201cThis\u201d refer to another constructor in the same
class

Refers to instance of class
\u201cSuper\u201d to invoke the super class constructor
Execute an overridden method in the super class
\u201cInheritance\u201d cannot be inherited
Can be inherited
We can \u201coverload\u201d but we cannot \u201coverridden\u201d

Can be inherited
Will automatically invoke when an object is
created

Method has called explicitly
Q) Garbage collection

G.C is also called automatic memory management as JVM automatically removes the unused variables/objects
(value is null) from the memory. User program cann't directly free the object from memory, instead it is the job of the
garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits

finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no

more references to the object exists. In Java, it is good idea to explicitly assignnull into a variable when no more in use, callingS ystem.gc() andRuntime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is a low-priority thread.

G.C is a low priority thread in java, G.C cannot be forced explicitly. JVM may do garbage collection if it is running short of memory. The call System.gc() does NOT force the garbage collection but only suggests that the JVM may make an effort to do garbage collection.

Q) How an object becomes eligible for Garbage Collection?
Page 3

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->