In Java , interface doesn't allow
you to declare any instance variables. Using a variable
declared in an interface as an
instance variable will return a
compile time error. You can declare a constant variable, using static
final which is different from an instance variable.
Java By Trilochan – Learn Core Java, Java 8+, Collections, OOP, Multithreading, Spring Boot with Micro Services and Java Interview Questions with practical examples, coding solutions and interview preparation tips.
Wednesday, 30 August 2017
Saturday, 19 August 2017
How Java Lambdas Expressions affect the code in simple way
Let us start with some
basic examples. Here, we will see how lambda expressions affect the code in a
simple way. Having a list of players, the “for loop”, as programmers often
refers to the for statement, can be translated in Java SE 8 as below:
String[] s1 =
{"Anant", "Nilan", "Dolagobinda",
"Babu", "Rupeli", "Sanjay", "Akshay",
"Santosh"};
List<String>
players = Arrays.asList(s1);
// Old looping
for (String player :
players) {
System.out.print(player + "; ");
}
// Using lambda
expression and functional operations
players.forEach((player)
-> System.out.print(player + "; "));
// Using double colon
operator in Java 8
players.forEach(System.out::println);
What is JAVA Lambda Expression???
Java Lambda expressions are a new and
important feature included in Java SE
8. A lambda expression provides a way to represent one method interface using
an expression. A lambda expression is like a method, it provides a list of
formal parameters and a body (which can be an expression or a block of code)
expressed in terms of those parameters.
Lambda expressions also improve the Collection libraries. Java SE
8 added two packages related to bulk data operations for Collections, the java.util.function package, and the java.util.stream. A stream is like an
iterator, but with a lot of extra functionality. Taken together, lambda
expressions and streams are the biggest change to Java programming since the
generics and annotations were added to the language.
Wednesday, 5 July 2017
How to sort ArrayList in JAVA
|
CollectionSort.java
package java8s;
import java.util.*;
public class CollectionSort {
public
static void main(String... args)
{
List
l = new ArrayList();
l.add("Bhubaneswar");
l.add("Cuttack");
l.add("Paradeep");
l.add("Jagatsinghpur");
l.add("Kujanga");
Collections.sort(l);
System.out.println("Location :
"+l);
}
}
|
Output
Location : [Bhubaneswar, Cuttack,
Jagatsinghpur, Kujanga, Paradeep]
How to avoid duplicate values from JAVA List / ArrayList
Hiii frnds, this is a nice question in interview forum, so let's see how to remove duplicate objects from List / ArrayList
ArrayListExample.java
ArrayListExample.java
package java8s;
import java.util.*;
public class
ArrayListExample {
public static void main(String[] args) {
ArrayList
al=new ArrayList();
al.add("first");
al.add("second");
al.add("third");
al.add("first");
al.add("first");
ArrayList
al1=new ArrayList(new LinkedHashSet(al));
Iterator
it=al1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Output
first
second
third
Thursday, 8 June 2017
GET vs. POST in Servlet
|
|
GET
|
POST
|
|
1.
|
It is used for getting information from
the server side
|
It is used to post the information to the
server.
|
|
2.
|
It performs read-only operation
|
It performs update/write operation.
|
|
3.
|
Whatever the request can be sent that is
append with URL
|
Whatever request send to the server that
is not exposed in the URL
|
|
4.
|
Only text or character type of data can
be sent
|
Any type of data can be send
|
|
5.
|
Limited amount of data can be send (up to
2KB)
|
No length limit of data that we can send
|
|
6.
|
It provides less security because the
user’s request is directly exposed in URL bar. Hence, sensitive information is not recommended to be send.
|
It provides high security
|
|
7.
|
Bookmarking is possible which is the
advantage
|
Bookmarking is not possible
|
|
8.
|
Caching is also possible which is also another
advantage of get method
|
Caching is not possible
|
Thursday, 11 May 2017
What is Composition in JAVA !!!
Composition is restricted form of agreegation. For example, a class car can't exist without engine.
class Car
{
private Engine engine;
Car(Engine en)
{
engine en;
}
}
class Car
{
private Engine engine;
Car(Engine en)
{
engine en;
}
}
Thursday, 4 May 2017
Why String[] args in main() method in JAVA?
The main objective is designed very
intelligently by developers. Actual thinking is very deep. Which is basically
developed under consideration of C & C++ based on Command line argument but
nowadays nobody uses it more.
Thing 1- User can enter any type of data from the command line can
be Number or String & necessary to accept it by the compiler which datatype
we should have to use?
Thing 2- String is the datatype which supports all of the primitive
datatypes like int, long, float, double, byte, shot, char in Java. You can
easily parse it in any primitive datatype.
let's see the following example,
If input is -> 1 1
// one class needs to have a main() method
public class HelloSilan
{
// arguments are passed using the text field
below this editor
public static void main(String[] x)
{
System.out.println(x[0] + x[1]); // Output is 11
//Comment out below code in case of String
System.out.println(Integer.parseInt(parameter[0]) + Integer.parseInt(parameter[1])); //Output is 2
System.out.println(Float.parseFloat(parameter[0]) + Float.parseFloat(parameter[1])); //Output is 2.0
System.out.println(Long.parseLong(parameter[0]) + Long.parseLong(parameter[1])); //Output is 2
System.out.println(Double.parseDouble(parameter[0]) + Double.parseDouble(parameter[1])); //Output is 2.0
}
}
What is checked exception?
The exception which is checked by the compiler at the time of
compilation for smooth execution of the program is known as checked exception.
Example:
import java.io.*;
class Test{
public static void main(String[] args){
PrintWriter pw=new PrintWriter("abc.txt");
pw.println("SILAN TECHNOLOGY");
}
}
sometimes the compiler check some specific program for smooth execution at run time, so when we compile this program we will get following compile time error :
Example:
import java.io.*;
class Test{
public static void main(String[] args){
PrintWriter pw=new PrintWriter("abc.txt");
pw.println("SILAN TECHNOLOGY");
}
}
sometimes the compiler check some specific program for smooth execution at run time, so when we compile this program we will get following compile time error :
unreported exception
java.lang.FileNotFoundException must be caught or declared to be thrown
Friday, 21 April 2017
JDBC-Store image in Oracle Database
It is
possible that we can store images in the database by executing jdbc program
with the help of PreparedStatement interface.
The setBinaryStream() method
of PreparedStatement is used to set Binary information into the parameterIndex.
For example:
CREATE TABLE user
(
id varchar2(100),
name varchar2(100),
image BLOB
);
Let's
write the program to store the image in the database. Here we are using e:\\a.jpg
for the location of image. You can change it according to the image location.
JDBC Example to store image in the
database
import java.sql.*;
import java.io.*;
public class InsertImageDemo {
public static void main(String[] args) throws
Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement("insert into user values(?,?,?)");
ps.setString(1,101);
ps.setString(2,"Tapuuu");
FileInputStream fin=new FileInputStream("e:\\a.jpg");
ps.setBinaryStream(2,fin,fin.available());
int k=ps.executeUpdate();
System.out.println(k+" row inserted");
con.close();
}
}
Output
1 row inserted
Note : If you see the table, record is stored in the database but image
will not be shown. For this, you need to retrieve the image from the database.
Subscribe to:
Posts (Atom)