Static import in Java allows to import static members of class and use
them, as they are declared in the same class. Static import is introduced in
Java 5 along with other features like Generics,
Enum,
Autoboxing
and Unboxing and variable
argument methods. Many programmer think that using static import can reduce
code size and allow you to freely use static field of external class
without prefixing class name on that. For example without static import you will
access static constant MAX_VALUE of Integer class as Integer.MAX_VALUE but by
using static import you can import Integer.MAX_VALUE and refer
it as MAX_VALUE. Similar to regular import
statements, static import also allows wildcard * to import all static
members of a class. In next section we will see Java program to demonstrate
How to use static import statements to import static fields.
Monday, October 29, 2012
Saturday, October 27, 2012
SQL Query to find all table names on database in MySQL and SQL Server Examples
How do you find names of all tables in a database is a recent SQL interview questions asked to one of my
friend. There are many ways to find all table names form any database like
MySQL and SQL Server. You can get table names either from INFORMATION_SCHEMA or sys.tables based upon
whether you are using MySQL or Sql Server database. This is not a popular
question like when
to use truncate and delete or correlated
vs noncorrelated subquery which you can expect almost all candidate prepare
well but this is quite common if you are working on any database e.g. MySQL. In
this SQL tutorial we will see examples of getting names of all tables from MySQL
and SQL Server database. In MySQL there are two ways to find names of all
tables, either by using "show" keyword or
by query INFORMATION_SCHEMA. In case of SQL Server or MSSQL, You can either
use sys.tables or INFORMATION_SCHEMA to get all
table names for a database. By the way if you are new in MySQL server and
exploring it , you may find this list of frequently
used MySQL server commands handy.
Labels:
database,
mysql,
programming,
SQL
What is difference between java.sql.Time, java.sql.Timestamp and java.sql.Date - JDBC interview Question
Difference between java.sql.Time, java.sql.Timestamp and java.sql.Date is most common JDBC question appearing on
many core Java interviews. As JDBC
provides three classes java.sql.Date, java.sql.Time and java.sql.Timestamp to
represent date and time and you already have java.util.Date which can
represent both date and time, this question poses lot of confusion among Java
programmer and that’s why this is one of those tricky
Java questions which is tough to answer. It becomes really tough if
differences between them is not understood correctly. We have already seen some
frequently asked or common JDBC questions like why
JDBC has java.sql.Date despite java.util.Date and Why
use PreparedStatement in Java in our last tutorials and we will see
difference between java.sql.Date, java.sql.Time and java.sql.Timestamp in this
article. By the way apart from these JDBC interview questions, if you are
looking to get most from JDBC you can also see 4
JDBC performance tips and 10
JDBC best practices to follow. Those article not only help you to
understand and use JDBC better but also help on interviews. Let’s come back to
difference sql time, timestamp and sql date.
Labels:
core java,
core java interview question,
JDBC,
programming,
SQL
Friday, October 26, 2012
JSTL foreach tag example in JSP - looping ArrayList
JSTL foreach loop in
JSP
JSTL foreach tag is pretty useful
while writing Java free JSP code. JSTL
foreach tag allows you to iterate or loop
Array List, HashSet
or any other collection without using Java code. After introduction of JSTL and
expression language(EL) it is possible to write dynamic JSP code without using
scriptlet which clutters jsp pages. JSTL
foreach tag is a replacement of for loop and behaves similarly like foreach
loop of Java 5 but still has some elements and attribute which makes it hard
for first-timers to grasp it. JSTL foreach loop can iterate over arrays,
collections like List,
Set
and print values just like for loop. In this JSP tutorial we will see couple of
example of foreach loop which makes it easy for new guys to understand and use
foreach loop in JSP. By the way this is our second JSP tutorial on JSTL core
library, in last tutorial we have seen How
to use core <c:set> tag in JSP page.
Labels:
coding,
interview questions,
JSP,
JSTL,
programming
Thursday, October 25, 2012
Eclipse shortcut to System.out.println in Java program - Tips
Eclipse IDE provides quick shortcut keys to print System.out.println
statement in Java but unfortunately not every Java programmers are
familiar of that. Even programmers with 3 to 4 year of experience sometime doesn't know this useful Eclipse shortcut to generate System.out.println messages. This Eclipse tips is to help those guys. Let me ask you one
question, How many times you type System.out.println in your
Java program? I guess multiple time, while doing programming, debugging
and testing small stuff. System.out.println
is most preferred way to print something on console because it doesn’t
required setup like configuring Log4J or java.util.Logger . Though I
recommend using logging for better information display in production
code, nobody can undermine importance of System.out.println statement
in Java. On the quest of learning Eclipse shortcut, some of them which I have
discussed in my list of Top
30 Eclipse shortcut for Java programmers, I found a very useful Eclipse shortcut for generating
code for System.out.println statement in Java source
file. By using this Eclipse shortcut you can create System.out.println() messages in
60% less time, as you only need to type
the message. This Eclipse shortcut will place your cursor right in the place,
where it should be i.e. inside System.out.println method.
Labels:
coding,
core java,
Eclipse,
programming
Java program to calculate area of Triangle - Homework, programming exercise example
Calculate area of Triangle in Java
Write a Java program to calculate
Area of Triangle, accept input from User and display output in Console is
one of the frequently asked homework question. This is not a tough
programming exercise but a good exercise for beginners and anyone who has
started working in Java. For calculating area of triangle using formula 1/2(base*height), you need
to use arithmetic operator. By doing this kind of exercise you will understand
how arithmetic operators works in Java, subtle details which affect calculation
like precedence and associativity of operator etc. Fortunately this question
is not very popular on Java
interview like other exercise we have seen e.g. Java
program to print Fibonacci series and Java
program to check for palindrome. Nevertheless its a good Java homework.
Labels:
coding,
core java,
homework,
programming
Tuesday, October 23, 2012
Difference between notify and notifyAll in Java - When and How to use
notify vs notifyAll in Java
What is difference between notify and notifyAll method is one of the tricky Java question, which is
easy to answer but once Interviewer ask followup questions, you either got
confused or not able to provide clear cut and to the point answers. Main
difference between notify and notifyAll is that notify method
will only notify one Thread and notifyAll method
will notify all Threads which are
waiting on that monitor or lock. By the way this is something you have been
reading in all over places and to be frank,
this statement despite being correct is not complete and its very
difficult to understand difference
between notify vs notifyAll by just reading this statement. Lot of questions
comes in mind like
Which thread will be notified if I use notify()?
How do I know how many threads are waiting, so that I can use notifyAll() ?
How to call notify()?
What are these thread waiting for being notified etc.
Actually discussion of notify and notifyAll is
incomplete without discussing wait method in Java and I had touched based on
this on my earlier article why wait and notify must be called from
synchronized context. In order to get answer of those questions and
understand difference between notify and notifyAll we will use a simple Java
Thread example using wait and notify code :
Saturday, October 20, 2012
5 ways to add multiple JAR in to Classpath in Java
How to add JAR file to Classpath in Java
Adding JAR into classpath is a common task for Java programmer and
different programmer do it on different way. Since Java allows multiple ways to
include JAR file in classpath, it becomes important to know pros and cons of
each approach and How exactly they work. There are 5 ways to add jars on into classpath in Java some of them we
have already seen in How
classpath works in Java and How
to set Path in Java. In this post we will revisit some of those techniques and
explore Java 1.6 wildcard to add multiple JAR into classpath. By the way here
is 5 ways to add JAR
file into classpath. In short knowledge of Path, Classpath and Classloaders
are must for any Java programmer, not only from knowledge point of view but
also to successfully debug and resolve issues related to Classpath e.g. NoClassDefFoundError
and java.lang.ClassNotFoundException
in Java.
Labels:
coding,
core java,
programming
Friday, October 19, 2012
Regular Expression in Java to check numbers in String - Example
Regular Expression
to check numeric String
In order to build a regular expression to check if String is number or
not or if String contains any non digit
character or not you need to learn about character set in Java regular
expression, Which we are going to see in this Java regular expression example.
No doubt Regular Expression is great
tool in developer arsenal and familiarity or some expertise with regular
expression can help you a lot. Java supports regular expression using java.util.regex.Pattern and java.util.regex.Matchter class, you
can see a dedicated package java.util.regex for
regular expression in Java. Java supports regex from JDK 1.4, means well before
Generics, Enum or Autoboxing. If you are writing server side code in Java
programming language than you may be familiar with importance of regular
expression which is key in parsing certain kind of messages e.g. FIX Messages used in
Electronic trading. In order to parse Repeating groups in FIX protocol
you really needs understanding of regular expression in Java. Any way In order
to learn validating numbers using regular expression we will start with simple
example
Labels:
coding,
core java,
core java interview question,
programming
Difference between private, protected, public and package modifier in Java
private vs public vs protected vs package in Java
Java has four access modifier namely private, protected and public. package
level access is default access level provided by Java if no access modifier is specified. These
access modifier is used to restrict accessibility of a class,
method or variable
on which it applies. We will start from private
access modifier which is most restrictive access modifier and then go towards
public which is least restrictive access modifier, along the way we will see
some best
practices while using access modifier in Java and some examples of using private and protected keywords.
Labels:
coding,
core java,
core java interview question,
programming
Tuesday, October 16, 2012
10 Garbage Collection Interview Questions and Answers
GC Interview Questions Answer in Java
Garbage collection
interview questions are very popular in both core Java and advanced Java
Interviews. Apart from Java Collection
and Thread many tricky Java questions stems Garbage
collections which are tough to answer. In this Java Interview article I will
share some questions from GC which is asked in various core Java interviews.These
questions are based upon concept of How Garbage collection works,
Different kinds of Garbage collector and JVM parameters used for
garbage collection monitoring and tuning. As I said GC is an important part of
any Java interview so make sure you have good command in GC. One more thing
which is getting very important is ability to comprehend and understand Garbage collection Output,
more and more interviewer are checking whether candidate can understand GC
output or not. During Java interview they may
provide a snippet of GC output and ask various questions based on that e.g.
Which Garbage collector is used, whether output is from major collection or
minor collection, How much memory is free from GC, What is size of new
generation and old generation after GC etc. I have included few Garbage
collection interview questions and answers from GC output to help with that. It’s recommended to prepare questions from Java collection, multithreading and programming along with Garbage
collection to do well in Java interviews at any stage.
Labels:
core java,
core java interview question
Saturday, October 13, 2012
Java Program to get input from User from Console or command line- Example Tutorial Code
How to get input from user in Java from command line or console is one of the common thing
,every one started learning Java looks for. It’s second most popular way of
starting programming after HelloWorld
in Java. There are so many ways to get input from User in Java including
command line and Graphical user interface. For beginner simpler the example,
better it is. first and foremost way of getting input from user is String[]
passed to main
method in Java but that only work for one time requirement and its not
interactive. Another way of getting input from User is involving IO classes
like InputStream
to read from console or command line which can be little complicated for true
beginner. Thanks to Java 5 which added a nice utility class called Scanner, has made
task of getting input from user very easy. Scanner is
powerful and allows you to get any kind of input from User e.g. String,
int,
float etc.
Labels:
coding,
core java,
homework,
programming
Friday, October 12, 2012
Eclipse shortcut to remove all unused imports in Java file
How to
remove all unused imports in Eclipse
Eclipse IDE gives warning "The import XXX is never used"
whenever it detect unused import in
Java source file and shows a yellow underline. Though unused import in Java
file does not create any harm, its unnecessary increase length and size of
Java source file and if you have too many unused imports in your Java source
file, those yellow underline and Eclipse warning affect readability and working.
In our last post on Eclipse, we have seen dome Java
debugging tips on Eclipse and in this post we will see Eclipse shortcut to
remove all unused imports in Eclipse. There are many option to tackle with this
problem e.g. you can collapse import section of code in Eclipse or you can
altogether remove all unused imports from Java file. Since Eclipse is favorite
IDE of many Java programmer, who love to do things fast in shortcuts, I will
also shared Eclipse shortcut to remove unused imports from Java program in this
article. This is one of many Eclipse shortcuts which we have discussed in our
post Top
30 Eclipse keyboard shortcut for Java programmer. Let's see How to remove all unused imports from Java
file in Eclipse IDE.
Labels:
coding,
core java,
Eclipse,
programming
Sunday, October 7, 2012
10 Java String interview Question answers - Advanced
10
Java String interview Question answers
String interview questions in Java is one of Integral part of any Core Java or J2EE interviews. No one can
deny importance of String and How much it in any Java
application irrespective of whether its core Java desktop application, web
application, Enterprise application or Mobile application. String is one of the
fundamental of Java programming language and correct understanding of String class is
must for any Java programmer. What makes String
interview questions in Java even more interesting is the special status of
String in terms of features and privileges it has, like + operator is kind of
overloaded to perform String concatenation despite the fact that Java does not support operator
overloading. There is a separate String pool to store String
literal etc. In this article we will some frequently asked question on String
in a Java interview which focuses on range of issues like immutability, thread-safety, Security etc.
Labels:
core java,
core java interview question
Saturday, October 6, 2012
What is Inheritance in Java and OOPS Tutorial - Example
Inheritance in Java is an Object oriented or OOPS
concepts, which allows to emulate real world Inheritance behavior, Inheritance
allows code reuse in Object oriented programming language e.g. Java. Along with
Abstraction,
Polymorphism
and Encapsulation,
Inheritance forms basis of Object oriented programming. Inheritance is
implemented using extends keyword in Java and When one
Class extends another Class it inherit all non
private members including fields and methods. Inheritance in Java can be
best understand in terms of Parent and Child class,
also known as Super class and Sub class in Java programming language. The class
which extends another class becomes Child of the class it extends and
inherit all its functionality which is not private or package-private given
where Child class is created. Inheritance is the
most easy way to reuse already written and tested code but not always best way
to do so, which we will see in this article. There are lot of example of
Inheritance in Object oriented world e.g. Child inherit properties from parent,
Dog inherit properties of Animal etc. In this Java and OOPS
tutorial we will learn What is Inheritance in Java, How to use
Inheritance in Java, Simple Example of
Java Inheritance and some important points about Inheritance in Java.
Labels:
core java,
object oriented programming,
programming
Subscribe to:
Posts (Atom)