Skip to main content

Posts

Showing posts with the label Java

Deep Cloning In Java

I have already mentioned about cloning in my previous post and also the types of cloning in java i.e. 1. Shallow cloning and 2. Deep cloning We have already seen what is shallow cloning, how to use it and the example to demonstrate it in my previous post you can read about it here . In this post we will discuss about deep cloning in java. Deep cloning basically refers to creating an exact copy of an object that means if we have a object class Person() {  String name;  Address address; //rest of the code } class Address () {  String state;  String city; //rest of the code } Now if I create a deep clone of the object "Person" then it will also create a new object of the "Address" and not a reference. Just to verify you can try making changes to the new "Address" object and then see if you still having the same value in the main object. That will be different if not then something is wrong. For creating a Deep clone you can use ei...

Shallow cloning in Java

Cloning basically refers to creating an identical copy of an existing object.  The term  clone  is derived from  κλών , the Greek word for "twig, branch", referring to the process whereby a new plant can be created from a twig. The word 'Cloning' in java refers to the same meaning. When we say we have created a clone object in java we mean we have created a new object with identical values in the object. There are two type of cloning in java as below : 1. Shallow cloning 2. Deep cloning Since this article is about shallow cloning will give description about that only and will include about the deep cloning in my next post. Shallow cloning : So shallow cloning is nothing but creating a clone of the object but not in detail. Now what does that mean? It means that if you have a object with referenced object in your main object then shallow cloning will not clone them, it will just clone the attribute of the main object.  Let's take an example : ...

Ternary Operator in Java and Javascript

Ternary Operators are basically replacement for if-else condition. Below is the example for java: if(condition == true) {     System.out.println("Condition is true"); } else {    System.out.println("Condition is false"); } The above example can be replaced by a ternary operator as : (condition == true)?System.out.println("Condition is true"):System.out.println("Condition is false"); The syntax remains almost same for javascript : if(condition == true) {     alert("Condition is true"); } else {    alert("Condition is false"); } (condition == true)?alert("Condition is true"):alert("Condition is false"); Ternary Operators reduces the readability of the code little bit but they give a better performance. If you are using if-else condition and there are millions of records to be processed then go for ternary operator.

If condition in jstl tags

For using JSTL ( J SP S tandard T ag L ibrary) you must include the below code in your jsp page : <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> After including this you can use the JSTL tags in your jsp page with prefix as 'c'. Using if condition in the jsp : <c:if test="${variableName=='test'}">      <label>The condition is true</label> </c:if> this will show an label on the the jsp page as "The condition is true" if it satisfies the mentioned condition.

Difference between getAttribute and getParameter

getParameter() -------------------------------------------- This is used for sending a parameter from client side to server side. Example : On client side  :    http://test.com/servlet?parameter=hi On server side :   request.getParameter('parameter'); The value returned by the request.getParameter is always "String". So, even if you are sending a numeric value in the parameter, on server side it will be treated as string only. So if you want a different data type in that case you have to typecast it. For example if you want a Long value from the parameter which is set as :  http://test.com/servlet?parameter=1 then (Long) request.getParameter('parameter'); this will return a Long value but do check for null value else it will throw an NullPointerException. getAttribute() -------------------------------------------- This is commonly used on server side. You can setAttribute value like this:  setAttribute('nameOfAttribute',valueOfAttr...

Difference Between LinkedList and Arraylist

Array list and linked list both implements list interface. ArrayList is more popular amongst programmer.The main difference between Arraylist and Linked list is arraylist implemented using re sizable array while linkedlist is implemented using doubly linked list. ArrayList LinkedList Arraylist is an index based data structure.Array provides O(1) performance for get(index) method but remove is costly in ArrayList as you need to rearrange all elements. LinkedList doesn't provide Random or index based access and you need to iterate over linked list to retrieve any element which is of order O(n). In case of arraylist for updating the list anywhere other than end of the list need re-indexing of the list. Insertion are easy and fast in linked list because there is no risk of resizing the data and copying the data into new array. ArrayList only have the data object Linked list has both data and address to the next link.Hence linked list has more ...

Java Uses both compiler and interpreter

Java is both compiled and interpreted language.First Java source code has to be translated into Byte code, which is done with the help of a compiler.But these byte codes are not machine instructions. Therefore ,in second stage this byte code has to be translated into machine code.This task is performed by an Interpreter.Hence, Java use both compiler and interpreter. Read more: http://wiki.answers.com/Q/Why_does_java_use_both_compiler_and_interpreter#ixzz22azx8Bfs

Java Hello World program

Before starting working with java I am assuming that you have jdk installed on your local machine. You can start writing java program using a notepad. Below are the steps for writing hello world program in java : 1) Create a file and rename it to HelloWorld.java 2) Open the above file and put the blow code in it 1| public class HelloWorld 2| { 3|   public static void main(String args[]) 4|   { 5|       System.out.println("Hello World"); 6|   } 7| } 3) Save the file. 4) Open a command prompt go to the location where u have saved this file and run the following command : C:>javac HelloWorld.java 5) This should run successfully with no errors. 6) Now run it using below command : C:>java HelloWorld 7) This should run successfully and should show you "Hello World" message on command prompt. 8) Congrats you are done with your first java program. Description : Line No.1 It's the declaration of a class. pub...