tag:blogger.com,1999:blog-79550032091262720362024-02-24T14:13:20.784+05:30Geek Explains: Java, J2EE, Oracle, Puzzles, and Problem Solving!Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.comBlogger296125tag:blogger.com,1999:blog-7955003209126272036.post-68115075018108640032009-11-28T21:32:00.002+05:302009-11-28T21:40:47.052+05:30Why String has been made immutable in Java?Why String has been made immutable in Java?Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why.Suppose you Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com17tag:blogger.com,1999:blog-7955003209126272036.post-70888065904046179942009-11-17T19:37:00.004+05:302009-11-17T19:49:43.890+05:30Memory Leak in Java? Does 'static' cause it?Does 'static' cause Memory Leak in Java?What's memory leak? In simple terms, it's unused but referenced (somehow because the programmer probably unintentionally forgot to remove the references once the use of the object was over) part of the memory. Before we start discussing if 'static' can cause memory leak in Java, let me assure you that whatever you've read about Garbage Collectors in Java, Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com9tag:blogger.com,1999:blog-7955003209126272036.post-75349736896850915552009-10-29T19:33:00.005+05:302009-10-31T10:06:18.168+05:30Marker Interface in Java: what, why, uses, etc.What are Marker Interfaces in Java?An empty interface having no methods or fields/constants is called a marker interface or a tag interface. This of course means if the interface is extending other interfaces (directly or indirectly) then the super interfaces must not have any inheritable member (method or field/constant) as otherwise the definition of the marker interface (an entirely empty Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com12tag:blogger.com,1999:blog-7955003209126272036.post-85070447651695259332009-10-13T16:40:00.004+05:302009-10-13T17:11:30.974+05:30Connecting to an HTTP Web Service from VBA via ProxyConnecting to an HTTP Web Service from VBA Excel via a Proxy ServerThough MSDN suggests using stubs generated from the WSDL by MS Soap Toolkit for connecting to an HTTP Web Service from within VBA Excel, but it might not work as you would like it to, especially for a SOA-compliant web service and particularly in the cases where you need to access the service via a Proxy Server.I have used the Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com0tag:blogger.com,1999:blog-7955003209126272036.post-23239704501678774372009-10-11T10:16:00.003+05:302009-10-11T10:31:57.875+05:30Why wait(),notify() and notifyAll() in the Object class?Why wait(), notify() and notifyAll() methods have been defined in the Object class?Java concurrency model uses locks to implement mutually exclusive access to objects in a multi-threaded environment and locks are associated with every object in Java (of type 'Object'), not only with Threads.wait, notify/notifyAll methods are used by threads to communicate with each other while trying to access a Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com10tag:blogger.com,1999:blog-7955003209126272036.post-29802975099699146802009-08-03T21:47:00.003+05:302009-08-03T22:03:48.064+05:30per-thread Singleton and per-thread Logging in JavaUsage of ThreadLocal: per-thread Singleton and per-thread LoggingShould you require a refresh of what ThreadLocals in Java are and how they work, refer to this article first. You can then proceed with the current article for understanding two of the most common uses of ThreadLocals in Java.per-thread Singleton impl using ThreadLocalSuppose you have a need of having a JDBC Connection objects per Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com1tag:blogger.com,1999:blog-7955003209126272036.post-10145985319674502842009-06-18T20:41:00.005+05:302009-06-18T22:06:07.136+05:30Choosing the Most Specific Method - Tricky OverloadingChoosing the Most Specific Method - Tricky Method OverloadingLet's start with looking at a code-segment and try to think of the output/error, it would produce when compiled/executed and subsequently we'll discuss the behavior of code.public class NullTest { public static void method(Object obj){ System.out.println("method with param type - Object"); } public static void method(String Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com8tag:blogger.com,1999:blog-7955003209126272036.post-48057981142898322342009-06-11T22:43:00.007+05:302009-06-11T23:54:16.090+05:30Initializer Blocks & their alternatives in JavaInitializer Blocks in Java and their possible alternativesInitializer Blocks - what are they, why & how are they used?These blocks are similar to the static initialization blocks with the only difference being the absence of the 'static' keyword. The Java compiler copies all the initializer blocks in the same order as they are in the source code in every constructor before any executable Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com2tag:blogger.com,1999:blog-7955003209126272036.post-52924488417526538872009-06-07T00:27:00.005+05:302009-06-07T00:56:16.270+05:30Static Initialization Blocks & their alternativesStatic Initialization Blocks and their alternatives in JavaWhy do we need Static Initialization Blocks?The easiest way of initializing fields (static or instance) in Java at the time of their declaration is simply by providing a compile time constant value of a compatible data type. For example:public class InitializationWithConstants{public static int staticIntField = 100;private boolean Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com0tag:blogger.com,1999:blog-7955003209126272036.post-88386061251022294822009-05-30T20:18:00.006+05:302009-05-30T22:07:52.649+05:30Choosing a suitable access control modifier in JavaHow to choose a suitable access control specifier of a method or a field?Access Control Modifiers available for classes and members in JavaTop-level classes in Java can have only two access control modifiers - public and default (package-private) whereas the members have four access control modifiers - public, protected, default (package-private), and private. Nested Classes are treated as Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com2tag:blogger.com,1999:blog-7955003209126272036.post-44189610830728396162009-05-29T00:47:00.010+05:302009-05-29T01:50:26.311+05:30Finding caller of an instance or static method in JavaFinding Caller object, method/class of an instance method in JavaYou might think if we would ever need it? Well... you may never, but no harm in exploring whether we have any/some ways of finding this in Java or not. This question was asked by one of our visitors - Marco Servetto. Thanks Marco for bringing this question up.For instance methods, by caller you might mean either the object instance Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com4tag:blogger.com,1999:blog-7955003209126272036.post-87565060844488678022009-05-24T17:24:00.003+05:302009-05-24T17:54:33.011+05:30Passing '\n' (new-line) on command line in JavaCan we pass a new-line ('\n') character or any other escape sequence via command line in Java?One of our visitors (Vivek Athalye) asked this in response to the article - Tricky use of static initializer block. Thought of posting the answer as a separate article to increase the chances of it reaching to a wider range of audience.The answer to the query is NO. The question arises, if you pass the Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com2tag:blogger.com,1999:blog-7955003209126272036.post-41991282647430730222009-05-10T03:12:00.007+05:302009-05-10T03:48:48.256+05:30Tricky use of static initializer in Java - Override printlnCan we get a different output without changing the main-method definition?public class HelloMain {/*** @param args*/public static void main(String[] args) { System.out.println("Mr. main.");}}Can we have the output of the above code-segment as "Hi, Mr. main. Bye!" without changing the main-method code?Yeah, we can have the required output by using 'static initializer' blocks effectively. As we Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com2tag:blogger.com,1999:blog-7955003209126272036.post-79536816389506111192009-05-03T12:53:00.004+05:302009-05-03T13:33:43.986+05:30Finding if a number is a power of 2 or not in Java?How to find whether a number is a power of 2 or not in Java?There can be many possible solutions to this problem, but probably the most efficient remains to be the one which uses bit-level manipulation wisely.We'll talk about only that solution here and since it involves bit-level manipulation of binary sequences, so should you require a refresh, first go through this article - 2's complement, 1'Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com5tag:blogger.com,1999:blog-7955003209126272036.post-9613144315952756782009-05-03T12:36:00.005+05:302009-05-03T12:47:42.520+05:30Binary rep of negative numbers in Java - 2's complement2's Complement: Binary representation of negative numbers in JavaNegative numbers in Java are represented using 2's complement. As we know that integers in Java occupy 4 bytes so to understand how a negative integer (say -4) is represented internally in Java, we first need to find the binary equivalent of the positive value of the integer (in this case 4) and subsequently by finding the 2's Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com0tag:blogger.com,1999:blog-7955003209126272036.post-22810430635829749402009-05-02T15:25:00.003+05:302009-05-02T15:30:34.144+05:30Puzzle: Circular Table, Pile of Quarters and 2 PlayersPuzzle: There is a huge pile of quarters and a circular table. There are two people to play a game of placing the quarters down on the table alternately without any overlap. The one who can't put down a quarter loses. Assuming that the pile of quarters is non-exhaustive what will be your winning strategy? Whether you would like to start or let your partner start?Solution: The winning strategy Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com0tag:blogger.com,1999:blog-7955003209126272036.post-90547028109022416242009-05-02T15:12:00.005+05:302009-05-02T15:50:27.751+05:30Viewing/Editing PPT, Doc, TIFF, etc. in your browserViewing/Editing files without having the required s/w or tools installedWonder what will you do in case you end up getting caught in a situation where none of the widely used tools/softwares (such as MS Office, Flash, File Viewers/Editors like Acrobat, etc.) are installed on a machine on which you need to at least view (or maybe edit if possible) some docs/files?Well... until recently it was Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com0tag:blogger.com,1999:blog-7955003209126272036.post-10046725666112418452009-04-04T00:46:00.020+05:302009-04-12T08:19:04.741+05:30Implementation of SAX Parser in Java using SAX2 APIsFor those who have reached to this article directly, before we move on to discussing the implmenetation of a sample SAX-based XML parser in Java, they may like to refresh their understanding of SAX by referring to this article - Evolution of Java and XML combo. SAX, DOM, JAXP, JDOM >>Implementation of a SAX2-based XML parser in JavaWe will start with looking at the various steps involved inGeekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com0tag:blogger.com,1999:blog-7955003209126272036.post-56581100787218482312009-04-01T00:23:00.006+05:302009-06-17T21:11:09.818+05:30SAX v/s DOM. How to choose between DOM and SAX?Differences between DOM and SAX. When to use what? Before going through the differences, if you need a refresh of what SAX and DOM are, please refer to this article - SAX, DOM, JAXP, & JDOM >>.While comparing two entities, we tend to see both of them as competitors and consequently comparing them to find a winner. This of course is not applicable in every case - not at least in the caseGeekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com4tag:blogger.com,1999:blog-7955003209126272036.post-1028111902654073232009-04-01T00:10:00.005+05:302009-06-17T20:50:19.106+05:30Sax, DOM, JAXP, & JDOM. Evolution of Java-XML combo.Evolution of the XML Parsing/Manipulation using JavaThe combination of Java and XML has been one of the most attracting things which had happened in the field of software development in the 21st century. It has been mainly for two reasons - Java, arguably the most widely used programming language and XML, almost unarguably the best mechanism of data description and transfer.Since these two were Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com3tag:blogger.com,1999:blog-7955003209126272036.post-50864939598180747632009-03-22T22:44:00.005+05:302009-03-22T22:59:48.772+05:30Getting Class name in a static method in JavaFinding the name of a Class in Java from within a static methodOne of our regular visitors (Ranvijay) had asked it a couple of days back. Though, the code is pretty straightforward, I think it might help few others if posted as an article.One of the many possible ways of finding the name of the class in Java from within a static method is by using a static nested class, which will have a public Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com2tag:blogger.com,1999:blog-7955003209126272036.post-75674264519917006222009-03-02T20:27:00.008+05:302009-03-02T20:48:46.650+05:30How to recover Shift + Deleted folders/emails in Outlook?How to recover Shift + Deleted folders/emails in Microsoft Outlook?Ever wondered if you can really recover something in Windows, which you have Shift + Deleted? Well, you can do that at least in few cases (if not in all) - one of them being recovering folders/emails which you accidentally Shift + Deleted in your Microsoft Outlook (running with MS Exchange Server). I managed to delete one Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com15tag:blogger.com,1999:blog-7955003209126272036.post-54232250633014635362009-03-02T20:09:00.005+05:302009-03-02T20:45:51.957+05:30Outlook Express v/s Outlook. Which suits you better?Outlook Express v/s MS Outlook. How to choose one between the two?Outlook Express v/s Microsoft OutlookMany of you would already be aware of the main difference between the two. For those, who might not have cared to thought of it so far, here is the summary of the main difference(s) between the two messaging and collaboration clients delivered to us by Microsoft:-Outlook Express: it has been Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com2tag:blogger.com,1999:blog-7955003209126272036.post-16997873030484376972009-02-22T22:03:00.005+05:302009-02-22T22:49:58.682+05:30ThreadLocal & InheritableThreadLocal in Java, initialValue() v/s set(), Uses of childValue()ThreadLocal in Java - what is it used for?As the name suggests this Java library class is used for supporting thread-local variables - the variables which are local to the particular thread instance and hence each thread will have their own copy of such variables which will be initialized every time a new thread is spawned.To be more clear, let's take one simple example. If you have a class Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com3tag:blogger.com,1999:blog-7955003209126272036.post-36776156478115497522009-02-16T23:58:00.006+05:302009-02-17T00:29:43.637+05:30LoadLibrary failed - a DLL initialization routine failedResolving 'LoadLibrary failed - a DLL initialization routine failed' errorThere might be other reasons for this error as well, but the most common one is probably because of a missing dependent DLL. While trying to register SOAPIS30.dll (for those who are new to DLL installation - that's done by using RegSvr32.exe OR Gacutil.exe), I encountered this error.Identifying the problem and fixing that -Geekhttp://www.blogger.com/profile/00648920090539126396noreply@blogger.com3