Escolar Documentos
Profissional Documentos
Cultura Documentos
Ctrl
Shift
In brief, == tests if references are equal and equals() tests if values are equal.
Unless you want to check if two strings are the same object, you should always
use equals().
is that finally block will execute even if you put areturn statement in the try
block or catch block but finally block won't run if you callSystem.exit form try or
catch.
Anagram : a word, phrase, or name formed by rearranging the letters of another,
such as spar, formed from rasp.
String vs StringBuilder: StringBuilder is mutable, which means you can modify it
after its creation.
StringBuilder vs StringBuffer: StringBuffer is synchronized, which means it is
thread-safe but slower than StringBuilder.
This will create a new char array that represents the new string. The
above approach sometimes can make your code faster, because
Garbage Collector can collect the unused large string and keep only
the sub string.
you can not override a private or static method in Java, if you create a similar method with
same return type and same method arguments in child class then it will hide the super class
method, this is known as method hiding. Similarly, you cannot override a private method in
sub class because it's not accessible there, what you do is create another private method
with the same name in the child class.
Nested classes are divided into two categories: static and non-static. Nested
classes that are declared static are called static nested classes. Non-static
nested classes are called inner classes.
A binary search tree (BST) is a node based binary tree data structure which has
the following properties.
The left subtree of a node contains only nodes with keys less than the nodes
key.
The right subtree of a node contains only nodes with keys greater than the
nodes key.
Both the left and right subtrees must also be binary search trees.
Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined as the
lowest node in T that has both n1 and n2 as descendants (where we allow a node to be a descendant
of itself).
Customer name
Customer ID
Address
Date of birth
A certain set of columns may be extracted and guaranteed unique to each customer. Examples
of superkeys are as follows:
Name+SSN+Birthdate
ID+Name+SSN
However, this process may be further reduced. It can be assumed that each customer ID is
unique to each customer. So, the superkey may be reduced to just one field, customer ID, which
is the candidate key. However, to ensure absolute uniqueness, a composite candidate key may
be formed by combining customer ID with SSN.
A primary key is a special term for candidate keys designated as unique identifiers for all table
rows. Until this point, only columns have been considered for suitability and are thus termed
candidate keys. Once a candidate key is decided, it may be defined as the primary key at the
point of table creation.
Description
malloc
realloc
calloc
free
Differences between
malloc()
malloc()
and
calloc()
calloc()
needs two
arguments (the number of variables to allocate in memory, and the size in bytes of a single variable).
malloc()
calloc()
Abstract Class
A class which contains the abstract keyword in its declaration is known
as abstract class.
Abstract classes may or may not contain abstract methods ie., methods with
out body ( public void get(); )
But, if a class have at least one abstract method, then the class must be
declared abstract.
To use an abstract class you have to inherit it from another class, provide
implementations to the abstract methods in it.
If you inherit an abstract class you have to provide implementations to all the
abstract methods in it.
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.
Constructors are called recursively up the class hierarchy. Every constructor must call its
super-class constructor as the first thing. (The call can be omitted if it is to the no-arg
constructor, in which case the compiler will automatically insert it.)
As an aside, Java doesn't have virtual methods
By default you can put any Object into a Queue, but from Java 5, Java
Generics makes it possible to limit the types of object you can insert into
a Queue. Here is an example:
Queue<MyObject> queue = new LinkedList<MyObject>();
this keyword can be used inside the constructor to call another overloaded
constructor in the same Class. This is called the Explicit Constructor
Invocation
Yes, You can have many main() methods in a class by overloading the main method.
Based on 2014 revenue, the NoSQL market leaders are MarkLogic, MongoDB, and
Datastax.[19] Based on 2015 popularity rankings, the most popular NoSQL
databases are MongoDB, Apache Cassandra, and Redis
The statement String str = new String("test"); creates a string object which gets stored
on the heap like any other object. The string literal "test" that is passed as an argument
is stored in the string constant pool.
1)Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known
as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are
checked at compile-time.
2)Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at
compile-time.
Both poll() and remove() take out the object from the Queue but if poll() fails then it
returns null but if remove fails it throws Exception.
Hashtable doesn't allow null keys but HashMap allows one null key.
As of Java 7 now, default size of ArrayList is 10 and default capacity of HashMap is 16, it
must be power of 2. Here is code snippet from ArrayList and HashMap class :
Is it possible for two unequal objects to have the same hashcode?
Yes, two unequal objects can have same hashcode that's why collision happen in a
hashmap.
the equal hashcode contract only says that two equal objects must have the same
hashcode it doesn't say anything about the unequal object.
Shortcut for Hierarchy : ctrl + T
You can use PowerMock library to test static methods in Java.
The eight primitive types are byte, char, short, int, long, float, double, and
boolean.
1)Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known
as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are
checked at compile-time.
2)Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException etc. Unchecked exceptions are not checked at
compile-time.
A class loader is an object that is responsible for loading classes. The class
ClassLoader is an abstract class.
String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object.
1.
What is gc()?
gc() is a daemon thread.gc() method is defined in System class that is used to send
request to JVM to perform garbage collection.
Reader/Writer
class
hierarchy
is
character-oriented,
and
the
What is reflection?
Reflection is the process of examining or modifying the runtime behaviour of a class at
runtime.
New objects are simply allocated at the end of the used heap.
HashMap has no or relatively less number of collision or binary tree is balanced. If that's
not the case then their performance degrades as a number of records grows.
What is heap and stack in a process?
They are two separate areas of memory in the same process. Talking about Java, the stack
is used to store primitive values and reference type to object but actual object is always
created in heap. One critical difference between heap and stack is that heap memory is
shared by all threads but each thread has their own stack.
In a strongly typed language compiler ensure type correctness, for example, you can not
store the number in String or vice-versa. Java is a strongly typed language, that's why you
have different data types e.g. int, float, String, char, boolean etc. You can only
store compatible values in respective types. On the other hand, weakly typed language
don't enforce type checking at compile time and they tree values based upon context.
Python and Perl are two popular example of weakly typed programming language, where
you can store a numeric string in number type.
A well-formed XML is the one which has root element and all tags are closed properly,
attributes are defined properly, their value is also quoted properly. On another hand, a
valid XML is the one which can be validated against an XSD file or schema. So it's possible
for an XML to be well-formed but not valid because they contain tags which may not be
allowed by their schema.
DOM parser is an in-memory parser so it loads whole XML file in memory and create a DOM
tree to parse. SAX parser is an event based parser, so it parses XML document based on the
event received e.g. opening tag, closing tag, the start of attribute or end of the attribute.
Because of their working methodology, DOM parser is not suitable for large XML file as they
will take a lot of space in memory and your process may run out of memory, SAX is the one
which should be used to parse large files. For small files, DOM is usually much faster than
SAX.
An immutable object is very useful for concurrent programming because they can be
shared between multiple threads without worrying about synchronization. In fact, the
whole model of functional programming is built on top of Immutable objects.
In Java, you can avoid SQL injection by using Prepared statement.
In short result of LEFT outer join is INNER JOIN + unmatched rows from LEFT table
and RIGHT OUTER join is INNER JOIN + unmatched rows from the right-hand side table.
A class is a blueprint on which objects are created. A class has code and behavior but an
object has both the state and behavior. You cannot create an object without creating a
class to represent its structure. The class is also used to map an object in memory, in
Java, JVM does that for you.
Association means two objects are related to each other but can exist without each other,
Composition is a form of association where one object is composed of multiple objects,
but they only exists together e.g. human body is the composition of organs, individual
organs cannot live they only useful in the body. Aggregation is a collection of object e.g.
city is an aggregation of citizens.
One drawback of recursion is depth since recursion stores intermediate result in the stack
you can only go up to a certain depth, after that your program will die
with StackOverFlowError, this is why iteration is preferred over recursion in
production code.
One difference between & and && is that bitwise operator (&) can be applied to both
integer and boolean but logical operator (&&) can only be applied to boolean variables.
Initial
S
SRP
OCP
LSP
ISP
DIP
In order to implement interface in Java, until your class is abstract, you need to provide
implementation of all methods, which is very painful.
A stateless system is a system which doesn't maintain any internal state. Such system will
produce the same output for same input at any point of time. It's always easier to code
and optimize a stateless system, so you should always strive for one if possible.
In correlated sub-query, inner query depends upon the outer query and executes for each
row in the outer query. While non-correlated subquery doesn't depend upon the outer
query and can be executed independently. Due to this reason former is slow and later is
fast. BTW, correlated subquery has some nice application, one of them is finding Nth
highest salary in Employee table, as seen on previous SQL question as well.
Main difference is that Iterator was introduced in place of Enumeration. It also allows you to
remove elements from collection while traversing which was not possible with Enumeration. The
methods of Iterator e.g. hasNext() and next() are also more concise then corresponding
methods in Enumeration e.g. hasMoreElements(). You should always use Iterator in your
Java code as Enumeration may get deprecated and removed in future Java release
This question is tricky because unlike the Integer, where MIN_VALUE is negative, both
the MAX_VALUE and MIN_VALUE of the Double class are positive numbers.
C and C++
C and C++ do not provide direct support for serialization.
Java
Java provides automatic serialization which requires that the object be marked by
implementing the java.io.Serializable interface. Implementing the interface
marks the class as "okay to serialize", and Java then handles serialization
internally. There are no serialization methods defined on the Serializable
interface, but a serializable class can optionally define methods with certain
special names and signatures that if defined, will be called as part of the
serialization/deserialization process. The language also allows the developer to
override the serialization process more thoroughly by implementing another
interface, the Externalizable interface, which includes two special methods that
are used to save and restore the object's state.
There are three primary reasons why objects are not serializable by default and
must implement the Serializable interface to access Java's serialization
mechanism.
1. Not all objects capture useful semantics in a serialized state. For
example, a Thread object is tied to the state of the current JVM.
There is no context in which a deserialized Thread object would
maintain useful semantics.
2. The serialized state of an object forms part of its classes'
compatibility contract. Maintaining compatibility between versions
of serializable classes requires additional effort and consideration.
Therefore, making a class serializable needs to be a deliberate
design decision and not a default condition.
3. Serialization allows access to non-transient private members of a
class that are not otherwise accessible. Classes containing sensitive
information (for example, a password) should not be serializable nor
externalizable.
The standard encoding method uses a simple translation of
the fields into a byte stream. Primitives as well as non-transient,
non-static referenced objects are encoded into the stream. Each
object that is referenced by the serialized object and not marked as
transient must also be serialized; and if any object in the complete
graph of non-transient object references is not serializable, then
serialization will fail. The developer can influence this behaviour by
marking objects as transient, or by redefining the serialization for
an object so that some portion of the reference graph is truncated
and not serialized.
It is possible to serialize Java objects through JDBC and store them
into a database. While Swing components do implement the
Serializable interface, they are not portable between different
versions of the Java Virtual Machine. As such, a Swing component,
The marker interface pattern is a design pattern in computer science, used with
languages that provide run-time type information about objects. It provides a
means to associate metadata with a class where the language does not have
explicit support for such metadata.
Serialization is simply turning an existing object into a byte array. This byte array
represents the class of the object, the version of the object, and the internal state of the
object. This byte array can then be used between JVM's running the same code to
transmit/read the object.
Serialization is simply turning an existing object into a byte array. This byte array
represents the class of the object, the version of the object, and the internal state of the
object. This byte array can then be used between JVM's running the same code to
transmit/read the object.
Why would we want to do this?
There are several reasons:
Communication: If you have two machines that are running the same code, and
they need to communicate, an easy way is for one machine to build an object with
information that it would like to transmit, and then serialize that object to the other
machine. It's not the best method for communication, but it gets the job done.
Deep Copy: If you need an exact replica of an Object, and don't want to go to the
trouble of writing your own specialized clone() class, simply serializing the object to
a byte array, and then de-serializing it to another object achieves this goal.
Caching: Really just an application of the above, but sometimes an object takes
10 minutes to build, but would only take 10 seconds to de-serialize. So, rather than
hold onto the giant object in memory, just cache it out to a file via serialization, and
read it in later when it's needed.
Cross JVM Synchronization: Serialization works across different JVMs that may
be running on different architectures.
HTML Tag
JavaScript
Web
page
<BODY> . . . </BODY>
document
Image
<IMG NAME="myImage">
document.myImage
HTML
form
<FORM name="myForm"> . . .
</FORM>
document.myForm
Button
<INPUT TYPE="button"
NAME="myButton">
document.myForm.myButton